C#如何在GridView上创建Hyperlink事件? [英] C# How do I create a Hyperlink OnClick event on GridView?

查看:117
本文介绍了C#如何在GridView上创建Hyperlink事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法创建我想要的GridView。
我希望用户进入该网站并查看连接到数据库的GridView。
列是: ID,InsertionTime,Filepath,ProccessedByUser
现在我希望用户单击他/她想要处理的文件路径。当他/她单击文件路径时,我希望他们的用户名(使用内置的asp网站身份验证登录)更新(添加)到数据库中。

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>

我尝试使用HyperlinkField,但它似乎不支持onlick事件。

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

有什么建议吗?
Thanks。

Any suggestions? Thanks.

推荐答案

我假设您正在寻找 LinkBut​​ton 控件,其中包含 OnClick 事件。

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>

现在您可以处理 LinkBut​​ton的 点击事件 GridView的 RowCommand 事件。

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
    }
}

请注意,我使用 TemplateField 这是 GridView 中最具动态的列类型,因为您可以添加任何你想要的,甚至嵌套的GridViews或 UserControls

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.

这篇关于C#如何在GridView上创建Hyperlink事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆