本文介绍了如何在asp.net的gridview中创建链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个具有 gridview 的网页.这个gridview应该有一个像下面这样的链接

I am trying to create a webpage that has a gridview. this gridview is supposed to have a link like below

http://localhost/Test.aspx?code=123

当用户在 gridview 中点击其中一行的链接时,它会打开一个空白页面并显示一些结果.

when the user clicks one of the rows' link in gridview, it will open a blank page and display some result.

这是我如何将数据绑定到 gridview 但我不知道如何设置链接

here is how I bind data to the gridview but I dont know how to set the link

protected void Page_Load(object sender, EventArgs e)
{
    string firma_no = logoFrmNr.ToString().PadLeft(3, '0');
    string active_period = logoFrmPeriod.PadLeft(2, '0');

    SqlConnection conn = new SqlConnection(conStr);
    string selectSql = @"SELECT
                                LOGICALREF,
                                CODE ,
                                DEFINITION_ ,
                                FROM
                                LG_CLFLINE";

    SqlCommand cmd = new SqlCommand(selectSql, conn);
    DataTable dt = new DataTable();
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    da.Fill(dt);
    GridView1.DataSource = dt;
    GridView1.DataBind();

    conn.Close();
}

这里是标记

<asp:GridView ID="GridView1" runat="server" EnableModelValidation="True">
</asp:GridView>

如何在 CODE 列外建立链接?

How can I make a link out of CODE column?

推荐答案

这有一个技巧.Hyperlinkcolumn 将不起作用,因为您无法格式化链接.您想使用边界字段并格式化文本.像这样

There's a trick to this. The Hyperlinkcolumn won't work, because you can't format the link. You want to use a boundfield and format the text. Like so

<asp:GridView ID="GridView1" runat="server" EnableModelValidation="True">
    <Columns>
        <asp:BoundField DataField="Code" HtmlEncode="False" DataFormatString="<a target='_blank' href='Test.aspx?code={0}'>Link Text Goes here</a>" />
    </Columns>
</asp:GridView>

或者,如果您需要指定编辑和插入模板,您可以使用模板字段.

Alternately, you can use a templatefield if you need to designate edit and insert templates.

这篇关于如何在asp.net的gridview中创建链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 02:58