Friday, September 5, 2008

Determining the Control that Caused a PostBack - In AJAX


I have posted - Determining the Control that Caused a PostBack a long back.

However, In ASP.NET, there's no property anywhere that can tell you which control caused the postback. The ID of the posting control, however, is not difficult to find.

The answer to this all jargon is – One Client side Hidden field named __EVENTTARGET and one JS fucntion named – __doPostBack()

But thanks to AJAX - In ASP.NET AJAX, when partial rendering is used, things are easier, as the ScriptManager control exposes a custom property—the AsyncPostBackSourceElementID property.

The following code shows how to determine whether the user clicked a menu item or posted back to the server through another control:

bool IsPostingFromMenu()


ScriptManager sm = ScriptManager.GetCurrent(this);

string ctlID = sm.AsyncPostBackSourceElementID;

Control c = this.FindControl(ctlID);

if (c == null)

return false;

return (c.ID == "Menu1");


Hope this help.

Thanks & Regards,

Arun Manglick Senior Tech Lead

No comments:

Post a Comment