How can we find the control in the row grid view command?
9 replies
There is actually no Row in the GridViewCommandEventArgs, so you will need to get the row from the command source naming container
GridViewRow row = (GridViewRow)(((Control)e.CommandSource).NamingContainer);
then you can use
TextBox myTextBox = row.FindControl("MyTextBoxId") as TextBox;
Hope this helps!
+17
source to share
You can use "CommandArgument" in your control with "CommandName". Here are 2 arguments:
<asp:LinkButton ID="UpdateButton" runat="server" CommandName="Update" CommandArgument='<%# Container.DataItemIndex + ";" + Eval("idinterlocuteur") %>'>
Then, in your code behind, you can get the arguments:
string[] arg = e.CommandArgument.ToString().Split(';');
int index = Convert.ToInt16(arg[0]);
string idinterlocuteur = arg[1];
And now you can find your controls:
CheckBox Check1 = GridView1.Rows[index].FindControl("MyCheckboxinrow") as CheckBox;
0
source to share
If you are using usercontrols on your gridview item then ((Control)e.CommandSource).NamingContainer
your gridviewrow may not return.
In this case, I used the following code to get the current line:
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.
0
source to share
<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");}
0
source to share