Gridview中的超链接在c#中?

时间:2017-06-24 20:30:27

标签: c# asp.net gridview hyperlink

当我点击超链接时,我想从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];
        }
    }

1 个答案:

答案 0 :(得分:0)

此示例可以提供帮助:

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