简体   繁体   English

从Java脚本在ASP.NET中调用服务器端事件

[英]Calling server side event in asp.net from java script

Surely will be marked as duplicate one but after tons of question and example i couldn't solve my problem. 当然会被标记为重复项,但是经过大量的问题和例子之后,我无法解决我的问题。
What i want? 我想要的是?
Calling server side event handler in asp.net from client side java script it is not going on server side. 从客户端Java脚本在asp.net中调用服务器端事件处理程序不会在服务器端进行。 I checked it by setting breakpoint, the page flicks but server side method is not called. 我通过设置断点来检查它,页面闪烁,但是未调用服务器端方法。
My click event on code behind is 我对后面代码的点击事件是

  protected void btninsert_Click(object sender, EventArgs e)
  {
      // my code
  }

aspx file aspx文件

   <asp:Button ID="btninsert" runat="server" ValidationGroup="form" CssClass="btn"
      OnClientClick="DoPost()" Text="Save" />

Javascript method is JavaScript方法是

   function DoPost() {
        function DoPost() {
        var chk = document.getElementById('<%= chkstatus.ClientID %>');
        if (chk.checked)
            __doPostBack('<%= btninsert.ClientID %>', 'OnClick');
        return true;
        //return false;
    } 
    } 

I also tried this __doPostBack('btninsert', 'OnClick'); 我也尝试过__doPostBack('btninsert', 'OnClick'); and __doPostBack('btninsert', ''); __doPostBack('btninsert', ''); and $('btnSubmit').trigger('click'); $('btnSubmit').trigger('click'); with no success. 没有成功。

What am i doing wrong? 我究竟做错了什么?

Edit: If i uses OnClick event then it is going in the server side event irrespective of the if condition in the DoPost method. 编辑:如果我使用OnClick事件,则无论DoPost方法中的if condition如何,它将在服务器端事件中进行。

Ahh it was simple. 嗯,很简单。 Thanks to all answers. 感谢所有答案。
Solution : 解决方案
Use OnClick attribute for server side event and OnClientclick for client event for validation. OnClick属性用于服务器端事件,将OnClientclick用于客户端事件以进行验证。 OnClientClick javascript method will return true and false which decides whether serverside onclick event will fire or not. OnClientClick javascript方法将返回truefalse ,这将决定是否触发服务器端onclick事件。
Code will be somthing like this 代码会像这样

  <script type="text/javascript">
    function DoPost() {
        var chk = document.getElementById('<%= chkstatus.ClientID %>');
        if (chk.checked) {
            var c = confirm("are you sure?");
            if (c == true) {
                return true;
            } else
            { return false; }
        }
        return true;
    } 
</script>

And the Button tag 还有Button标签

<asp:Button ID="btninsert" runat="server" ValidationGroup="form" CssClass="btn"
   OnClientClick="return DoPost();" OnClick="btninsert_Click" Text="Save" />

it can work!! 它可以工作!

if you call __doPostBack('btninsert', ''), add below to Page_Load 如果调用__doPostBack('btninsert',''),则将以下内容添加到Page_Load

 protected void Page_Load(object sender, EventArgs e)
{
    if ( Request["__EVENTTARGET"] == "btninsert" ) {
        btninsert_Click(sender, e);
    }
}
<asp:Button ID="btninsert" runat="server" ValidationGroup="form" CssClass="btn" OnClick="btninsert_Click" OnClientClick="DoPost()" Text="Save" />

Add OnClick="btninsert_Click" , because without this information, asp.net has no way know which event handler to run for the "Click" event. 添加OnClick="btninsert_Click" ,因为如果没有此信息,asp.net无法知道为“ Click”事件运行哪个事件处理程序。 In case you want to control whether to post back by client side script. 如果您想控制是否通过客户端脚本回发。 You can do something like this: 您可以执行以下操作:

<asp:Button ID="btninsert" runat="server" ValidationGroup="form" CssClass="btn" OnClick="btninsert_Click" OnClientClick="return DoPost()" Text="Save" />

function DoPost() {
        __doPostBack('<%= btninsert.ClientID %>', 'OnClick');
        return true;
      //return false;
    } 

Note the OnClientClick="return DoPost()" 请注意OnClientClick="return DoPost()"

<asp:Button ID="btninsert" runat="server" ValidationGroup="form" CssClass="btn"
      OnClientClick="return DoPost();" Text="Save" />

modify your line like this 像这样修改你的行

You can try Ajax, by using Webmethod. 您可以使用Webmethod尝试Ajax。 I used this in my project and it works fine! 我在项目中使用了它,效果很好! You may see the question, solutions and code here: http://www.codeproject.com/Questions/416748/How-to-Call-WebMethod-of-Csharp-from-JQuery 您可能会在这里看到问题,解决方案和代码: http : //www.codeproject.com/Questions/416748/How-to-Call-WebMethod-of-Csharp-from-JQuery

Use ASP link button instead of ASP BUTTON. 使用ASP链接按钮代替ASP按钮。 Button control is not called from javascript because it type is submit while link button type is button. 不从javascript调用按钮控件,因为它的类型是“提交”,而链接按钮类型是“按钮”。

For example i have added two asp control 例如我添加了两个ASP控件

        <asp:LinkButton ID="lbtest" runat="server" Text="Link Button"></asp:LinkButton>
        <asp:Button runat="server" ID="btnbutton" Text="Button" /> 

and execute the page When we view the source of running page its look like this 并执行页面当我们查看运行页面的源代码时,它看起来像这样

        <a id="lbtest" href="javascript:__doPostBack('lbtest','')">Link Button</a>
        <input type="submit" name="btnbutton" value="Button" id="btnbutton" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM