我有一个 gridview BoundField 需要显示字符串中存在的值 - 无需应用 HTML 格式并在其间保留多个空格(就像文本框的行为方式一样)
我尝试了以下方法,但没有奏效。我们怎样才能让它像文本框一样工作?
标记
<form id="form1" runat="server">
<asp:TextBox ID="Textbox1" runat="server" EnableViewState="false"></asp:TextBox>
<asp:TextBox ID="Textbox2" runat="server" EnableViewState="false"></asp:TextBox>
<asp:TextBox ID="Textbox3" runat="server" EnableViewState="false"></asp:TextBox>
<asp:TextBox ID="Textbox4" runat="server" EnableViewState="false"></asp:TextBox>
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Name" HeaderText="B" DataFormatString="<pre>{0}</pre>"
HtmlEncode="true" ItemStyle-BackColor="Gray" />
<asp:BoundField DataField="Name" HeaderText="D" HtmlEncode="true" ItemStyle-BackColor="Cyan" />
<asp:BoundField DataField="Name" HeaderText="E" ItemStyle-BackColor="Orange" />
<asp:BoundField DataField="Name" HeaderText="C" HtmlEncode="false" ItemStyle-BackColor="Teal" />
<asp:BoundField DataField="Name" HeaderText="A" DataFormatString="<pre>{0}</pre>"
HtmlEncode="false" ItemStyle-BackColor="Yellow" />
</Columns>
</asp:GridView>
</form>
C# 代码
protected void Page_Load(object sender, EventArgs e)
{
string selectedVal1 = "a b";
string selectedVal2 = "<br />";
string selectedVal3 = "<script 1>";
string selectedVal4 = "<script>";
this.Textbox1.Text = selectedVal1;
this.Textbox2.Text = selectedVal2;
this.Textbox3.Text = selectedVal3;
this.Textbox4.Text = selectedVal4;
DataTable table = new DataTable();
table.Columns.Add("Name", typeof(string));
table.Rows.Add(selectedVal1);
table.Rows.Add(selectedVal2);
table.Rows.Add(selectedVal3);
table.Rows.Add(selectedVal4);
GridView2.DataSource = table;
GridView2.DataBind();
}
HTML
<input name="Textbox1" type="text" value="a b" id="Textbox1" />
<input name="Textbox2" type="text" value="<br />" id="Textbox2" />
<input name="Textbox3" type="text" value="<script 1>" id="Textbox3" />
<input name="Textbox4" type="text" value="<script>" id="Textbox4" />
<div>
<table cellspacing="0" rules="all" border="1" id="GridView2" style="border-collapse:collapse;">
<tr>
<th scope="col">B</th><th scope="col">D</th><th scope="col">E</th><th scope="col">C</th><th scope="col">A</th>
</tr><tr>
<td style="background-color:Gray;"><pre>a b</pre></td><td style="background-color:Cyan;">a b</td><td style="background-color:Orange;">a b</td><td style="background-color:Teal;">a b</td><td style="background-color:Yellow;"><pre>a b</pre></td>
</tr><tr>
<td style="background-color:Gray;"><pre><br /></pre></td><td style="background-color:Cyan;"><br /></td><td style="background-color:Orange;"><br /></td><td style="background-color:Teal;"><br /></td><td style="background-color:Yellow;"><pre><br /></pre></td>
</tr><tr>
<td style="background-color:Gray;"><pre><script 1></pre></td><td style="background-color:Cyan;"><script 1></td><td style="background-color:Orange;"><script 1></td><td style="background-color:Teal;"><script 1></td><td style="background-color:Yellow;"><pre><script 1></pre></td>
</tr><tr>
<td style="background-color:Gray;"><pre><script></pre></td><td style="background-color:Cyan;"><script></td><td style="background-color:Orange;"><script></td><td style="background-color:Teal;"><script></td><td style="background-color:Yellow;"><pre><script></pre></td>
</tr>
</table>
</div>