ASP.NET 数据表格HTML Output(显示)
-
03-07-2019 - |
题
目前,我结合的数据集与数据表格.
ds = query.ExecuteReadQuery("select PollQuestionText as 'Survey Question',
PollAnswer1Text as 'Selection 1', PollAnswer2Text as 'Selection 2', PollAnswer3Text
as 'Selection 3', PollEnabled 'Status' from tbl_pollquestions")
For Each row As Data.DataRow In ds.Tables(0).Rows
If row.ItemArray(4).ToString = "0" Then
row.ItemArray."<a href=""""> <img src=""img/box_icon_edit_pencil1.gif"" border=""0""> </a>"
ElseIf row.ItemArray(4).ToString = "1" Then
row.Item(4) = "<a href=""""> <img src=""img/box_icon_edit_pencil2.gif"" border=""0""> </a>"
End If
Next
GridView1.DataSource = ds
GridView1.DataBind()
因为我插入html代码,为什么这不是正在转化为html?
输出结果,所有文本。(假设是正在显示没有重定向url)
我不知道为什么。
感谢
解决方案
这是一种在不使用内容模板的情况下解决问题的快捷方法。
首先,将 RowDataBound
事件添加到GridView。
<asp:GridView ID="GridView1" runat="server" onrowdatabound="GridView1_RowDataBound">
</asp:GridView>
其次,使用您的逻辑添加事件处理程序的代码。 RowDataBound事件将为每一行触发,我们不必使用 foreach
。我正在使用C#,但您可以轻松将其转换为VB。
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {
if (e.Row.RowType == DataControlRowType.DataRow) {
if (e.Row.Cells[4].Text == "0") {
e.Row.Cells[4].Text = "<a href=''> <img src='img/box_icon_edit_pencil1.gif' border='0'> </a>"
} else {
e.Row.Cells[4].Text = "<a href=''> <img src='img/box_icon_edit_pencil2.gif' border='0'> </a>"
}
}
}
作为旁注,您可能想要更改
<a href=''> <img src='img/box_icon_edit_pencil1.gif' border='0'> </a>
<a href=''> <img src='img/box_icon_edit_pencil2.gif' border='0'> </a>
到
<a href="" class="Pencil1"></a>
<a href="" class="Pencil2"></a>
并使用CSS设置背景图片。
其他提示
要使GridView输出HTML,您需要做的就是在所需的边界字段上将HtmlEncode参数设置为false。
<asp:BoundField DataField="Question" HeaderText="Question" HtmlEncode="false" />
如果您使用的内,你可能想要使用因为它的目的。
你应该有一个内容的模板。
如果你需要做的格式基于的价值观,你这样做的rowdatabound事件。
我认为你得到的"意想不到的"行为,因为内可以结合范围广泛的收藏(阵列,Hashtable、数据集,等等。) 和它管理如何具体地结合的数据。
意图的内是做格式在html部分的页面...有很多的花哨的格式,你可以在那里做。
如果你打算使用gridviews,这是很好的熟悉onrowdatabound和onrowcommand事件...
快速解决:
我猜这可能需要一点时间来学习如何做它正确的方式。在过渡期间,如果你想速战速决你的问题的少量的变化:
- 使用asp:文字的控制,你希望你的图像
- 改变你的查询数据库中替代的返回值与html而不是0/1值
- 结合文字来回值和跳过你的当前格式的部分
不隶属于 StackOverflow