我在gridview里利用模版列,RowDataBond事件中是这样写的:
 protected void GW_Ctable_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            UserEn ew = new UserEn();
            UserBLL ub = new UserBLL();
            TextBox txtID = (TextBox)e.Row.Cells[0].FindControl("txtID");
            TextBox txtName = (TextBox)e.Row.Cells[1].FindControl("txtName");
            TextBox txtPassword = (TextBox)e.Row.Cells[2].FindControl("txtPassword");
            DataRow row = ((DataRowView)(e.Row.DataItem)).Row;
            txtID.Text = row["ID"].ToString();
            txtName.Text = row["Name"].ToString();
            txtPassword.Text = row["Password"].ToString();        }
可是在更新的RowUpdating事件中怎么取的改变后的值呢
我是这么写的
ID = ((TextBox)GW_Ctable.Rows[e.RowIndex].Cells[0].Controls[0]).Text.ToString();
可是报无法将类型为“System.Web.UI.LiteralControl”的对象强制转换为类型“System.Web.UI.WebControls.TextBox”。
怎么写才正确呀,大家帮帮忙

解决方案 »

  1.   

    你在控件上把DataKeysField属性赋值
    然后在RowUpdateing事件中 
    用GridView1.DataKeys[(int)e.RowIndex].Value.ToString();能不能找到?
      

  2.   

    Controls[0] 改成Controls[1]
    或者用((TextBox)GW_Ctable.Rows.FindControl("controlID")).Text
      

  3.   

    string id = GridView1.DataKeys[e.NewEditIndex].Value.ToString();
      

  4.   

    string ID;
    ls正确,不过不是取得更改后的值,和我想的不一样啊
      

  5.   

    songxr001() ,我是在更新的RowUpdating事件写的,提示说不包括e.NewEditIndex的定义啊
      

  6.   

    ID = ((TextBox)GW_Ctable.Rows[e.RowIndex].Cells[0].Controls[0]).Text.ToString();
    可是报无法将类型为“System.Web.UI.LiteralControl”的对象强制转换为类型说明控件Controls[0]是LiteralControl类型 你看看你在模板列放的是不是TextBox
    不是的话 不能强制转换为TextBox还有 你如果是模板列自定义的控件,就用FindControl("controlID")
    如果是不是模板列就用Controls[0]
      

  7.   

    string str=((LiteralControl)GW_Ctable.Rows[e.RowIndex].Controls[0].Controls[0]).Text
      

  8.   

    谢谢大家,问题解决了
      TextBox txtID = (TextBox)GW_Ctable.Rows[e.RowIndex].Cells[0].FindControl("txtID");
      

  9.   

    看看 http://www.zhibin.cn/article.asp?id=10
    分析及解决问题
      

  10.   

    http://www.zhibin.cn/article.asp?id=10