严重事件发生后触发javascript函数 [英] javascript function firing after sever side event

查看:63
本文介绍了严重事件发生后触发javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在服务器端事件之前触发javascript函数

I need to fire javascript function before than server side event

<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);

}


这里服务器端事件首先触发它没有获得"lblDistance"值.

我应该怎么做才能确保首先触发javascript函数.


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.

推荐答案

在表单加载中执行RegisterStartupScript.此外,单击按钮还应包含设置为调用此initialize()方法的OnClientClick方法.

现在,您正在单击按钮上吐出的Java脚本,这实际上是在服务器上发生的.您需要先准备好javascript,然后再单击页面加载,然后onclientclick 首先将在客户端上调用javascript函数,然后将调用服务器端函数.
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.


这篇关于严重事件发生后触发javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆