Which Control Caused PostBack!

What is a Postback?

Working with web server controls, each action taken will cause the page to send HTTP POST to itself.

How to know which control caused the postback?

There are two hidden fields which will hold the control id and any extra argument. Once for example, you clicked on a button, a javascript event handler will handle this event. The __doPostBack() function will be fired which will set the control id in __EVENTTARGET hidden field and the arguments in __EVENTARGUMENT and finally it submits the form to the server.

So to know which control caused postback in code behind, you can use

string controlID = Request.Params["__EVENTTARGET"];

if controlID returned "" ( in .NET framework 1.1) you should consider trying


if(Request["BUTTONID"] != null)

{
 // button is clicked

}
else
{
 // Button is not clicked
}

Where BUTTONID Is the id of the button you are trying to figure if it was clicked or not

Hope this helps,

HC

Published 18 May 2007 05:05 AM by haissam

Comments

# JoshStodola said on 18 May, 2007 11:03 AM

You are missing to double quotes there, my friend:

string controlID = Request.Params["__EVENTTARGET"];

Regards,

Josh

# haissam said on 18 May, 2007 05:10 PM

Thank you for noticing Josh :)

Best Regards,

HC

# vishal sharma said on 02 June, 2007 02:56 PM

hi

but same code is not working here.

it is writing the junk value of viewstate but doesnot returning anything for __EVENTTARGET & __EVENTARGUMENT.

thanks

vishal

# shankar said on 18 November, 2009 04:51 AM

my mail id is shankar.m@skelta.com

Am creating control and adding those controls in pageload(). one of those controls is <asp:image button..> which is rendered as <input type:image...>  in browser. (that image control is placed inside an update panel(ajax) in my page)

if the image is clicked it is posted back to the server, in server-side coding how will i identify the id,imageulr/src of the control which caused the post-back.

The code you mentioned will not work because am creating controls only in the page load. so the control is not available in the server side(ID is not available)

Please help me...

thanks in advance,

shan.

This site

Search

Go

This Blog

Syndication

Sponsors

  • MaximumASP
  • Social Bookmarking
    Online Shopping
    asp.net hosting
    UK online local dating