如何在asp.net中的gridview中创建一个链接 [英] how to create a link in gridview in asp.net

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

问题描述

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

  http://localhost/Test.aspx?code = 123 $ b $当用户在gridview中点击其中一行的链接时,它会打开一个空白页面并显示一些结果。 



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

  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 =GridView1runat =serverEnableModelValidation =True> 
< / asp:GridView>

如何从CODE列创建链接?

解决方案

这有一个技巧。超链接列将不起作用,因为您无法格式化链接。您想要使用绑定字段并格式化文本。像这样

 < asp:GridView ID =GridView1runat =serverEnableModelValidation =True> 
<列>
< /列>
< / asp: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

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

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

here is the markup

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

How can I make a link out of CODE column?

解决方案

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中创建一个链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆