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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14748675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:54:51  来源:igfitidea点击:

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

c#asp.net.netvb.netvisual-studio

提问by hardeep

How can I find the control in the row commandof grid view?

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

回答by Vasil Trifonov

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

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

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

then you will be able to use

然后你就可以使用

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

Hope this helps!

希望这可以帮助!

回答by smith269

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 lablewith Id lblthen use..

例如,如果你想找到一个带有 Id lbl标签,那么使用..

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

回答by 0nly.Phani

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

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

回答by Lionel Page

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

您可以在带有“CommandName”的控件中使用“CommandArgument”。这里有2个参数:

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

回答by breez

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

如果您在 gridview itemtemplate 中使用用户控件,则((Control)e.CommandSource).NamingContainer可能不会返回您的 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.

它不漂亮,但它有效。

回答by Krishna shidnekoppa

if your using LinkButton

如果您使用 LinkBut​​ton

 LinkButton ctrl = e.CommandSource as LinkButton;
   if (ctrl != null)
    {
        GridViewRow row = ctrl.Parent.NamingContainer as GridViewRow;
        TextBox txtDescription = (TextBox)row.FindControl("txtDescription");
    }

回答by kaszona

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

回答by Mayur Khairnar

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

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

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

回答by Mayur Khairnar

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;

}

回答by user10290759

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