views:

10579

answers:

7

Hi there,

Can anyone help, I have a asp.net button but recently replaced it with a standard html button ... What i need to do is postback to an asp.net page and ensure a method is called

the button before was an asp.net button so i had this event

Protected Sub btnCancelar_Click(ByVal sender As Object, ByVal e As System.EventArgs) 
    UtilTMP.DisposeObjects()
    Server.Transfer("~\Forms\test.aspx", True)
End

But i was using a button with javascript ALERT and recently changed to Jquery UI Model dialog but it doesn't wait for me to answer the question.. the postback happenes immediatly ... so i decided to change to a standard HTML button ... but i need to postback to the asp.net page and call a method like.

If i just postback it won't call the cleanup

Protected Sub Cleanup()
    UtilTMP.DisposeObjects()
    Server.Transfer("~\Forms\test.aspx", True)
End
+2  A: 

You need to set the __EVENTTARGET hidden field to an appropriate value if you want to trigger the event handler on postback. I would do it a different way, however. Put ASP buttons in your modal dialog that have the event handler associated with them. Have the click handler that pops up the dialog return false (so that the postback doesn't happen from that button click). This way your form is posted back from an ASP button and the handler, including the client-side hidden field setting, is invoked automatically.

tvanfosson
A: 

You should be able to stop the postback with whatever JS is attached to the button onclick event, regardless of whether it is a WebControl or a standar HTML button, by returning false.

E.g. <input type="image" id="test" onclick="doWhatever();return false;" />

Andrew Corkery
A: 

Hey thanks for the quick reponse...

So in my jquery ui model i have this - its an example and has the "OK" button.. so when they click ok its postbacks.. - i will have a cancel that just closes the ui model dialog..

You say put the buttons in my model form, can you elaborate?

here is the jquery extract

buttons: { Ok: function() { $(this).dialog('close'); // NOW DO POSTBACK ENSURING MY METHOD IS CALLED }

So do i still need to set ___EVENTTARGET?

Thanks once again

mark smith
+6  A: 

See if this helps: http://www.codeproject.com/KB/aspnet/sample.aspx.

Basically, you declare a dummy anchor tag:

<a id="anchorId" runat="server" onclick="return true" onserverclick="foo"></a>

In your code behind, you need to declare a foo method:

protected void foo(object sender, EventArgs e)
{
    // Do something here
}

Then you can invoke this anchor's onclick function with this javascript:

document.getElementById('anchorId').click()

David
Or this is another option:http://blogs.microsoft.co.il/blogs/gilf/archive/2008/10/04/asp-net-ajax-pagemethods.aspx
David
+2  A: 

While you can do Postbacks with JQuery, it might be better to call a web method (web service that is in your page). The call could also be faster because you are not posting the entire page ViewState.

I would follow what David Ward spells out here:

http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/

Chris Brandsma
This will work, but there is a way to do this with postbacks, so you don't have to declare a web service method.
David Basarab
+1  A: 

Check out this article

http://www.deviantpoint.com/post/2009/03/12/Using-jQuery-UI-Dialogs-for-confirmation-windows.aspx

The basic idea is you place the call back function in a hidden field and run an eval on the $(this).dialog('close');

I used this to have it work in a Gridview, so if you want to know how to do that leave a comment.

David Basarab
A: 

I like call method, but d´not postback.

marcelo