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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bmanmike39Author Commented:
Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.