Sunday, April 26, 2009

Calling __dopostback function for Asp.net controls which Don't have AutoPostBack property

While working on the last two post which include the __dopostback function of the JavaScript and Jquery alert Dialog I realize that the button control of the asp.net didn't have the autopostback property which other asp.net control has. Other controls of the asp.net such as check box, DropDownList etc has autopostback property.The autopostback property is Boolean which which is set as false by default. So in case of controls which has the autopostback property and which is set to true by the developer, the asp.net will generate the __dopostback JavaScript function and also two hidden fields which are __EVENTTARGET and __EVENTARGUMENT. But in case of the asp.net button control as it does not have the autopostback property so __dopostback function and two hidden field are not generated.
Here is the code you have to write if your control on the page don't have the autopostback property to true.
In order to __dopostback function to call manually you have to set the autopostback property of the control to true. But if the auto autopostback property is not set to true then __dopostback function will not generated by asp.net. In case of Button control which don't have the autopostback property and you don't have other control on the page which has the autopostback property and is not set to true , you have to add the __dopostback function and also the hidden fields for your own in order to call the __dopostback function manually. Here is the simple HTML code with JavaScript in it, which is used to call the __dopostback function manually. Here in the code I have added the two hidden fields called __EVENTTARGET and __EVENTARGUMENT, which are used to store the event target name , ID of the control which cause the postback and the event argument , argument which you want to pass for the postback.

< html>
< body>
< form name="myForm" method="post" action="test.aspx" id="myForm">
<input type="hidden" name="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" value="" />
< input type="hidden" name="__VIEWSTATE" value="dDwtMTAwOTA0ODU1NTs7PsllCK1DzvZsK0J6OQ2dxKqmEwVs" / >
< script language="javascript">
function __doPostBack(eventTarget, eventArgument) {
var theform = document.myForm;
theform.__EVENTTARGET.value = eventTarget;
theform.__EVENTARGUMENT.value = eventArgument;
theform.submit();
}
< /script>
< /form>
< /body>
< /html>

After the insertion of the the hidden field you must define the __dopostback function in your JavaScript section of your page. In the above code we have a __dopostback function which take the event target and event argument as parameters. In the first statement I have get the form by using the statement document.myForm, where myForm is the id of the my form which is included in my asp.net page. In the next statements I have assign event target and event argument the values which are passed to the function as parameter and at the I have called the submit function of the myForm object. Which will cause the postback to the form.
Here is the code behind statement which will attach onclick attribute to the button control. In this case cmdPostBack is the name of the button.

cmdPostBack.Attributes.Add("onclick", "CallPostBack(); return false;");

In the above code i have write return false statement in the onclick attribute of the button control, As I don't want to post back when user click the button, the page will be post back only when user click OK in the confirm dialog of the JavaScript , which is written in the CallPostBack JavaScript function. Here is the CallPostBack function definition.

function CallPostBack()
{
var returnValue= confirm('Are you sure you want to postback ?');
if(returnValue)
__doPostBack('cmdPostBack','');
}

In the above code I have passed the ID of the my Button control which is cmdPostBack which will then execute the event handler in the code behind file.
From this article hope you get understanding of how you can manually write code for the __dopostabck function. You can download the source code from here

All and any comments / bugs / suggestions are welcomed!


1 comment:

HeBecameaMan said...

really appreciated this article as it helped me with something I was working on. Thanks very much.