gridview更新时获取不到textbox中新值的解决方法

发布时间:2020-11-04编辑:脚本学堂
在asp.net gridview中,更新数据时无法获取textbox中的新值,这里分享下我的解决方法,供大家参考。

本节内容:
gridview更新时获取不到textbox中新值

在 GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) 中,有如下的代码:
      

复制代码 代码示例:
int Index = e.RowIndex;
        string pcnum = GridView1.DataKeys[Index].Value.ToString();
        string pctime = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim();
        string pcplace = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim();
        string pcteacher = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString().Trim();
        if ((DropDownList1.SelectedIndex != 0) && (DropDownList2.SelectedIndex != 0) && (DropDownList3.SelectedIndex != 0))
        {
            string strCond1 = DropDownList1.SelectedItem.ToString();  //获取DropDownList中的选项值
            string strCond2 = DropDownList2.SelectedItem.ToString();
            string strCond3 = DropDownList3.SelectedItem.ToString();
            string sqlstring = "update JW_pcourse set Pctime='" + pctime + "',Pcplace='" + pcplace + "',Pcteacher='" + pcteacher + "' where Pcnum='" + pcnum + "' and Pmajor='" + strCond1
            + "' and Pgrade='" + strCond2 + "' and Pstartterm='" + strCond3 + "'";
            myConnection.ConnectionString = myConnectionString;
            myConnection.Open();
            SqlCommand cmd = new SqlCommand(sqlstring, myConnection);
            cmd.ExecuteNonQuery();
            myConnection.Close();
            GridView1.EditIndex = -1;
            DropDownBind();
        }
 

一直获取不了新的值,需要进行如下的操作:
   

GridView是在后台进行绑定的,把Page_Load里绑定的代码放在
    if(!IsPostBack){}里面
 

其余均成功。