6

I have a variable in aspx.cs when I click the Datagrid particular row;
Then from the javascript, I should get that value and pass into aspx.cs variable.

how to do this?

Vikrant
  • 4,920
  • 17
  • 48
  • 72
Out
  • 627
  • 2
  • 13
  • 20
  • 2
    what exactly you want to do, provide some code, what exactly you want to do with that variable at server side? – Rahul R. Jan 30 '13 at 11:50
  • There are many ways to do this however if you tell the purpose someone might advise you an alternate easier method. – Zo Has Jan 30 '13 at 13:48

1 Answers1

12

Using html controls

First you use a hidden input control as:

<input type="hidden" value="" id="SendA" name="SendA" />

Second you add to that control the value you like to send on code behind using javascript as:

document.getElementById("SendA").value = "1";

And then on post back you get that value as:

Request.Form["SendA"]

Using asp.net Controls

The same way if you use asp.net control can be as:

<asp:HiddenField runat="server" ID="SendA" Value="" />
<script>
   document.getElementById("<%=SendA.ClientID%>").value = "1";
</script>

and get it on code behind with SendA.Value;

And of course you can use ajax calls to send on code behind values, or simple call url with url parameters that return no content.

Aristos
  • 66,005
  • 16
  • 114
  • 150
  • finally, so simple. Adding "<%=...%>" did the trick. thanks a lot Aristos. – Hades200621 Jun 25 '14 at 06:14
  • one thing I'd like to add is that if you're not seeing an updated value from the hiddenField, try creating a button to trigger a new event and you will see the value update(as opposed to relying on the pageLoad function event) – dizad87 Jan 29 '18 at 04:31