Paul Stearns
Paul Stearns

Reputation: 906

What happened between .NET framework 1 and 4 that caused the underscore character in postbacks to be treated differently?

We are upgrading an application written in ASP.NET using the v1 framework, to use a V4.7 framework. One of the first things that drove us crazy for a day or two is related to postbacks on datagrids.

The current code has onItemCreated for each datagrid which sets onMouseOver, onMouseOut and onClick attributes. The onClick creates a __doPostBack for each row in the table. When run the onClick would fire, but it never would execute the SelectedIndexChanged handler.

I created the default "Select" button, and that worked fine. The term button is a misnomer, as .NET generates an [a HREF...] tag.

Being a clever person I used the IE debugger to see what was passed to __DoPostBack from the Select and the onClick, and with a little tweaking, I got them to be identical. But still it would only work when I clicked the select button, but not anywhere else on the row.

An example of what I saw in the "eventTarget" variable in the javascript function __doPostBack was "dgProps$_ctl03$_ctl00" It was the same either way I called it from the href or the onClick. href worked onClick didn't.

I declared it magic, went and had a beer.

When I returned to the task of finding out how the magic was performed, I created a label to display stuff in, and added the following lines to the page_load subroutine of the page.

            If (Page.Request("__EVENTTARGET") IsNot Nothing) Then
                Label2.Text = Label2.Text + " - " & Page.Request("__EVENTTARGET")
            End If
            If (Page.Request("__EVENTARGUMENT") IsNot Nothing) Then
                Label2.Text = Label2.Text + " - " & Page.Request("__EVENTARGUMENT")
            End If

What I discovered was when the "Select" button/link was pressed the value displayed for __EVENTTARGET was "dgProps$ctl03$ctl00" When the onClick fired what was displayed was "dgProps$_ctl03$_ctl00". Something, somewhere removed the underscores from the __EVENTTARGET value.

I changed the code that built the EventTarget value for the onClicks to remove the "_" and it worked.

Obviously the code with the underscores worked fine on Windows 2003 server with .NET framework 1.

We are just starting down the path of upgrading this application and it has been my experience that Mysteries come to bite you in the rump, so;

What would cause;

a) This difference between the framework versions?

b) The difference between an [a href...] calling the __doPostBack and an onClick calling the same __doPostBack with the same value?

Upvotes: 0

Views: 87

Answers (0)

Related Questions