Geeks With Blogs
Server Error in Application An unhandled exception occurred during the execution of the current web request

Hi,

      Few day back i need post back control name in the Page_Load event. I did lots of search on net. I found few intersting fact about that and i am sharing that.

There are two types of controls which make post back in ASP.NET. One button type control like image button, button (whose type is “submit”), and another type use javascript function “_doPostBack” for the post back.

If post back control is button type then it will be added in the Request.Form collection means if there are two button in a page named button1 and button2 and if button1 make post back then only button1 will be in Request.Form Collection not button2 (but Request.Form collection can contains other server controls also like if page contains few textbox, dropdown list etc.) and if post back made by the button2 then only button2 will be available in Request.Form collection not button1(with other server control as I discuss earlier).

So if you want to catch which button type control made a post back in page load event, you have to just iterate the Request.Form collection.

 I’ll show you demo latter in this article.

Another category of server control who make post back use the client side javascript function _doPostBack like if we made autopostback true for dropdown list, radio button etc. 

 

If you look view source you will found _doPostBack javascript function.

 

function __doPostBack(eventTarget, eventArgument)

 {

if  (!theForm.onsubmit || (theForm.onsubmit() ! =   false ))

{

theForm.__EVENTTARGET.value  =  eventTarget;

theForm.__EVENTARGUMENT.value  =  eventArgument; theForm.submit();

 }

 }

 

As you can see this function take two arguments “eventTarget” and “eventArgument”. “eventTarget” used for the control ID who is responsible for the postback and “eventArgument” used for the additional information about the control.

 

If you look at the view source you will also found these two hidden fields.

 

<input type= "hidden"  name= "__EVENTTARGET"  id= "__EVENTTARGET"  value= ""  />

<input type= "hidden"  name= "__EVENTARGUMENT"  id= "__EVENTARGUMENT"  value= ""  /> 

 

Lets add one dropdown server control in page and make autopostback true also add some dummy data. If you look at the view source you will found

 

<select name="DropDownList1" onchange="javascript:setTimeout('__doPostBack(\'DropDownList1\',\'\')', 0)" id="DropDownList1">

            <option value="1">abc</option>

            <option value="2">xyz</option>

 

</select>

 

ASP.NET engine automatically add onchange event and call the _doPostBack function and pass the appropriate parameter.

 

_doPostBack function first set the value of those hidden field and then submit the form. So if you want to know whether this dropdown list make post back or not you have to just check the value of “__EVENTTARGET” hidden field from the form parameter collection. I’ll show you code as well.

 

In my example I am adding two buttons, one image button, one dropdown list, one checkbox and two image buttons. I’ll try to print the control name which makes the post back. All I’ll try to find in page load event. So be ready for the ride.

 

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        if (IsPostBack)

            Response.Write(getPostBackControlName());

    }

 

   

    private string getPostBackControlName()

    {

        Control control = null;

        //first we will check the "__EVENTTARGET" because if post back made by       the controls

        //which used "_doPostBack" function also available in Request.Form collection.

        string ctrlname = Page.Request.Params["__EVENTTARGET"];

        if (ctrlname != null && ctrlname != String.Empty)

        {

            control = Page.FindControl(ctrlname);

        }

        // if __EVENTTARGET is null, the control is a button type and we need to

        // iterate over the form collection to find it

        else

        {

            string ctrlStr = String.Empty;

            Control c = null;

            foreach (string ctl in Page.Request.Form)

            {

                //handle ImageButton they having an additional "quasi-property" in their Id which identifies

                //mouse x and y coordinates

                if (ctl.EndsWith(".x") || ctl.EndsWith(".y"))

                {

                    ctrlStr = ctl.Substring(0, ctl.Length - 2);

                    c = Page.FindControl(ctrlStr);

                }

                else

                {

                    c = Page.FindControl(ctl);

                }

                if (c is System.Web.UI.WebControls.Button ||

                         c is System.Web.UI.WebControls.ImageButton)

                {

                    control = c;

                    break;

                }

            }

        }

        return control.ID;

 

    }

}

 

I Hope that will help you out in some situation. If you have any issue you can post in this blog.

Thanks

Related Post:

Association, Aggregation, Composition object relationship

Talent vs Attitude – Harsha Bhogle

What we can learn from Babies

 

 

Posted on Tuesday, June 27, 2006 9:48 AM ASP.NET , C# , Web Services , Design Pattern , BizTalk , unhandled exception | Back to top


Comments on this post: Get control name in Page_Load event which make the post back

# re: Get control name in Page_Load event which make the post back
Requesting Gravatar...
Excellent!!.

Thanks for posting this good article.

Regards,
Ramana
Left by Ramana on Jan 26, 2007 3:37 PM

# re: Get control name in Page_Load event which make the post back
Requesting Gravatar...
hi..ramana
actually I was struggling for this from two days, thanq very much for the article. it is very useful. thanx once again
bye
natraj
Left by K NATRAJ on Feb 14, 2007 5:48 AM

# re: Get control name in Page_Load event which make the post back
Requesting Gravatar...
Great idea!

It has really saved lots of time.
This is very useful in Handheld devices where javascript popups (e.g. alert, confirm, etc) are used.
Left by Vipul on Apr 13, 2007 1:56 PM

# re: Get control name in Page_Load event which make the post back
Requesting Gravatar...
Dude..this is exactly what I needed. Thanks!
Left by JJ on Apr 25, 2007 8:05 PM

# re: Get control name in Page_Load event which make the post back
Requesting Gravatar...
Thanks a bunch for this one dude! Spent a few hours without figuring this out myself!
Left by Fredrik on May 09, 2007 11:30 AM

# re: Get control name in Page_Load event which make the post back
Requesting Gravatar...
Perfect!! Exactly what I needed.
Left by Matt on May 14, 2007 6:57 PM

# re: Get control name in Page_Load event which make the post back
Requesting Gravatar...
I knew this must be possible to capture the id of control which caused the post back, thanks for this article, it saved lots of time.
Left by Sumit Soni on May 22, 2007 4:51 AM

# re: Get control name in Page_Load event which make the post back
Requesting Gravatar...
I knew this must be possible to capture the id of control which caused the post back, thanks for this article, it saved lots of time.

Regards,
Sumit Soni
Left by Sumit Soni on May 22, 2007 4:52 AM

# re: Get control name in Page_Load event which make the post back
Requesting Gravatar...
Nice work. Thank you!
Left by Jeff Ostrander on Jun 05, 2007 2:56 PM

# re: Get control name in Page_Load event which make the post back
Requesting Gravatar...
Thanks for sharing...
Regards
Abdalla
Left by Abdalla on Sep 28, 2007 11:28 AM

# re: Get control name in Page_Load event which make the post back
Requesting Gravatar...
Gud article. Thanks for posting this, really helped me.
Left by Linz Mathew on Feb 27, 2008 10:52 PM

# re: Get control name in Page_Load event which make the post back
Requesting Gravatar...
very good article. been looking around for this. thanks a bunch
Left by Ak on Mar 03, 2008 11:54 PM

# re: Get control name in Page_Load event which make the post back
Requesting Gravatar...
Thanks Mahesh, it helped me and my team to move forward from a very tricky situation.
Left by Ashish on Mar 08, 2008 1:35 PM

# re: Get control name in Page_Load event which make the post back
Requesting Gravatar...
Great article, I didn't tested the code though, but it looks great!
Left by Muhammad Usman Arshad on Mar 16, 2008 5:09 PM

# re: Get control name in Page_Load event which make the post back
Requesting Gravatar...


Hi Mahesh,

Excellent Reserch and Article, Bravo,

And you put Information straight forward is appriciable,

Thanks, Thanks a lot.

Vishal Parekh
Left by vishal parekh on Aug 18, 2008 8:49 PM

Your comment:
 (will show your gravatar)


Copyright © Mahesh Singh | Powered by: GeeksWithBlogs.net