CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Peter's Gekko

public Blog MyNotepad : Imho { }

How to find out which control was responsible for the postback of an ASP.NET web form

This sounds and is so trivial that it is well hidden in the docs.

On my form is a datalist with in each row a label and a radiobuttonlist. On the radiobuttonlist autopostback is set to true, selecting another radiobutton will lead to a roundtrip. The items of a datalist are templates so there are no codebehind eventhandler for the controls. But my code needs to know which row of the datalist triggered the postback. This is a part of the HTML rendered:

<span id="DataList1__ctl11_Label1" style="height:1px;width:336px;">Wijzigingen in het suiker en specialiteiten gebouw, hygiënische ruimten of installaties voor opslag van produktspan>
<span id="DataList1__ctl11_RadioButtonListAntwoord" style="height:5px;width:142px;"><input id="DataList1__ctl11_RadioButtonListAntwoord_0" type="radio" name="DataList1:_ctl11:RadioButtonListAntwoord" value="1" onclick="__doPostBack('DataList1__ctl11_RadioButtonListAntwoord_0','')" language="javascript" />

<label for="DataList1__ctl11_RadioButtonListAntwoord_0">Jalabel><input id="DataList1__ctl11_RadioButtonListAntwoord_1" type="radio" name="DataList1:_ctl11:RadioButtonListAntwoord" value="2" onclick="__doPostBack('DataList1__ctl11_RadioButtonListAntwoord_1','')" language="javascript" />

<label for="DataList1__ctl11_RadioButtonListAntwoord_1">Neelabel><input id="DataList1__ctl11_RadioButtonListAntwoord_2" type="radio" name="DataList1:_ctl11:RadioButtonListAntwoord" value="3" onclick="__doPostBack('DataList1__ctl11_RadioButtonListAntwoord_2','')" language="javascript" /><label for="DataList1__ctl11_RadioButtonListAntwoord_2">?label>span>
      

Every radiobutton has a snippet of script which takes the control's ID as a parameter. The script function invoked is also in the HTML, it is the __doPostBack function found in every asp.net webform

function __doPostBack(eventTarget, eventArgument) {
		var theform;
		if (window.navigator.appName.toLowerCase().indexOf("netscape") > -1) {
			theform = document.forms["ProjektWegwijzer"];
		}
		else {
			theform = document.ProjektWegwijzer;
		}
		theform.__EVENTTARGET.value = eventTarget.split("$").join(":");
		theform.__EVENTARGUMENT.value = eventArgument;
		theform.submit();
	}

It copies the (cleaned) name of the control into a form member. The contents of this form member can be read in the code behind.

char[] splitters = {'_'};
string[] pbCtrl = Request.Form["__EVENTTARGET"].Split(splitters);
// Index of control ID's is 1-based !
int lIndex = int.Parse(pbCtrl[2].Substring(3)) - 1;
int aw = int.Parse(pbCtrl[4]);

The ID of all the radibuttons have a format like DataList1__ctlxx_RadioButtonListAntwoord_y. In it xx will return the datalist row which triggered the callback and yy the index of the radiobutton.

Peter


Published Dec 06 2004, 02:40 PM by pvanooijen
Filed under:

Comments

TrackBack said:

# March 1, 2005 3:57 AM

Andrew Werber said:

I tried this and it will work for a drop down, but this does not work for a button. Is there a way to determine what button caused a postback?

I have an app where I'm loading a user control based on a value stored in a button on the form. Let's say the user controls are type A and B. If A is on the form and the user clicks the button, B should be loaded. While I can do this now in the button click event, I cannot load the control in the page_load event during the postback. That's bad because the user control can cause a postback. So when a button is clicked on the user control, it's not reloaded, so it disappears and it's event does not get executed.

Thanks for any help.

Andy
# May 26, 2005 4:55 PM

Peter's Gekko said:

Controls on your webforms all have an ID, like TextBox1, LinkButtonAdd, etc. This ID is not the internal...
# May 31, 2005 2:23 AM

Yitzhak Gootvilig's Blog said:

# August 31, 2005 9:52 AM

Yitzhak Gootvilig's Blog said:

# January 23, 2006 8:20 AM

Osama Mirza said:

This article really solved my problem

# November 2, 2006 11:36 AM

Tarik said:

I guess this might be late but anyways as an answer to Andrew Werber i'll post this. In order to make this mechanism work for buttons "UseSubmitBehaviour" property should be set to false

regards

# January 26, 2007 9:48 AM

bizz said:

thanks

# June 25, 2008 8:55 AM

Saravana said:

Thanks it helped me a lot :-)

# August 29, 2008 3:37 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!

Our Sponsors

This Blog

Syndication

News