Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# How do I create a Hyperlink OnClick event on GridView?

I'm having trouble creating the GridView I want. I would like the user to get inside the website and see the GridView which is attached to a DB. Columns are: ID, InsertionTime, Filepath, ProccessedByUser Now I want the user to click the filepath he/she would like to process. When he/she clicks the filepath, I want their username (logged in with built-in asp website authentication) to be updated (added) into DB.

My markup is standard and I haven't got to manage with code behind.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    DataKeyNames="ID" DataSourceID="AccessDataSource1" 
    onselectedindexchanged="GridView1_SelectedIndexChanged">
    <Columns>
        <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" 
            ReadOnly="True" SortExpression="ID" />
        <asp:BoundField DataField="starttime" HeaderText="starttime" 
            SortExpression="starttime" />
        <asp:HyperLinkField DataNavigateUrlFields="path" DataTextField="path" 
            HeaderText="path" />
        <asp:BoundField DataField="user" HeaderText="user" SortExpression="user" />
    </Columns>
</asp:GridView>

I tried using HyperlinkField but it doesn't seem to support onlick events.

Any suggestions? Thanks.

like image 383
devdc Avatar asked Dec 27 '22 07:12

devdc


1 Answers

I assume you are looking for the LinkButton control which has an OnClick event.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    DataKeyNames="ID" DataSourceID="AccessDataSource1" 
    onselectedindexchanged="GridView1_SelectedIndexChanged">
    <Columns>
        <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" 
            ReadOnly="True" SortExpression="ID" />
        <asp:BoundField DataField="starttime" HeaderText="starttime" 
            SortExpression="starttime" />
        <asp:TemplateField HeaderText="Path" SortExpression="Filepath">
            <ItemTemplate>
                <asp:LinkButton ID="LbPath" runat="server" 
                    Text='<%# Eval("Filepath") %>'
                    CommandName="PathUpdate" 
                    CommandArgument='<%#Bind("path") %>'>
                </asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="user" HeaderText="user" SortExpression="user" />
    </Columns>
</asp:GridView>

Now you can handle the LinkButton's click event or the GridView's RowCommand event.

protected void Gridview1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "PathUpdate")
    {
        string path= e.CommandArgument.ToString();
        // do you what you need to do
    }
}

Note that i've used a TemplateField which is the most dynamic column type in a GridView since you can add anything you want, even nested GridViews or UserControls.

like image 173
Tim Schmelter Avatar answered Jan 05 '23 16:01

Tim Schmelter