DataRow(数据表)中的链接

发布于 2024-10-01 00:53:59 字数 1361 浏览 8 评论 0原文

我正在动态构建一个数据表,并尝试在要添加到数据表的数据行中添加一个“链接”。 DataTable 在创建后绑定到 GridView。

类似这样的事情:

   DataTable dataTable = new DataTable();
   foreach (Item item in items)
    {
        DataRow row = dataTable.NewRow();
        dataTable.Columns.Add(new DataColumn("col"));

        row["col"] = "<a href='http://www.google.com'>Link here</a>";

        dataTable.Rows.Add(row);

    }

然后我将它绑定到 GridView :

        <asp:GridView ID="grdView" Runat="server" border="0" EnableViewState="true" style="width:100%;"
            AutoGenerateColumns="true" AllowPaging="false" PagerSettings-Visible="false" 
            ShowHeader="true" ShowFooter="true" CellPadding="0" CellSpacing="0"
            Visible="True">
        </asp:GridView>

但是当我将它绑定到 GridView 时,Column 中的 HTML 会被编码。 有没有办法在那里添加超链接对象或类似的东西?

PS 它不在示例中,但列是动态添加的(这意味着我在渲染之前不知道我将拥有多少列)

更新#1

当我创建列。 我能够做类似的事情:

    dataTable.Columns.Add(new DataColumn("col"));

    BoundField bf = new BoundField();
    bf.HtmlEncode = false;
    bf.DataField = "col";
    grd.Columns.Add(bf);

   row["col"] = "<a href='http://www.google.com'>Link here</a>";

但它显示 2 列“col”...

更新#3: 我改用了 DataGrid。当插入数据行中的“纯文本”时,它不会对 HTML 进行编码。

I'm building a DataTable dynamically and I'm trying to add a "link" in the DataRow(s) that I'm adding to the DataTable. The DataTable is bound to a GridView after it's creation.

Something like that :

   DataTable dataTable = new DataTable();
   foreach (Item item in items)
    {
        DataRow row = dataTable.NewRow();
        dataTable.Columns.Add(new DataColumn("col"));

        row["col"] = "<a href='http://www.google.com'>Link here</a>";

        dataTable.Rows.Add(row);

    }

Then I bind it to a GridView :

        <asp:GridView ID="grdView" Runat="server" border="0" EnableViewState="true" style="width:100%;"
            AutoGenerateColumns="true" AllowPaging="false" PagerSettings-Visible="false" 
            ShowHeader="true" ShowFooter="true" CellPadding="0" CellSpacing="0"
            Visible="True">
        </asp:GridView>

But the HTML in the Column is encoded when I bind it to the GridView.
Is there a way to add a HyperLink object there or something like that?

P.S. It's not in the example but the columns are added dynamically (it means that I don't know before the rendering how many columns I'll have)

UPDATE #1

I have access to the GridView when I create the columns.
I was able to do something like that :

    dataTable.Columns.Add(new DataColumn("col"));

    BoundField bf = new BoundField();
    bf.HtmlEncode = false;
    bf.DataField = "col";
    grd.Columns.Add(bf);

   row["col"] = "<a href='http://www.google.com'>Link here</a>";

But it displays 2 coloumns "col"...

UPDATE #3 :
I used a DataGrid instead. It doesn't encode HTML when inserted in "plain text" in the data rows.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

冷默言语 2024-10-08 00:53:59

如果您从查询中返回 html 代码,只需在绑定字段上使用 htmlEncode=False 即可。还要在您的 gridview 上设置 AutoGenerateColumns="false",这就是您在 gridview 上获得双列的原因。

If you are returning html code from your query, just use htmlEncode=False on your boundfield. Also set AutoGenerateColumns="false" on your gridview, that's why you're getting double columns on your gridview.

腹黑女流氓 2024-10-08 00:53:59

自从有人问这个问题以来已经很长时间了,但我到达这里是第一个谷歌结果之一,所以我想告诉你我已经解决了这个问题,添加了一个“HyperLinkField”:

HyperLinkField myLink = new HyperLinkField();
myLink.HeaderText = "Link Here";
myLink.DataTextField = "Click here";
myLink.DataNavigateUrlFields = new string[] { "field1", "field2", "field3" };
myLink.DataNavigateUrlFormatString = "NewPage.aspx?id={0}&otherId={1}&otherId2={2}";

myGridView.Columns.Add(myLink);

//Finally bind the data...
myGridView.DataBind();

It's been an long time since this was asked, but I arrived here being one of the first google results, so I would like to tell you that I have resolved this adding a "HyperLinkField":

HyperLinkField myLink = new HyperLinkField();
myLink.HeaderText = "Link Here";
myLink.DataTextField = "Click here";
myLink.DataNavigateUrlFields = new string[] { "field1", "field2", "field3" };
myLink.DataNavigateUrlFormatString = "NewPage.aspx?id={0}&otherId={1}&otherId2={2}";

myGridView.Columns.Add(myLink);

//Finally bind the data...
myGridView.DataBind();
风柔一江水 2024-10-08 00:53:59

抱歉,忽略...我没有看到动态表约束
为什么要在GridView中添加模板列,然后添加超链接。抱歉,此代码在此空间中似乎无法正确格式化

    <asp:TemplateColumn>                                                                        <ItemTemplate>
<asp:HyperLink runat="server" ID="hypLink" Text="test" Target='<%# DataBinder.Eval(Container.DataItem, "moved_ref_amt").ToString() %>'></asp:HyperLink>                                                                        </ItemTemplate>                                                                            </asp:TemplateColumn>

sorry disregard...I didnt see the dynamic table constraint
Why do you add a template column to the GridView and then add a hyperlink. Sorry this code wont seem to format correctly in this space

    <asp:TemplateColumn>                                                                        <ItemTemplate>
<asp:HyperLink runat="server" ID="hypLink" Text="test" Target='<%# DataBinder.Eval(Container.DataItem, "moved_ref_amt").ToString() %>'></asp:HyperLink>                                                                        </ItemTemplate>                                                                            </asp:TemplateColumn>
心碎无痕… 2024-10-08 00:53:59

GridView 有一个 GridView_RowDataBound 事件,它允许您动态地将控件注入到行中、格式化内容等。

您将可以访问表的行元素 (DataItem),并且能够根据需要解析该元素并格式化网格行。

有关良好的介绍示例,请参阅: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx?ppud=4

编辑:您收到重复的列,因为您有 < code>AutoGenerateColumns="true" 以及列的模板。

GridView has a GridView_RowDataBound event which allow you to dynamically inject controls in to rows, format the content, etc.

You will have access to the table's row element (DataItem) and will be able to parse that and format the grid row as you need.

For a good intro example see: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx?ppud=4

Edit: You are getting duplicate columns because you have AutoGenerateColumns="true" as well as the template for the column.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文