Calling server side event in asp.net from java script

前端 未结 6 2069
-上瘾入骨i
-上瘾入骨i 2020-12-20 09:50

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

相关标签:
6条回答
  • 2020-12-20 10:07

    Ahh it was simple. Thanks to all answers.
    Solution:
    Use OnClick attribute for server side event and OnClientclick for client event for validation. OnClientClick javascript method will return true and false which decides whether serverside onclick event will fire or not.
    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

    <asp:Button ID="btninsert" runat="server" ValidationGroup="form" CssClass="btn"
       OnClientClick="return DoPost();" OnClick="btninsert_Click" Text="Save" />
    
    0 讨论(0)
  • 2020-12-20 10:25
    <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. 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()"

    0 讨论(0)
  • 2020-12-20 10:26

    You can try Ajax, by using Webmethod. 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

    0 讨论(0)
  • 2020-12-20 10:28

    it can work!!

    if you call __doPostBack('btninsert', ''), add below to Page_Load

     protected void Page_Load(object sender, EventArgs e)
    {
        if ( Request["__EVENTTARGET"] == "btninsert" ) {
            btninsert_Click(sender, e);
        }
    }
    
    0 讨论(0)
  • 2020-12-20 10:29
    <asp:Button ID="btninsert" runat="server" ValidationGroup="form" CssClass="btn"
          OnClientClick="return DoPost();" Text="Save" />
    

    modify your line like this

    0 讨论(0)
  • 2020-12-20 10:29

    Use ASP link button instead of ASP BUTTON. Button control is not called from javascript because it type is submit while link button type is button.

    For example i have added two asp control

            <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" />
    
    0 讨论(0)
提交回复
热议问题