如何在asp.net中的Button Click事件中调用RowDataBound事件?

时间:2013-12-13 09:37:47

标签: asp.net

protected void GVPaperrate_RowDataBound(object sender, GridViewRowEventArgs e)
{

    DataTable dtbirdtype = new DataTable();
    objRetailPL.status = 5;
    dtbirdtype = objRetailBAL.GetType(objRetailPL);


    DropDownList ddl1 = (DropDownList)e.Row.FindControl("ddlType");
    if (ddl1 != null)
    {
        ddl1.DataSource = dtbirdtype;
        ddl1.DataTextField = "birdname";
        ddl1.DataValueField = "sno";
        ddl1.DataBind();
        ddl1.Items.Add(new ListItem("--Select--", "0"));
        ddl1.SelectedIndex = ddl1.Items.Count - 1;
    }
    DataTable dtzonedet = new DataTable();
    dtzonedet = objRetailBAL.GetZoneDet();
    DropDownList ddlzone = (DropDownList)e.Row.FindControl("ddlzone");
    if (ddlzone != null)
    {
        ddlzone.DataSource = dtzonedet;
        ddlzone.DataTextField = "ZoneName";
        ddlzone.DataValueField = "SNo";
        ddlzone.DataBind();
        ddlzone.Items.Add(new ListItem("--Select--","0"));
        ddlzone.SelectedIndex=ddlzone.Items.Count-1;

    }
}protected void btngo_Click(object sender, ImageClickEventArgs e)
{
    DataTable dtinsert = new DataTable();
    objRetailPL.ZoneName = txtzone.Text.ToString();
    objRetailPL.Username = Session["Username"].ToString();

    dtinsert = objRetailBAL.InsertZone(objRetailPL);
    if (dtinsert.Rows.Count > 0)
    {
        if (dtinsert.Rows[0]["status"].ToString() == "2")
        {
            ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "insert", "alert('Saved Successfully')", true);
            //GVPaperrate.DataBind();
            txtzone.Text = "";
        }
        else if (dtinsert.Rows[0]["status"].ToString() == "1") 
        {
            ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "insert", "alert('ZoneName Already Exists')", true);
        }
    }
    else

    { 
        ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "insert", "alert('Insertion Failed Please Consult IT Department')", true);
    }
}

如何在按钮Click事件中调用RowDataBound。我想要结果,就好像点击go按钮一样,它会自动在dropdownlist中显示输入的数据。但是我没有那样。我必须做的就是帮助我。

2 个答案:

答案 0 :(得分:0)

我认为您在数据库中插入数据。插入后获取数据并与gridview绑定。

试试这个,     GVPaperrate.DataBind();     为什么你评论这一行。你应该取消评论它会起作用。

您的按钮点击代码

  protected void btngo_Click(object sender, ImageClickEventArgs e)
  {
    DataTable dtinsert = new DataTable();
    objRetailPL.ZoneName = txtzone.Text.ToString();
    objRetailPL.Username = Session["Username"].ToString();

    dtinsert = objRetailBAL.InsertZone(objRetailPL);
    if (dtinsert.Rows.Count > 0)
    {
       if (dtinsert.Rows[0]["status"].ToString() == "2")
       {
           ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "insert", "alert('Saved Successfully')", true);
          **GVPaperrate.DataBind()**; //why you comment this 
           txtzone.Text = "";
       }
       else if (dtinsert.Rows[0]["status"].ToString() == "1") 
       {
          ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "insert", "alert('ZoneName Already Exists')", true);
       }
   }
   else

   { 
    ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "insert", "alert('Insertion Failed Please Consult IT Department')", true);
   }
  }

答案 1 :(得分:0)

单击Go按钮后绑定网格或在Page_PreRender()方法上绑定网格。