我动态生成一个DataTable,我尝试添加在我加入到DataTable DataRow中(S)链接。该数据表绑定到一个GridView之后的创作
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.
类似的东西:
DataTable dataTable = new DataTable(); foreach (Item item in items) { DataRow row = dataTable.NewRow(); dataTable.Columns.Add(new DataColumn("col")); row["col"] = "<a href='www.google'>Link here</a>"; dataTable.Rows.Add(row); }
然后我把它绑定到一个GridView:
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>
但是,当我把它绑定到GridView在列中的HTML编码。 是有办法有添加一个超链接的对象或成这样?
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东西。这不是在本例中,但列的动态添加(这意味着我不渲染我多少列必须知道之前)
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)
更新#1
我有机会到GridView当我创建的列。 我能够做这样的事情:
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='www.google'>Link here</a>";
但它显示2 coloumns山坳...
But it displays 2 coloumns "col"...
更新3: 我用一个DataGrid来代替。在数据行纯文本插入时它不编码的HTML。
UPDATE #3 : I used a DataGrid instead. It doesn't encode HTML when inserted in "plain text" in the data rows.
推荐答案它是一个长久以来这段时间有人问,但我来到这里是第一个谷歌搜索结果中的一个,所以我想告诉你,我已经解决了这个加入了HyperLinkField字段:
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();更多推荐
Link在一个DataRow(数据表)
发布评论