开发者

I have a btnSave button i want to call this btnSave_Click method on click of an input button btnmore through jquery how can i do that

开发者 https://www.devze.com 2023-02-08 01:31 出处:网络
<html xmlns=\"http://www.w3.org/1999/xhtml\" > <head id=\"Head1\" runat=\"server\"> <title>Jquery</title>

<html xmlns="http://www.w3.org/1999/xhtml" >

<head id="Head1" runat="server">
    <title>Jquery</title>

    <script type="text/javascript" src="scripts/jquery-1.4.2.js"></script>
    <script type="text/javascript">  jQuery(document).ready(function () {

         $('#btnmore').click(function () {  
          $('<%=btnSave.ClientID%>').click(); 
          alert('done');
         $.ajax({type: 'POST',
                contentType: 'application/json; charset=utf-8',
                data: '{}',
                dataType: 'json',
                url: 'Default.aspx/btnSave_Click',
                success: function(res开发者_JAVA技巧ult){

                                            alert(result);} });   
         });
        });</Script> 
</head>
<body>
    <form id="form1" runat="server">
    <asp:scriptManager id="script1"></asp:scriptManager>
    <div>
        <table>

            <tr>
                <td>
                    <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />

                    <input type="button" id="btnmore" value="More" />
                </td>

            </tr>
        </table>
    </div>
    </form>
</body>
</html>

Server side code

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Services;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    [WebMethod]
    public static string MyMethod()
    {
        return "abc";
    }


    [WebMethod]
    protected void btnSave_Click(object sender, EventArgs e)
    {
        Response.Write("abc");
    }
}


Why not just use:

$('#btnmore').click(function(
    $('#btnSave').click();
});

Note that you will need to get the actual name of btnSave, because it will probably be in a naming container. Something like this should get the correct id:

$('<%=btnSave.ClientID%>').click();


This can be an alternate solution. On the same button you can call 2 click functions; one client side and another server side.

<asp:Button ID="btnSave" Text="Save" runat="server" OnClick="btnSave_Click" OnClientClick=" return buttonClientValue()" />

buttonClientValue() function should return true or false. If returns true than only btnSave_Click will be executed, otherwise not.


Here's what I like to do. Since you can't really control the ID assigned by ASP.NET, you need some javascript code in the same page as the button you want clicked. you can wrap that code in a function with a known name that you can call from external .js files if need be. But you need to let ASP.NET generate the 'click' function for you:

function clickit() {
    <%=this.Page.GetPostBackClientEvent(this.btnSave, string.Empty)%>;
}

then elsewhere in your javascript just call clickit()


$('#btnmore').click(function(
    $('#btnSave').click();
});

Its work for me.


<script type="text/javascript">
    $(document).ready(function () {
        $('#_btnSave').click(function () {
            $("form").submit();
        });
    });
</script>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号