简体   繁体   中英

how to call a a C# server side method from ajax passing parametres to the function with CommandArgument?

I have ac# method which stores data to sql server. This function is called from an Onlick and i pass the parametres using CommandArgument. Like this.

<asp:LinkButton runat="server" onClick="save" CommandArgument='<%# Eval("post_id").ToString() + "," + Eval("user_id").ToString()%>'></asp:LinkButton>

This is the c# method

    protected void vote(object sender, EventArgs e)
  {
    LinkButton lb = (LinkButton)sender;
    string arguments = lb.CommandArgument;
    string[] args = arguments.Split(',');

    string post = args[0];
    string user = args[1];
    .............
  }

All i want to do is to call the c# method using ajax because i dont want the page to be refreshed, but i cant call that function passing CommandArgument. I want to use CommandArgument because i dont want the c# method to have other parametres than object sender and EventArgs e

Try this:

<asp:LinkButton runat="server" onClick="save" CommandArgument='<%# Eval("post_id").ToString() + "," + Eval("user_id").ToString()%>'></asp:LinkButton>

In script tag:

$("#<%= myVote.ClientID %>").click();

Attempt 2:
In script tag:

var func = $("#<%= myVote.ClientID %>").attr('OnClick');

eval(func);

The idea is that Asp.Net WebForms uses JS to do postback to backend, a function generated called __doPostBack , the idea is to use the same function and call it, or just inspect the generated HTML of the page and get the value of ,as I remember, OnClick attribute of the button, and use it.

This may help also:
How to use __doPostBack()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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