Monday, September 08, 2003 - Posts

GotDotNet Unofficial Feed.

Can someone please take the files from the link below and then host it in your server for the mainfeed. I am unable to find any good hosting site and frankly this is dissapointing .

Here are the files.

Catching the Postback cause.

This is one of the most common questions that i have come across and many people i know have been stuck with this problem particularly. Trying to figure out this, i stumbled upon a very interesting discovery some time back.
 
There is a protected method in the Page which returns a NameValueCollection which contains all the information posted back from the client. Now it looks like a norm that always the first row in the namevalue collection is the viewstate information stored, which has the key “__VIEWSTATE“. The value for this is an arraylist which contains the client side viewstate information as such, in the encrypted format.
 
Now coming to the important part, the event source, the control which has raised this postback event is also contained in the namevalue collection. Now the next index variable in the namevalue collection will give the Name of the control which raised the event and the value in the collection corresponding for this key contains the value of the control !
 
Ex : Say FormTest.aspx has two buttons with
 
                         1            2
 id's :       MyBtn1   MyBtn2
 values       Yes        No
Now say, the user clicks on Button2 which initiates the postback, then in Page_Load event

private void Page_Load(object sender, System.EventArgs e){

if(this.IsPostBack){

System.Collections.Specialized.NameValueCollection coln = this.DeterminePostBackMode();

string ViewState = coln["__ViewState"] ;

string source = coln.Keys[1] ;

}

}

 
Note: Now if your actual control has been inside a usercontrol and if it has triggered the event, then the source value that you will get is the client name and not the client id.
 
Ex :   Client ID : “UserCnt_myChildCnt1“      Client Name : “UserCnt:myChildCnt1“
 
This is all that is required to capture the event source before the event for the control gets triggered :)

Link Interface 11.

Here is today's ration of good links .