HtmlEncode无法在asp.net中运行

时间:2012-08-16 10:55:26

标签: asp.net html gridview

我有一个问题要让HtmlEncode在ASP.Net中的GridView中正常工作。基本上我使用TemplateField来切换ItemTemplate和EditItemTemplate以在GridView本身上输入数据。我正在寻找一些关于如何在ASP.Net中对Eval和Bind方法进行编码的资源,但所有这些都不适用于我。

这是我的代码:

<div id="dvShowContent" runat="server" style="text-align: center">
    <asp:GridView ID="dgvSortKey" runat="server" AllowSorting="True" OnRowDataBound="gv_drb"
        AutoGenerateColumns="False" AllowPaging="True" BackColor="White" BorderColor="#336666"
        BorderStyle="Double" BorderWidth="3px" CellPadding="4" GridLines="Horizontal"
        Height="73px" AutoGenerateEditButton="True" OnRowEditing="dgvSortKey_RowEditing"
        OnRowUpdating="dgvSortKey_RowUpdating" OnRowCancelingEdit="dgvSortKey_RowCancelingEdit"
        OnSelectedIndexChanging="dgvSortKey_SelectedIndexChanging" OnPageIndexChanged="dgvSortKey_PageIndexChanged"
        OnPageIndexChanging="dgvSortKey_PageIndexChanging" OnRowCommand="dgvSortKey_RowCommand"
        OnRowDeleted="dgvSortKey_RowDeleted" OnRowUpdated="dgvSortKey_RowUpdated" Width="561px"
        PageSize="15" DataKeyNames="KeyCode,KeyDescription">
        <FooterStyle BackColor="White" ForeColor="#333333" />
        <RowStyle BackColor="White" ForeColor="#333333" />
        <PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" HorizontalAlign="Center" />
        <AlternatingRowStyle BackColor="LightCyan" />
        <Columns>
            <asp:TemplateField HeaderText="">
                <ItemTemplate>
                    <asp:LinkButton ID="lnkdelete" runat="server" OnClick="lnkdelete_Click">Delete</asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Instruction Key Code">
                <ItemTemplate>
                    <asp:Label ID="lblValKeyCode" runat="server" Text='<%# Server.HtmlEncode((string)Eval("KeyCode")) %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtValKeyCode" runat="server" Text='<%#Bind("KeyCode") %>' MaxLength="10"></asp:TextBox>
                    <%--<asp:TextBox ID="txtValKeyCode" runat="server" Text='<%#System.Web.HttpUtility.HtmlEncode(Eval("KeyCode").ToString()) %>' MaxLength="10"></asp:TextBox>--%>
                </EditItemTemplate>
                <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" CssClass="GvBorderGreen" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Description">
                <ItemTemplate>
                    <asp:Label ID="lblValKeyDescription" runat="server" Text='<%# Server.HtmlEncode((string)Eval("KeyDescription")) %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtValKeyDescription" runat="server" Text='<%#Bind("KeyDescription") %>'
                        MaxLength="10"></asp:TextBox>
                    <%--<asp:TextBox ID="txtValKeyDescription" runat="server" Text='<%#System.Web.HttpUtility.HtmlEncode(Eval("KeyDescription").ToString()) %>'
                        Width="300" MaxLength="20"></asp:TextBox>--%>
                </EditItemTemplate>
                <ItemStyle CssClass="GvBorderGreen" />
            </asp:TemplateField>
            <%--<asp:BoundField DataField="KeyCode" HeaderText="Instruction Key Code" HtmlEncode="true" />
            <asp:BoundField DataField="KeyDescription" HeaderText="Description" HtmlEncode="true" />--%>
        </Columns>
    </asp:GridView>
</div>

请帮我解决这个问题。

1 个答案:

答案 0 :(得分:2)

正如Waqar建议答案在他提供的链接中 - 为了使这更清楚,你需要改变这一行:

<asp:TextBox ID="txtValKeyCode" runat="server" Text='<%#Bind("KeyCode") %>' MaxLength="10"></asp:TextBox>

对此:

<asp:TextBox ID="txtValKeyCode" runat="server" Text='<%#System.Web.HttpUtility.HtmlEncode((string)Eval("KeyCode")) %>' MaxLength="10"></asp:TextBox>