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
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.
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.
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());
}
}