How can we find the control in the row command of

2020-04-08 13:25发布

问题:

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

回答1:

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!



回答2:

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");
    }


回答3:

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

Label lblProdId = (Label)row.FindControl(“lblproductId”);


回答4:

If u want to find a control in row command Use

controlname controlId=(controlname)e.FindControl("controlId"); 

For example if u want to find a lable with Id lbl then use..

Label lbl = (Label)e.Row.FindControl("lbl");


回答5:

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;


回答6:

If you are using usercontrols in your gridview itemtemplate then ((Control)e.CommandSource).NamingContainer might not return your gridviewrow.

In that case I used the following code for getting the current row:

var c = ((Control) e.CommandSource).NamingContainer;
while (c.GetType() != typeof(GridViewRow))
{
    c = c.Parent;
}
var currentRow = (GridViewRow) c;

It's not pretty, but it works.



回答7:

<asp:TemplateField HeaderText="Next Date To Attend" ItemStyle-CssClass="col-md-2" >
            <EditItemTemplate>
                 <asp:TextBox ID="NextAttendTextBox" CssClass="col-sm-12" runat="server"></asp:TextBox>
                <span class="text-muted">DD-MM-YYYY</span>
            </EditItemTemplate>
            <ItemTemplate>
               <%#Eval("NextAttend") %>
            </ItemTemplate>
            <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Update Status" ItemStyle-CssClass="col-md-1" >
            <EditItemTemplate>
                 <div class="btn-group">

                <asp:LinkButton ID="LinkButton31" class="btn btn-sm btn-success" CommandArgument='<%#Container.DataItemIndex %>' CommandName="UpdateStat" runat="server" >
                    <i class="ace-icon fa fa-save"></i></asp:LinkButton>
               <asp:LinkButton ID="LinkButton32" class="btn btn-sm btn-error" CommandName="Cancel" runat="server" >
                    <i class="ace-icon fa fa-close"></i></asp:LinkButton>
                    </div>
            </EditItemTemplate>
            <ItemTemplate>
                <div class="btn-group">
                <asp:LinkButton ID="LinkButton3" class="btn btn-sm btn-warning" CommandName="Edit" runat="server" >
                    <i class="ace-icon fa fa-upload"></i></asp:LinkButton>

                    </div>
            </ItemTemplate>
            <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
        </asp:TemplateField>


 if (e.CommandName == "UpdateStat")
        {
            HiddenField IDHiddenField=(HiddenField)GridView1.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("IDHiddenField");
            TextBox CurrentStatDesTextBox=(TextBox)GridView1.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("CurrentStatDesTextBox");}


回答8:

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

        string Cat_name = (GridView1.Rows[rowIndex].FindControl("TxtName") as TextBox).Text;


回答9:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)    
{

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

string Cat_name = (GridView1.Rows[rowIndex].FindControl("TxtName") as TextBox).Text;

}


回答10:

 GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);                    
                    HiddenField hdMeasurementId = ((HiddenField)row.FindControl("hdMeasurementId"));


回答11:

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
       }
    }