How would I move the value of input type text to a asp.net hiddenfield control on button click?

How would I move the value of input type text to a asp.net HiddenField control on button click?

I would like to move the value of the text box to the HiddenField control so i have access to it in code behind in C#.

Thanks!

Html:
        <input id="Text1" type="text" /><br />
        <asp:HiddenField ID="HF1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click"  />

Open in new window


js:
function getHFvalue(){
document.getElementById('HiddenField1').value === document.getElementById('Text1').value;
}

Open in new window


Button Click in code behind:
var x = HiddenField1.Value.ToString();
//  manipulate x here...

Open in new window

bmanmike39Asked:
Who is Participating?
 
Robert SchuttSoftware EngineerCommented:
HTML; add an attribute OnClientClick to the button which calls the javascript function:
        <input id="Text1" type="text" /><br />
        <asp:HiddenField ID="HF1" runat="server" />
        <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="getHFvalue()" OnClick="Button1_Click"  />

Open in new window


JS; use only one equals sign for an assignment and use the same ID as in the HTML for the hidden field:
        function getHFvalue() {
            document.getElementById('HF1').value = document.getElementById('Text1').value;
        }

Open in new window


code behind; also use the same ID, better to use string type instead of var and .Value is already a string so no .ToString() necessary:
string x = HF1.Value;

Open in new window

0
 
bmanmike39Author Commented:
Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.