JQuery - pass asp command line argument using jquery

I have an asp button:

<asp:button ID="btn1" runat="server" CommandArgument="" CssClass="btn1" OnClick="button_click"></asp:button> 

and script:

 $("a").click(function () { var val = $(this).attr('id').toString(); $('.btn1').attr('CommandArgument',val); alert($('.btn1').attr('CommandArgument').toString()); $('.btn1').click(); }); 

after clicking, he warns me the command argument. But in the next step - when I run btn1 by clicking with jquery, it will go into codebehind and the command argument is empty. Can I pass command argument with jquery?

I tried to save the value for global variables, but after the postback, they are all empty. And I do not want to use cookies or viewstate for this.

Thanks!

+4
source share
5 answers

CommandArgument is a fully server-side property and does not display the html attribute. Thus, you cannot change any button attribute and click on it. The good news is that you can trigger the postback using the __doPostBack function on the client side and pass your custom value as the second parameter:

 <script type="text/javascript"> $("a").click(function () { var val = $(this).attr('id').toString(); __doPostBack("<%= btn1.UniqueID %>", val); }); </script> 

And you can get the passed argument in the server click handler from the Request.Form collection:

 protected void button_click(object sender, EventArgs e) { var argument = Request.Form["__EVENTARGUMENT"]; } 

If the script above will not work, perhaps the __doPostBack function is not defined on the page. In this case, add this code to the Page_PreRender method: ClientScript.GetPostBackEventReference(btn1, string.Empty); , this will cause the page to define the __doPostBack method on the page.

+9
source

Or you can try the following:

 <script type="text/javascript"> function MaybeThisWorks(obj) { __doPostBack( obj.id, 'OtherInformation' ); return true; } </script> ... <asp:Button Text="ButtonValue" ID="RepP1" runat="server" OnClientClick="MaybeThisWorks(this);" OnClick="CodeBehindFunction" /> 

"OtherInformation" can be replaced with any string or variable.
eg
$ ('# calendar'). fullCalendar ('getDate'). getMonth () + ',' + $ ('# calendar'). fullCalendar ('getDate'). getFullYear ()

CodeBehindFunction is a code function executed on the server when you click a button (for example, a button that prints a report and needs jquery values ​​in the codebehind function).
In the codebehind function, you can get arguments using Request.Form ["__ EVENTARGUMENT"]

 protected void CodeBehindFunction(object sender, EventArgs e) { string arguments = Request.Form["__EVENTARGUMENT"]; ... } 
+1
source

Usually I just add the attribute to the button in the code behind, then access it through the DOM in Javascript or jQuery.

  mybutton.Attributes.Add("MyVar", "MyValue"); 

This will appear on the page and you can access the value using Javascript.

+1
source

I find using __doPostBack ('id', 'args') creates the postback of the full page when on the submit button. I find it easier: add a hidden page margin, set a value, then trigger the button.click () event

  <asp:Button ID="btnTrigger" runat="server" ClientIDMode="Static" OnClick="btnTrigger_Click" /> <asp:HiddenField ID="CommandArg" runat="server" ClientIDMode="Static" /> $('#CommandArg').val('my argument'); $('#btnTrigger').click(); 
+1
source

Try it. It worked for me.

// Client side jQuery:

 $('#btnTrigger').val('MyParameter'); $('#btnTrigger').click(); 

// Server side selection method

 String Param = Request.Form(btnTrigger.UniqueID) 
+1
source

Source: https://habr.com/ru/post/1447301/


All Articles