Asp.net 防止在自动生成的GridView列中进行HTML编码
我有一个绑定到我构造的数据表的GridView。表中的大多数列都包含HypeLink的原始HTML,我希望该HTML在浏览器中呈现为链接,但GridView会自动对HTML进行编码,因此它呈现为标记 在不显式添加超链接或任何其他列的情况下,如何避免此问题?只需将属性设置为false:Asp.net 防止在自动生成的GridView列中进行HTML编码,asp.net,gridview,Asp.net,Gridview,我有一个绑定到我构造的数据表的GridView。表中的大多数列都包含HypeLink的原始HTML,我希望该HTML在浏览器中呈现为链接,但GridView会自动对HTML进行编码,因此它呈现为标记 在不显式添加超链接或任何其他列的情况下,如何避免此问题?只需将属性设置为false: =true。但是,我可以想出两种解决您所面临问题的方法: 选项1:继承GridView类,重写Render方法,循环遍历所有单元格,解码其内容,然后执行基本方法: for (int i = 0; i < Ro
=true
。但是,我可以想出两种解决您所面临问题的方法:
选项1:继承GridView
类,重写Render
方法,循环遍历所有单元格,解码其内容,然后执行基本方法:
for (int i = 0; i < Rows.Count; i++)
{
for (int j = 0; j < Rows[i].Cells.Count; j++)
{
string encoded = Rows[i].Cells[j].Text;
Rows[i].Cells[j].Text = Context.Server.HtmlDecode(encoded);
}
}
由于链接的html已经在数据库中,您可以将html输出到文本控件
<asp:TemplateField HeaderText="myLink" SortExpression="myLink">
<ItemTemplate>
<asp:Literal ID="litHyperLink" runat="server" Text='<%# Bind("myLink", "{0}") %>' />
</ItemTemplate>
</asp:TemplateField>
这将使您的链接呈现为原始文本,允许浏览器将其呈现为您期望的链接。另一种方法是向RowDataBound事件处理程序添加如下内容
If e.Row.RowType = DataControlRowType.Header Then
For Each col As TableCell In e.Row.Cells
Dim encoded As String = col.Text
col.Text = Context.Server.HtmlDecode(encoded)
Next
End If
我能够通过使用Jørn Schou Rode提供的解决方案来实现这一点,我对其进行了一些修改,使其能够从Gridview的RowDataBound事件中工作
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int j = 0; j < e.Row.Cells.Count; j++)
{
string encoded = e.Row.Cells[j].Text;
e.Row.Cells[j].Text = Context.Server.HtmlDecode(encoded);
}
}
}
受保护的无效GridView1\u行数据绑定(对象发送方,GridViewRowEventArgs e)
{
如果(e.Row.RowType==DataControlRowType.DataRow)
{
对于(int j=0;j
使用OnRowCreated
protected void gvFm_RowCreated(object sender, GridViewRowEventArgs e)
{
foreach (TableCell cell in e.Row.Cells)
{
BoundField fldRef = (BoundField)((DataControlFieldCell)cell).ContainingField;
switch (fldRef.DataField)
{
case "ColToHide":
fldRef.Visible = false;
break;
case "ColWithoutEncode":
fldRef.HtmlEncode = false;
break;
}
}
}
如果要在所有行和列中禁用HTML编码,可以在RowDataBound事件中使用此代码
protected void GV_Product_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
foreach (TableCell ObjTC in e.Row.Cells)
{
string decodedText = HttpUtility.HtmlDecode(ObjTC.Text);
ObjTC.Text = decodedText;
}
}
}
由于所有的答案似乎都是C语言的,而且问题也不具体,所以我在使用ASP.Net和VB.Net时遇到了这个问题,而被接受的解决方案在VB中对我不起作用(尽管我认为它在C语言中确实起作用)。希望这能帮助任何在ASP中使用VB.Net的人,就像我一样偶然发现这一点
在VB.Net中,不能将BoundColumn
添加到Gridview.Columns
,因为它不是System.Web.UI.WebControl.DataControls
字段,因此必须使用BoundField
,它是DataControlField
For Each dataCol As DataColumn In dv.Table.Columns
Dim boundCol As New BoundField With {
.DataField = dataCol.ColumnName,
.HeaderText = dataCol.ColumnName,
.HtmlEncode = False
}
gvResult.Columns.Add(boundCol)
Next
gvResult.DataSource = dv
gvResult.Databind()
boundColumn
也没有HtmlEncode
属性,但是BoundField
有。另外,在VB.Net中,DataSource
变成了DataField
For Each dataCol As DataColumn In dv.Table.Columns
Dim boundCol As New BoundField With {
.DataField = dataCol.ColumnName,
.HeaderText = dataCol.ColumnName,
.HtmlEncode = False
}
gvResult.Columns.Add(boundCol)
Next
gvResult.DataSource = dv
gvResult.Databind()
还请注意,您必须显式设置AutoGenerateColumns=“False”
,否则GridView仍将生成列以及上面添加的列。HtmlEncode=“False”不建议使用,因为可能会发生XSS攻击