Tuesday 27 April 2010

asp.net button not posting back

I have a modalpopup extender attached to a panel, inside which resides two asp.net buttons. Each of these had the normal "Onclick" attribute assigned to them. However, when they were clicked, no postback was occuring.

From some reading around, it appears that this is by design, and that the buttons are there for this to be closed.

So how do you get round this?

You can attach a javscript client side event that mimmicks the behaviour of what happens with asp anyway, by forcing the postback to take place.

Firstly, in the head of your page, define a function that ensures that the page is valid first, and then causes the postback to take place (the validation part is crucial for this to work.

function doPostback(sender,e)
{
if(Page_IsValid)
{
__doPostback(sender,e);
}
else
{
return false;
}

}

Now in your code behind, tell your buttons to call this:

btnSaveUserDetails.OnClientClick = "javascript:return doPostback('" + btnSaveUserDetails.UniqueID + "');";

It is important here that the uniqueID is used, as this ensures that any controls that are nested pass the right details to the postback function.

And ta da, they now work!

No comments:

Post a Comment