Run Server-side Function *immediately after* executing client-side JavaScript.

G

Guadala Harry

Suppose I have a hyperlink that, when clicked, executes a JavaScript
function on the client. Separately I have a button that, when clicked,
causes a post back and executes a server-side function, as in the following:

<a id="HyperLink5" href="javascript:DoSomethingInClient()">Do Something</a>

<asp:Button id="btnSaveChanges" EnableViewState="true"
CommandName="SaveChanges" Visible="true" Width="115" runat="server"
Text="Save Changes"></asp:Button>

What I want is to have it so that when the user clicks on the hyperlink, the
JavaScript function executes and then the PostBack gets initiated
immediately after the JavaScript function does its thing - as if the user
had clicked on the Save Changes button (but without requiring the user to
click on the button). That is, I want the JavaScript to make its client-side
changes and then cause the server-side "Save Changes" function to execute.
Any ideas?

Thanks!
 
J

John Saunders

Guadala Harry said:
Suppose I have a hyperlink that, when clicked, executes a JavaScript
function on the client. Separately I have a button that, when clicked,
causes a post back and executes a server-side function, as in the following:

<a id="HyperLink5" href="javascript:DoSomethingInClient()">Do
Something said:
<asp:Button id="btnSaveChanges" EnableViewState="true"
CommandName="SaveChanges" Visible="true" Width="115" runat="server"
Text="Save Changes"></asp:Button>

What I want is to have it so that when the user clicks on the hyperlink, the
JavaScript function executes and then the PostBack gets initiated
immediately after the JavaScript function does its thing - as if the user
had clicked on the Save Changes button (but without requiring the user to
click on the button). That is, I want the JavaScript to make its client-side
changes and then cause the server-side "Save Changes" function to execute.
Any ideas?

Use btnSaveChanges.click() in DoSomethingInClient.
 
G

Guadala Harry

Sorry if I'm a bit dense on this, but here is the syntax I used and it
doesn't trigger the server-side code:

function DoSomethingInClient() {
Do Some Stuff Here
btnSaveChanges.click();
}

How can I get the server-side code (btnSaveChanges.click()) to execute?

Thanks.
 
J

John Saunders

Guadala Harry said:
Sorry if I'm a bit dense on this, but here is the syntax I used and it
doesn't trigger the server-side code:

function DoSomethingInClient() {
Do Some Stuff Here
btnSaveChanges.click();
}

How can I get the server-side code (btnSaveChanges.click()) to execute?

I'm surprised that didn't work.

In server code, you can call GetPostBackClientEvent(btnSaveChanges) to get a
string containing a JavaScript function which can be called to do a
postback. I'll research this a bit and get you back some code.
 
G

Guest

Hi
You can call Server Side Script from Client Script Execution.
function DoSomethingInClient()
{
Do Some Stuff Here
__doPostBack('btnSaveClick','');
}
I am using same code and its working for me....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top