0

I have a c# 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

rambo
  • 21
  • 1
  • 7
  • use webmethod..or use ajax call..or since you tagged asp.net assuming its web form..use updatepanel – Sreenath Ganga Dec 25 '18 at 10:16
  • refer [this link](https://stackoverflow.com/a/27917333/5002329) – Sushil Jadhav Dec 25 '18 at 10:22
  • Possible duplicate of [ASP.NET jQuery Ajax Calling Code-Behind Method](https://stackoverflow.com/questions/18236634/asp-net-jquery-ajax-calling-code-behind-method) – Sushil Jadhav Dec 25 '18 at 10:28
  • J Sushil, thanks for your anwser. I think my problem is different because i want to use CommandArgument which means that i dont want the c# method to have other parametres than object sender and EventArgs e, only to access the parametres using the CommandArgument – rambo Dec 25 '18 at 10:43
  • I alse tried script manager and updatepanel with content template but doesnt work. It is still refreshing – rambo Dec 25 '18 at 10:46

1 Answers1

0

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()

Dabbas
  • 3,112
  • 7
  • 42
  • 75