【问题标题】:How can I assign a callback function to an asp:HyperlinkField?如何将回调函数分配给 asp:HyperlinkField?
【发布时间】:2009-06-26 19:36:41
【问题描述】:

我希望人们单击链接(从 asp:HyperlinkField 生成)并让它调用服务器上的方法,而不是将用户重定向到某个地方。有人知道怎么做吗?

谢谢,
马特

【问题讨论】:

    标签: asp.net gridview callback hyperlink


    【解决方案1】:

    我会改用 asp:CommandField 或 asp:ButtonField 并使用 ButtonType=Link - 这看起来与您的链接字段相同,然后您可以处理网格中的 OnRowCommand 事件来运行您的代码。

    【讨论】:

    • 是的,你是对的,不需要添加模板栏(就像我的回答一样),这个很简单:)
    【解决方案2】:

    改用 asp:LinkBut​​ton。除非有特殊原因您附加到 asp:Hyperlink?

    【讨论】:

    • 不,没有理由,只是不想让它看起来像一个按钮。谢谢。
    【解决方案3】:

    HyperLinkField 用于在数据绑定控件中生成简单的超链接。相反,您可以使用ButtonField。或者您可以使用TemplateField 定义您自己的链接。

    这是一个生成链接列的示例,其中包含服务器端事件:

    <asp:templatefield headertext="Link Column">
        <itemtemplate>
          <asp:LinkButton ID="myLink" 
                CommandName="MyLinkCommand" 
                CommandArgument='<%#Bind("TableID") %>'
                runat="server">My Link</asp:LinkButton>
        </itemtemplate>
    </asp:templatefield>
    

    在代码后面:

    protected void YouGridView_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "MyLinkCommand")
        {
            // Do stuff
        }
    }
    

    【讨论】:

      【解决方案4】:

      为什么不使用 ButtonField 并将 ButtonType property 设置为链接?它看起来就像一个超链接。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多