【问题标题】:Hyperlink in Gridview in c#?c#中Gridview中的超链接?
【发布时间】:2017-11-28 04:09:06
【问题描述】:

当我单击超链接时,我想从 gridview 行中提取文章 ID。我的目标是当我单击 gridview 中的超链接时,能够捕获特定行的文章 id 字段。

这是我迄今为止尝试过的,但由于某种原因,当我点击超链接时它不会进入代码隐藏。

<asp:GridView ID="Rssfeed" runat="server" AutoGenerateColumns="false" onrowcommand="grid_RowCommand" CssClass="Grid">
        <Columns>

            <asp:TemplateField HeaderText="Info">
                <ItemTemplate>
                    Articleid:
                    <asp:Label ID="Label1" Text='<%#Eval("articleid") %>' runat="server" />
                    <br />
                    Title :
                    <asp:Label ID="Label2" Text='<%#Eval("title") %>' runat="server" />
                    <br />
                    Link:
                  <asp:HyperLink ID="hlnkFile"  runat="server" target="_blank" CommandName="Select" NavigateUrl='<%# Eval("link") %>' Text='<%# Eval("link") %>'></asp:HyperLink>                 <br />
                     Publicationdate:
                    <asp:Label ID="Label3" Text='<%#Eval("publicationdate") %>' runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>



public void grid_RowCommand(Object sender, GridViewCommandEventArgs e)
    {

        // If multiple ButtonField column fields are used, use the
        // CommandName property to determine which button was clicked.
        if (e.CommandName == "Select")
        {
            // Convert the row index stored in the CommandArgument
            // property to an Integer.
            int index = Convert.ToInt32(e.CommandArgument);

            // Get the last name of the selected author from the appropriate
            // cell in the GridView control.
            GridViewRow selectedRow = Rssfeed.Rows[index];
        }
    }

【问题讨论】:

  • 问题是超链接不会执行回发。如果您需要回发,则必须使用 LinkBut​​ton

标签: c# asp.net gridview hyperlink


【解决方案1】:

这个例子可以提供帮助:

<ItemTemplate>
   Link:
   <asp:LinkButton ID="lbFile" CommandName="Select" runat="server"><%# Eval("link") %></asp:LinkButton>
</ItemTemplate>

后面的代码

public void Rssfeed_RowCommand(Object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Select")
        {
            // get text from LinkButton e.g. URL as in question
            string link = ((LinkButton)selectedRow.FindControl("lbFile")).Text;

            // Redirect to the URL link
            Response.Redirect(link);
        }
    }

【讨论】:

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