问题描述
任何人都可以给我一个详细的解释或资源,说明在asp.net按钮点击事件中使用Client-Script调用
java脚本函数时会发生什么。我的问题是如果我从第一行调用java脚本函数,事件将不会停止并填充java脚本函数。相反,它将遍历整个C#代码,然后填充java脚本函数。
Ex:
Can anyone give me a detailed explanation or resource regarding what happens when calling a
java-script function using Client-Script, inside an asp.net button click event. And my problem is if I call a java-script function from the 1st line, event won't stop and populate the java-script function. Instead it'll go through the whole C# code and then populate the java-script function.
Ex:
<script type="text/javascript">
function ShowPopUp() {
var result = confirm("Do you want to proceed? ");
alert(result);
}
</script>
protected void btnClick_Click(object sender, EventArgs e)
{
ClientScript.RegisterStartupScript(this.GetType(), "Popup", "ShowPopUp();", true);
for (int i = 0; i < 10; i++)
{
}
}
<asp:button ID="btnClick" runat="server" text="Click here" OnClick="btnClick_Click" />
推荐答案
<asp:Button ID="btnClick" runat="server" Text="Click here" OnClick="btnClick_Click" OnClientClick="return ShowPopUp();" />
并将JavaScript函数更新为...
And update the JavaScript function as...
<script type="text/javascript">
function ShowPopUp() {
return confirm("Do you want to proceed? ");
//alert(result);
}
</script>
因此,当用户在确认框中点击确定
时,它会返回 true
,因此它将触发按钮单击服务器端事件。否则,它将返回 false
并保持原样。
So, when user would click Ok
in confirm box, it would return you true
, hence it will then fire the Button Click Server Side Event. Otherwise, it would return false
and stay as it is.
<input type="button" id="btn" />
<script>
这篇关于在C#按钮中调用JavaScript方法使用ClientScript单击“事件”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!