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:48

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");
查看更多
别忘想泡老子
3楼-- · 2020-04-08 13:56
<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");}
查看更多
趁早两清
4楼-- · 2020-04-08 13:59
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;

}
查看更多
唯我独甜
5楼-- · 2020-04-08 14:00
 GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);                    
                    HiddenField hdMeasurementId = ((HiddenField)row.FindControl("hdMeasurementId"));
查看更多
太酷不给撩
6楼-- · 2020-04-08 14:01

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.

查看更多
登录 后发表回答