Click here to Skip to main content
15,878,945 members
Articles / Web Development / ASP.NET
Tip/Trick

Use HiddenField Control to store values from javascript function

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
21 Sep 2010CPOL 16.8K   1
To be able to get the value in the PostBack from the ASP.Net TextBox control that assigned by an external javascript function, you have to store the same value also in a HiddenField Control.

Then in the PostBack Event you can get the value of the HiddenField Control But not the TextBox Control, because the TextBox Control value will be reseted.

Here is an Example:


We passed the controls ClientIDs as querystring to the popup window, then from the popup window we have the following javascript to assign the values to the controls in the opener aspx page :

JavaScript
window.opener.document.getElementById('<%=Request["lat_ctr_id"]%>').value = marker.getPoint().lat();

 window.opener.document.getElementById('<%=Request["ctr_hdn_lat_id"]%>').value = marker.getPoint().lat();


Now from my cs file, i have a search button with click event (this will do postback)

C#
protected void btn_search_Click(object sender, EventArgs e)
   {
       //this will not work.. the "lat" querystring will be empty
       //Response.Redirect("search_results.aspx?lat=" + txt_lat.Text  ) ;

       //this will work.
       Response.Redirect("search_results.aspx?lat=" + hdn_lat.Value);


   }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Gulf air - Bahrain
Bahrain Bahrain
Current Position is : Sr. Information System Analyst

Leading a Team of Developers for developing ecommerce websites and systems integration using several integration methods such as XML.

Manage ISP infrastructure, web servers, database servers, application servers, email servers and networks. Design the architecture of complex web applications; build web applications in JAVA, asp, asp.Net; manage web sites including networking and database; experienced in MS Sql Server and Oracle;

stay current with emerging trends and technological advances in the industry.

MCTS,MCAD VB.NET, ASP.NET and XML Services,BTEC National Diploma in Computer Studies.

Comments and Discussions

 
Questionthe cause of the problem Pin
hussain.attiya4-Dec-11 0:50
hussain.attiya4-Dec-11 0:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.