如何使用gridview中的linkbutton导航到URL

时间:2015-02-21 21:18:23

标签: c# asp.net gridview linq-to-sql

我有一个gridview,它存储软件名称和按钮字段。我想要做的是通过执行“打开”链接按钮后面的查询和结果答案,这是一个来自数据库的URL,我想导航到存储在变量中的那个链接。 例如:当我点击打开按钮时,我想导航到“www.google.com”

到目前为止,我编码的是:

<asp:GridView ID="GridView2" runat="server" ShowHeaderWhenEmpty="True" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" DataSourceID="softwares" ForeColor="Black" GridLines="Vertical" OnRowCommand="GridView2_RowCommand">
                <AlternatingRowStyle BackColor="#CCCCCC" />
                <Columns>
                    <asp:BoundField DataField="Softwares" HeaderText="Softwares" SortExpression="Softwares" />
                    <asp:ButtonField CommandName="open" Text="Open" />
                </Columns>
                <FooterStyle BackColor="#CCCCCC" />
                <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
                <SortedAscendingCellStyle BackColor="#F1F1F1" />
                <SortedAscendingHeaderStyle BackColor="#808080" />
                <SortedDescendingCellStyle BackColor="#CAC9C9" />
                <SortedDescendingHeaderStyle BackColor="#383838" />
            </asp:GridView>
            <asp:SqlDataSource ID="softwares" runat="server" ConnectionString="<%$ ConnectionStrings:certConnectionString %>" SelectCommand="SELECT sw_name AS Softwares FROM Software"></asp:SqlDataSource>

C#代码:

protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if(e.CommandName == "open")
            {
                using(CertDataContext context=new CertDataContext())
                {
                    int i=Convert.ToInt32(e.CommandArgument);
                    var open = (from a in context.Softwares where a.sw_name == GridView2.Rows[i].Cells[0].Text select a).FirstOrDefault();
                    Response.Redirect(open.tosting());
                }
            }
        }

1 个答案:

答案 0 :(得分:0)

HttpResponse.Redirect(string)

参数CAN可以是绝对URL。查看Microsoft在其文档中提供的示例。