How to use HtmlEncode with TemplateFields, Data Binding, and a GridView

后端 未结 9 2041
借酒劲吻你
借酒劲吻你 2020-12-13 18:51

I have a GridView bound to an ObjectDataSource. I\'ve got it supporting editing as well, which works just fine. However, I\'d like to safely HtmlEncode text that is displa

相关标签:
9条回答
  • 2020-12-13 19:51

    Kindly Refer to

    http://forums.asp.net/p/1056231/1504717.aspx

    I got the working solution from here . Its working like a charm for me.

    0 讨论(0)
  • 2020-12-13 19:52

    In my case I was forced to use the "Bind" method on mi EditItemTemplate's TextBox because needed the data to be accessible in the NewValues array at the item_Updating event handling. So i figured out as follow:

    on my EditItemTemplate :

    <EditItemTemplate>
         <asp:TextBox runat="server" Text='<%# Bind("field")%>' ID="TextBox112" OnPreRender="TextBox_PreRender_decode"></asp:TextBox>                                            
    </EditItemTemplate> 
    

    then in the code behind :

    protected void TextBox_PreRender_decode(object sender, EventArgs e)
    {
        TextBox tb = (TextBox)sender;
        tb.Text = WebUtility.HtmlDecode(tb.Text);
    }
    

    This solution allowed me to show properly an html-encoded data for all of my TextBoxes and at the same time being able to access this data from the newValues array when the item_Updating event fires.

    0 讨论(0)
  • 2020-12-13 19:54

    Bind() is used for Two-Way Data Binding, for this to work you will have to use the RowUpdating event of the gridview.

    void GridView_RowUpdating(Object sender, GridViewUpdateEventArgs e)
    {
        foreach (DictionaryEntry entry in e.NewValues)
        {
            e.NewValues[entry.Key] = System.Web.HttpUtility.HtmlEncode(entry.Value.ToString());
        }
    }
    
    0 讨论(0)
提交回复
热议问题