how to get the name of the gridview where the linkbutton has been clicked?

  • Thread starter Thread starter Eric
  • Start date Start date
E

Eric

Hi,

there are several gridviews all with a linkbutton which all trigger the same
procedure.
I need to know from which gridview the linkbutton has been clicked.

Thanks
Eric
Here the code:

<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lb1" OnClick="go" runat="server"
Text="go">
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="n1" ReadOnly="True"/>
</asp:BoundField>
</Columns>
</asp:GridView>

<asp:GridView ID="GridView2" runat="server" DataSourceID="SqlDataSource1" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lb1" OnClick="go" runat="server"
Text="go">
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="n2" ReadOnly="True"/>
</asp:BoundField>
</Columns>
</asp:GridView>

Protected Sub go(ByVal sender As Object, ByVal e As System.EventArgs)
dim gv as string
'need to get into a variable the name of the gridview where the linkbutton
has been clicked
End Sub
 
pretty simple

// find my parent gridview

GridView gv = null;
for (var ctl = sender as Control; ctl != null; ctl = ctl.Parent)
{
if (ctl is GridView)
{
gv = ctl as GridView;
break;
}
}


-- bruce (sqlwork.com)
 
Thanks

bruce barker said:
pretty simple

// find my parent gridview

GridView gv = null;
for (var ctl = sender as Control; ctl != null; ctl = ctl.Parent)
{
if (ctl is GridView)
{
gv = ctl as GridView;
break;
}
}


-- bruce (sqlwork.com)
 
I was a little too fast ...

i converted into vb.net:

Protected Sub bekijk(ByVal sender As Object, ByVal e As System.EventArgs)
Dim gv As GridView = Nothing
Dim ctl As Object = TryCast(sender, Control)
While ctl IsNot Nothing
If TypeOf ctl Is GridView Then
gv = TryCast(ctl, GridView)
Exit While
End If
ctl = ctl.Parent
End While

but this gives this: System.Web.UI.WebControls.GridView
what i want is the ID of the gridview.
Thanks
 
gv.ID

--

Riki
I was a little too fast ...

i converted into vb.net:

Protected Sub bekijk(ByVal sender As Object, ByVal e As
System.EventArgs) Dim gv As GridView = Nothing
Dim ctl As Object = TryCast(sender, Control)
While ctl IsNot Nothing
If TypeOf ctl Is GridView Then
gv = TryCast(ctl, GridView)
Exit While
End If
ctl = ctl.Parent
End While

but this gives this: System.Web.UI.WebControls.GridView
what i want is the ID of the gridview.
Thanks
 
Back
Top