5

如何在网格视图的行命令中找到控件?

4

11 回答 11

24

实际上 GridViewCommandEventArgs 中没有 Row,因此您需要从命令源命名容器中获取行

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

那么你就可以使用

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

希望这可以帮助!

于 2013-02-07T10:40:54.783 回答
3

如果您使用 LinkBut​​ton

 LinkButton ctrl = e.CommandSource as LinkButton;
   if (ctrl != null)
    {
        GridViewRow row = ctrl.Parent.NamingContainer as GridViewRow;
        TextBox txtDescription = (TextBox)row.FindControl("txtDescription");
    }
于 2016-12-07T11:56:17.833 回答
1
GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);

Label lblProdId = (Label)row.FindControl(“lblproductId”);
于 2015-02-11T12:14:21.570 回答
0

如果你想在行命令中找到一个控件使用

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

例如,如果你想找到一个ID为lbl的标签,那么使用..

Label lbl = (Label)e.Row.FindControl("lbl");
于 2013-02-07T11:04:32.123 回答
0

您可以在控件中使用“CommandArgument”和“CommandName”。这里有2个论点:

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

然后在你后面的代码中你可以获得参数:

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

现在你争论找到你的控制:

CheckBox Check1 = GridView1.Rows[index].FindControl("MyCheckboxinrow") as CheckBox;
于 2015-06-04T14:45:08.583 回答
0
<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");}
于 2017-01-25T12:23:26.410 回答
0

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

        string Cat_name = (GridView1.Rows[rowIndex].FindControl("TxtName") as TextBox).Text;
于 2017-10-30T06:49:17.560 回答
0
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;

}
于 2017-10-30T06:52:27.753 回答
0

如果您在 gridview 项目模板中使用用户控件,则((Control)e.CommandSource).NamingContainer可能不会返回您的 gridviewrow。

在这种情况下,我使用以下代码获取当前行:

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

它不漂亮,但它有效。

于 2016-07-26T13:35:14.267 回答
0
 GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);                    
                    HiddenField hdMeasurementId = ((HiddenField)row.FindControl("hdMeasurementId"));
于 2018-08-29T15:03:55.197 回答
0

GridViewCommandEventArgs 不支持行,所以使用命名容器来查找控件。

  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
       }
    }
于 2019-03-13T18:14:58.670 回答