Click here to Skip to main content
15,894,825 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need to fire javascript function before than server side event
C#
<asp:Button ID="btnFare" runat="server" Text="Calculate Fare" onclick="btnFare_Click"/>

protected void btnFare_Click(object sender, EventArgs e)
{

ClientScript.RegisterStartupScript(GetType(), "Javascript", "javascript:initialize(); ",true);


double farecharges;
double distance;
distance=Convert.ToDouble(lblDistance.Value);

}

here server side event is firing first that it is not getting "lblDistance" value.

what should i do to make sure that javascript function fires first.
Posted

1 solution

Do the RegisterStartupScript in form load. also the button click should contain the OnClientClick method set to call this initialize() method.

Rgiht now you are spitting the java script on button click which is essentially happening on server. yuo need to have javascript ready before button click i,e, page load and then the onclientclick will first call javascript function on client and then the server-side function will be invoked.
 
Share this answer
 
Comments
Member 8150795 9-Apr-12 4:33am    
actually the problem is that onclientClick is not working in my code .that why
i uses the registerstartscript tag .....again i tried and the same problem is coming

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900