在C#按钮中调用JavaScript方法使用ClientScript单击“事件” [英] Calling a JavaScript method in C# button Click Event using ClientScript

查看:71
本文介绍了在C#按钮中调用JavaScript方法使用ClientScript单击“事件”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以给我一个详细的解释或资源,说明在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" />

推荐答案

我建​​议你使用 OnClientClick 事件。



因此,代码看起来像......

I would suggest you to use OnClientClick Event.

So, the code would look like...
<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.


以及什么是Tadit Dash是真的。



通常我不喜欢从c#调用javascript,我更喜欢(如果可能的话)到相反的地方,





这里是一个更像客户端的解决方案(需要在你的页面中包含jquery.js):



well what sais Tadit Dash is true.

usually i don't like to call javascript from c#, i prefer (if possible) to to the opposite,


here is a more client-like solution (need to include jquery.js in your page) :

<input type="button" id="btn" />
<script>


function (){


这篇关于在C#按钮中调用JavaScript方法使用ClientScript单击“事件”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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