Thursday, 12 June 2008

Learning Log4: __doPostBack EVENTARGUMENT

This post is primarily for my own skills development.

However, if you've found this and it isn't very clear what i mean, feel free to say so and i'll explain it a little better.

I have determined the format for the EVENTARGUMENT parameter to __doPostBack.

Say you have a situation whereby you need to make programmatically force a click event on an Server-Side control.

For example, you have a gridview, within each row is a LinkButton which performs some action to the data in that row eg Edit/Update/Delete/StateChange etc.

When the user clicks on a LinkButton, you need to execute some custom javascript, maybe even open an Are You Sure? popup and then if the user says Yes, you want the LinkButton to postback to the server and have the code in GridView_RowCommand execute.

You can write the LinkButton.OnClientClick event in the code behind like this:

lnkButton.OnClientClick = "if(confirm('Are you sure?'))javascript:__doPostBack('" + lnkButton.UniqueID + "','CommandName$CommandArgument');return false;";

Things to note

  • The first argument to __doPostBack is the unique id of the control causing the post back. Note, when the LinkButton is embedded in a row of a gridview, the gridview is the control that causes the postback

  • CommandName and CommandArgument is just as you would define it in the LinkButton declaration within the ASPX

No comments:

Post a Comment