C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Post back gets demystified (__doPostBack defined)


Posted Date: 20 May 2008    Resource Type: Articles    Category: Web Applications
Author: Rahul SharmaMember Level: Silver    
Rating: Points: 50



It is amazing to note that only two of the ASP.NET web server controls cause a postback. All the other controls use the JavaScript __doPostBack function to trigger the postback.
__doPostBack function can be defined in javascript as below:
function __doPostBack(eventTarget, eventArgument)
{
if (!theForm.onsubmit || (theForm.onsubmit() != false))
{
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
The __doPostBack function takes two arguments, eventTarget and eventArgument. The eventTarget contains the ID of the control that causes the postback and the eventArgument contains any additional data associated with the control. Note that the two hidden fields, “__EVENTTARGET” and “__EVENTARGUMENT,” are automatically declared. The value of the eventTarget and eventArgument are stored in the hidden fields. The two hidden variables can be accessed from the code behind using the forms/params collection.
How to find controls which caused postback???
Using the hidden variables you can also find the ID of the control which causes the postback. All you need to do is to retrieve the value of the __EVENTTARGET from the form parameter collection.
string controlName = Request.Params.Get("__EVENTTARGET");

What about passing arguments???
Have a look at the __doPostBack function you will notice that the second argument is called the eventArgument. With this you can allow controls to pass arguments to the __doPostBack function.
<input type="button" id="Button2" value="Button 2 Click" onclick="DoPostBack()" />
<script language="javascript" type="text/javascript">

function DoPostBack()
{
__doPostBack('Button2','My Argument');
}

</script>

The event target can be retrieved with:
string passedArgument = Request.Params.Get("__EVENTARGUMENT");

The “Button2” when clicked fires the DoPostBack function which in turn calls the __doPostBack. The __doPostBack function contains two arguments, eventTarget and eventArgument. The eventTarget is “Button2” and the eventArgument is “My Argument.”

Mind Tickler –
Do you know the two controls that do not use __dopostback() are Button (submit type) and image button. They directly gets fired.






Responses

Author: Mahesh Raj    07 Jun 2008Member Level: Gold   Points : 1
This is very good information,Continue posting such useful articles.


Author: John Fernandez    08 Jun 2008Member Level: Gold   Points : 1
Very well written Article.Thanks for sharing this information.


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Trap the CheckedChanged event of checkbox control in ASP.NET datagrid.
Previous Resource: New features in ADO.NET 2.0
Return to Discussion Resource Index
Post New Resource
Category: Web Applications


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use