Tuesday, November 15, 2011

Passing the javascript variable value to code behind Using C#

In previous post we have seen how to pass variable from server side to client side,get filename from path in javascript,Is there any method to pass JavaScript value to server side?This query raised when i work with java variable in asp.net.Here i will also give an simple tip for new one's which are workd on javascript in asp.net.Many of that get doubt "how to write java script" in asp.net .We can write java script in asp.net as write in html page. "Using hidden field we can pass v javascript  variable from client side to server side".For this i just write a simple javascript function to assign the java variable to asp hidden control.Then we can access the hidden field value and assign to text box .

Function test()
{
  int i=10;
  var hiddentest = '';
document.getElementById('<%= hiddentest.ClientID%>').value = i;
 }
Default.aspx
<asp:HiddelFiled runat="server" id="test"></asp:Hiddenfield>
<asp:textbox runat="server" id="eletext"></asp:textbox>
The id of the hidden field is used to assign the JavaScript variable value
Code behind Page(.cs):
protected void Page_Load(object sender, EventArgs e)
{
eletext.text=test.value; 
}






4 comments:

Unknown said...

it's really helped a lot ..thanks

Anonymous said...

this does not work since document.getelementid('elementid').value does not exist in asp.net

Bhaskara said...

HI,

you have to use
<%= hiddentest.ClientID%> in java script when we access the id of asp.net web control

Anonymous said...

if possible (is available) try to assign value in java to document.cookie = javavalue and then get it from codebehind var x= document.cookie (either via webbrowser or page)

Bel