3

I'm making an ajax request and storing my response in an hidden field.I'm doing this through javascript using getelementbyid.value.This javascript function is on body onload.Now after I get this value I would like to use this in C#.I can't have any button onclick event or anything of that sort.Just have a hidden input type

1
  • As per my understanding u cant use script variable on server side. Also can u put some piece of code here? Commented Nov 12, 2010 at 5:27

1 Answer 1

5

If an asp.net HidenField webControl has a value then all you need to do is the following:

aspx page:

        <asp:hiddenfield id="hf_MyValue"
          value="whatever" 
          runat="server"/>

cs page:

string value = hf_MyValue.Value;

If you want to do something with the value when it's assigned handle the onValueChanged event:

        <asp:hiddenfield id="hf_MyValue"
          onvaluechanged="ValueHiddenField_ValueChanged"
          value="whatever" 
          runat="server"/>

While you CAN use a value of an asp.net HiddenField that is set using javascript in C# make sure that you understand that this can only be done after a postback.

Here is some info on the Client/Server relationship. Javascript and C# respectively in your question.

Sign up to request clarification or add additional context in comments.

1 Comment

Can you tell me how to postback for hidden field.I tried and it's not working

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.