How can we find the control in the row command of

2020-04-08 13:05发布

How can I find the control in the row command of grid view?

11条回答
来,给爷笑一个
2楼-- · 2020-04-08 13:40

GridViewCommandEventArgs not supported row so use naming container to find the control..

  protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
       Control ctrl = e.CommandSource as Control;  
       if (ctrl != null)
       {
           GridViewRow gvRow = ctrl.Parent.NamingContainer as GridViewRow; 
           Label slno = (Label)gvRow.FindControl("slno");  // Find Your Control here
           TextBox txtno = (TextBox)gvRow.FindControl("txtno");  // Find Your Control here
           // Your work start here
       }
    }
查看更多
看我几分像从前
3楼-- · 2020-04-08 13:41
GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);

Label lblProdId = (Label)row.FindControl(“lblproductId”);
查看更多
Emotional °昔
4楼-- · 2020-04-08 13:41

You can use "CommandArgument" in your Control with "CommandName". Here 2 arguments :

<asp:LinkButton ID="UpdateButton" runat="server" CommandName="Update" CommandArgument='<%# Container.DataItemIndex + ";" + Eval("idinterlocuteur") %>'>

Then in your code behind you can get arguments :

string[] arg = e.CommandArgument.ToString().Split(';');
int index = Convert.ToInt16(arg[0]);
string idinterlocuteur = arg[1];

And now you arguments to find your controls :

CheckBox Check1 = GridView1.Rows[index].FindControl("MyCheckboxinrow") as CheckBox;
查看更多
Anthone
5楼-- · 2020-04-08 13:43

GridViewRow gvr = (GridViewRow)((Control)e.CommandSource).NamingContainer; int rowIndex = gvr.RowIndex;

        string Cat_name = (GridView1.Rows[rowIndex].FindControl("TxtName") as TextBox).Text;
查看更多
干净又极端
6楼-- · 2020-04-08 13:45

Actually there is no Row in GridViewCommandEventArgs, so you will need to get the row from the command source naming container

GridViewRow row = (GridViewRow)(((Control)e.CommandSource).NamingContainer);

then you will be able to use

TextBox myTextBox = row.FindControl("MyTextBoxId") as TextBox;

Hope this helps!

查看更多
Luminary・发光体
7楼-- · 2020-04-08 13:48

if your using LinkButton

 LinkButton ctrl = e.CommandSource as LinkButton;
   if (ctrl != null)
    {
        GridViewRow row = ctrl.Parent.NamingContainer as GridViewRow;
        TextBox txtDescription = (TextBox)row.FindControl("txtDescription");
    }
查看更多
登录 后发表回答