开发者

How to get a part of response using jQuery Ajax

Consider there is a page with 10 buttons that every ones do a different work.How I can call server-side click event of each one and get response for just that button using jQuery Ajax? I don't want declare a static method for my code.thanks

*EDIT1 :*Consder this:

$.ajax({
    type: "POST",
    url: "MsAjax.aspx",
    method: "getTime",
    data: { date_in: new Date() },
    success: function(data) {
        $("#div").html(String(data));
    }
}); 

In normal way we code like top code ,but I want click on buttons cause execute it's server side event code but using ajax and page not postback,and get response of it


EDIT 2:

<table>
    <tr>
        <td>
            <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button"  OnClientClick="nima();"/>
        </td>
        <td>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            <asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Button" OnClientClick="nima2();"/>
        </td>
        <td>
            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            <asp:Button ID="Button3" runat="server" onclick="Button3_Click" Text="Button" />
        </td>
        <td>
            <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
        </td>
    </tr>
</table>

and code behind:

 protected void Button1_Click(object sender, EventArgs e)
    {
        TextBox1.Text = "11111111";
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        TextBox2.Text = "22222222";
    }

    开发者_StackOverflow中文版protected void Button3_Click(object sender, EventArgs e)
    {
        TextBox3.Text = "3333333";
    }


define every button with same class and different ids.

ex: <input type="button" class="cls_button" id="work1"/>
<input type="button" class="cls_button" id="work2"/>

now you can write a jquery ajax script like this

$('.cls_button').click(function() {
var id = $(this).attr('id');

  $.ajax({
   type: "POST",
   url: "some.php",
   data: "work="+id,
   success: function(msg){
     alert( msg );
   }

});
});

now you can write a switch expression in some.php page to define different works.

Update:

you can use same command for "OnCommand" and different "Button id" ex:

<asp:Button id="Button1" Text="Sort Ascending" CommandName="Sort" CommandArgument="Ascending" OnCommand="CommandBtn_Click" runat="server"/> 
<asp:Button id="Button2" Text="Sort Descending" CommandName="Sort" CommandArgument="Descending" OnCommand="CommandBtn_Click" runat="server"/>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜