Resolved Click link run vb code

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,530
Location
Lansing, MI; USA
Programming Experience
10+
I've got a webapp that displays data in a GrdiView and one of the columns I need to have a link displayed for the user to click on, which I've got just fine:

VB.NET:
<a href="#" runat="server"><%#DataBinder.Eval(Container, "DataItem.FILENAME")%></a>
but when this link is clicked I need it to run a subroutine in the code behind file, similar to handling an asp:ComboBox's SelectedIndexChanged event. How do I do this? I'll also need to pass the <%#DataBinder.Eval(Container, "DataItem.FILENAME")%> value to the routine somehow too. Any ideas?

Edit: Would an asp:LinkLabel be of use?
 
You should be able to just use a ButtonField.

VB.NET:
<gridview>
    <columns>
     ...
    <asp:ButtonField HeaderText="" DataTextField="" ButtonType="Link" CommandName="DoSomething" />
    </columns>
</gridview>


Then put your code n the RowCommand Event for the GridView

VB.NET:
if e.CommandName = "DoSomething" Then
   dim row as GridViewRow = GridView1.Rows(e.CommandArgument)
   'E.CommandArgument is the row index(i believe)
End If
 
Thanks, much appreciated.
VB.NET:
<asp:GridView CssClass="gv" ID="gvWebLocations" runat="server" Width="100%" AutoGenerateColumns="False" OnRowCommand="gvWebLocations_RowCommand">
    <Columns>
        <asp:TemplateField HeaderText="Available Files">
            <ItemTemplate>
                <asp:Label ID="QQNameLabel" runat="server" Text='<%#DataBinder.Eval(Container, "DataItem.FILENAME")%>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:ButtonField ButtonType="Link" CommandName="ZoomTo" Text="Zoom To" HeaderText="" />
        <asp:TemplateField HeaderText="">
            <ItemTemplate>
                <a href="<%#FormatURL(DataBinder.Eval(Container.DataItem, "FILENAME")) %>" target="DL">Download Data</a>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
    <EmptyDataTemplate>
        <div>Select a map location to find files to download.</div>
    </EmptyDataTemplate>
    <EmptyDataRowStyle  CssClass="gvEmptyData" />
    <HeaderStyle CssClass="gvHeader"  />
    <AlternatingRowStyle CssClass="odd" />
</asp:GridView>

Protected Sub gvWebLocations_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
    If e.CommandName = "ZoomTo" Then
        Dim QQName As String = DirectCast(gvWebLocations.Rows(Convert.ToInt32(e.CommandArgument)).Cells(0).Controls(1), Label).Text
        'Rest of code....
    End If
End Sub
 
No problem.

I've always retrieved data from code the way you have but I think adding the field name to the DataKeyNames property is the better option.


VB.NET:
<asp:GridView CssClass="gv" ID="gvWebLocations" runat="server" Width="100%" AutoGenerateColumns="False" OnRowCommand="gvWebLocations_RowCommand" DataKeyNames="FILENAME">

Then using something like this
VB.NET:
Dim QQName As String = gvWebLocations.DataKeys(e.CommandArgument).Value
 
Is there a way to have 2 DataKeyNames specified? In the actual app here I'd have a need to have 2 of them specified if I were to do it using the DataKeyNames field.
 
I'm pretty sure you can just separate them by commas. then in the code just use .Values() instead of .Value()
 
Back
Top