Thursday, 6 March 2008

Issues when Overriding the Render() method of a asp:Textbox

The framework at my current client has had a bug which has been sitting around and bugging our developers for months which no one has been able (or willing) to fix. As a tech lead, I felt that it was my responsibility to nip the issue once and for all.

The issue is that whenever we bound to textboxes in Multiline mode, the data would save correctly to the database but not render correctly. This effectively meant that our app would clear out all data in multiline textboxes if you save it twice. Now where I come from, Data loss is a basic no-no :o)

If you look at the code below, it all looks perfectly innocent. You render the begin tag, the contents and then the end tag. A few of the guys had a look at this and didn't know why the data was being erased and just avoided using the custom control - using the asp:TextBox instead :

protected override void Render(HtmlTextWriter writer)
// Assumes validator control has been added as a subcontrol

Looks fine? No. If you look at the base class in reflector (TextBox.Render(HtmlTextWriter)), you will see what is wrong with the code above:

protected internal override void Render(HtmlTextWriter writer)
if (this.TextMode == TextBoxMode.MultiLine)
HttpUtility.HtmlEncode(this.Text, writer);

So the HTML textareas being rendered by the extended control were blank as the control was omitting an essential step in the render method. I just entered the multiline condition into the inherited control - and everything worked fine.

1 comment:

Anonymous said...

голая школьница мастурбирует онлайн онлаин школьницы порно онлайн молодежное [url=][/url]