C# .NET - fetching javascript return valjue in C# code Behind

Asked By Nitin Sharma on 10-Jul-08 04:17 AM
Hi All.,
   I have a function in Javascript and suppose it returns a string...now i have to fetch that String in the C# code behind..
Hw can i do that???

Thanks in advance..!!

Bbye

Nitin Sharma
Software Engineer
.Net Technologies

u can achive this goal in no of way.

Web Star replied to Nitin Sharma on 10-Jul-08 04:24 AM

u can achive this goal in no of way,

u can use a hidden control and put that value string on that hidden control in javascript function

in javascript

documnent.getElementById('hdnInput').Value= strValue;

and u can access that hidden control in code file

Dim str as string

str = hdnInput.text


get thru hidden field

Santhosh N replied to Nitin Sharma on 10-Jul-08 04:25 AM

You can have a hidden field for doing this..

Store the return value from Javascript function in the hidden field...

document.getElementById('hdnName').value = "value of the return value";

Get this value from c# code..

hdnName.Text will give the value

cheers

Fetching Javascript return value in C# code behind

Kalit Sikka replied to Nitin Sharma on 10-Jul-08 04:30 AM

you can use a hidden that runs server side and set its value on the client with js.. then when u turn back server, u can load the new value of hidden from server..

<input type="hidden" id="myHidden" runat="server" />

By using Hidden Field
Sagar P replied to Nitin Sharma on 10-Jul-08 04:44 AM

You have to use hidden field for that. Just add a hidden field and put your string into that hidden field.

Then you can easily access that value of hidden field into code behind;

e.g.;

Declare a hidden filed like this;

<asp:HiddenField ID="hdnX" runat="server" />

Then in javascript file access it to store string like;

document.getElementById('hdnX').value= str ;

And if u r using master page then access it as;

document.getElementById('<%=hdnX.ClientID%>').value= str ;

And in code behind access it as;

hdnX.Value

Best Luck!!!!!!!!!!!!!!!!!!!!
Sujit.

 

Use Hidden Fields
Shailendrasinh Parmar replied to Nitin Sharma on 10-Jul-08 05:14 AM
You can use hiddenfields.

Store the value of string in hidden field variable in javascript and access that value
in code behind.

Or you can take one textbox and keep it hidden. And save your string value to textbox.
And get it in code behind from textbox.

like

document.getElementById('txtName').value = string_value;

Code Behind ::

txtFirstName.Text = txtName.Text;

Hope it helps.
any other option?
Nitin Sharma replied to Web Star on 10-Jul-08 05:15 AM

can it be without... the hidden field  option???

Isn't hidden field effects the performance??

Please tell me another option ,if any???


Thanks,
Nitin Sharma
another option??
Nitin Sharma replied to Shailendrasinh Parmar on 10-Jul-08 05:16 AM

can it be without... the hidden field  option???

Isn't hidden field effects the performance??

Please tell me another option ,if any???








Thanks,

Nitin

A hidden field does not affect performance any more
Peter Bromberg replied to Nitin Sharma on 10-Jul-08 07:04 AM

than any other kind of formfield. You could also consider putting the value on the querystring, e.g.

location.href = location.href +'?mything=' +myvalue;

This will cause the page to reload, and you can grab the value in your Page_Load event handler with Request.QueryString["mything"];