日期:2014-05-17  浏览次数:20439 次

C#获取Gridwiew里面textbox的值
<asp:TemplateField HeaderText="确定操作">
  <EditItemTemplate>
  <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
  </EditItemTemplate>
  <ItemTemplate>
  &nbsp;&nbsp;&nbsp;
  <asp:Button ID="btnEdit" CommandArgument='<%# Eval("ClsID") %>' runat="server" 
  Text="修改" onclick="btnEdit_Click" />
   
  &nbsp;&nbsp;
  <asp:Button ID="btnDel" runat="server" CommandArgument='<%# Eval("ClsID") %>' Text="删除" />
  </ItemTemplate>
  </asp:TemplateField>.

我要在后台获取到textbox的值



后台我写的如下
  int clsID = Convert.ToInt32(((Button)sender).CommandArgument);
  string name = (gwNwsType.Rows[0].Cells[1].FindControl("txtNewsType") as TextBox).Text.Trim();
  int orderby = Convert.ToInt32((gwNwsType.Rows[0].Cells[2].FindControl("txtPaiXu") as TextBox).Text.Trim());

  if (newsService.Update_D_NewsType_ByClsID(clsID, name, orderby))
  {
  Page.ClientScript.RegisterClientScriptBlock(typeof(string), "", "<script>alert('修改成功')</script>");
  }
  else
  {
  Page.ClientScript.RegisterClientScriptBlock(typeof(string), "", "<script>alert('修改成功')</script>");
  }
  ((Button)sender).Text = "修改";
  (gwNwsType.Rows[0].Cells[1].FindControl("txtNewsType") as TextBox).ReadOnly = true;
  (gwNwsType.Rows[0].Cells[2].FindControl("txtPaiXu") as TextBox).ReadOnly = true;



我只能获取到第一行的 我要动态控制他们,想控制那个都行 怎么写

------解决方案--------------------
加个选中控件,循环判断
------解决方案--------------------
Button.Parent.Parent as GridViewRow

C# code


 protected    void    GridView1_RowCommand(object    sender,    GridViewCommandEventArgs    e)   
   {   
           if    (e.CommandName    ==    "MyCommand")   
           {   
                   Button    btn    =    e.CommandSource    as    Button;   
                   GridViewRow    row    =    btn.Parent.Parent    as    GridViewRow;   
                   string    a    =    row.Cells[0].ToString();//获得第一个单元格的值   

           }   
   }