Peter's Gekko

Sponsors

The Lounge

News

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
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


Posted 12-06-2004 2:40 PM by pvanooijen
Filed under:

[Advertisement]

Comments

Robert wrote re: How to find out which control was responsible for the postback of an ASP.NET web form
on 12-15-2004 1:36 PM
Hi Peter,

I found this article extremly helpful, thanks for posting it!
TrackBack wrote Sorting in a datagrid without using the viewstate
on 03-01-2005 3:57 AM
Andrew Werber wrote re: How to find out which control was responsible for the postback of an ASP.NET web form
on 05-26-2005 4:55 PM
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
Peter's Gekko wrote ASP.NET Control ID and postback
on 05-31-2005 2:23 AM
Controls on your webforms all have an ID, like TextBox1, LinkButtonAdd, etc. This ID is not the internal...
Yitzhak Gootvilig's Blog wrote How to find out which control was responsible for the postback
on 08-31-2005 9:52 AM
Yitzhak Gootvilig's Blog wrote How to find out which control was responsible for the postback
on 01-23-2006 8:20 AM
Osama Mirza wrote re: How to find out which control was responsible for the postback of an ASP.NET web form
on 11-02-2006 11:36 AM

This article really solved my problem

Tarik wrote re: How to find out which control was responsible for the postback of an ASP.NET web form
on 01-26-2007 9:48 AM

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

bizz wrote re: How to find out which control was responsible for the postback of an ASP.NET web form
on 06-25-2008 8:55 AM

thanks

Saravana wrote re: How to find out which control was responsible for the postback of an ASP.NET web form
on 08-29-2008 3:37 AM

Thanks it helped me a lot :-)

Add a Comment

(required)  
(optional)
(required)  
Remember Me?