Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. to get javactript variable value in code behind

to get javactript variable value in code behind

Scheduled Pinned Locked Moved ASP.NET
csharpjavascripthtmltutorial
20 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Kasi Viswanathan

    Try this if(Request.querystring["Value"]!=null) { Idval=Request.querystring["Value"]; label1.Text=Idval; }

    P Offline
    P Offline
    P_Elza
    wrote on last edited by
    #6

    I tryed this but

    Kasi Viswanathan wrote:

    Request.querystring

    is showing error. I couldent rectify this. th error is 'System.Web.HttpRequest' does not contain a definition for 'querystring'

    1 Reply Last reply
    0
    • P P_Elza

      This is the function in javascript function display() { var localpath1 = document.getElementById('lblerr'); lblerr.innerText = localpath; } it is diplaying the value on label. lblerr is the id of label. am calling this function on pageload but when i try to get the value using lblerr.text, it is showing null. y is it so? how to get value on the label

      S Offline
      S Offline
      Sandeep Akhare
      wrote on last edited by
      #7

      i think this value that you are initialzing in javascript won't persist. Do one thing Add hidden variable runat server and assign value to that hidden varaible and use that hidden varaible at server side like this var label1=document.getElementById('hidden'); label1.value="Sandeep"; and at the server side just get the value from it hidden.Value that will return you string

      Thanks and Regards Sandeep If If you look at what you do not have in life, you don't have anything, If you look at what you have in life, you have everything... "

      P 1 Reply Last reply
      0
      • S Sandeep Akhare

        i think this value that you are initialzing in javascript won't persist. Do one thing Add hidden variable runat server and assign value to that hidden varaible and use that hidden varaible at server side like this var label1=document.getElementById('hidden'); label1.value="Sandeep"; and at the server side just get the value from it hidden.Value that will return you string

        Thanks and Regards Sandeep If If you look at what you do not have in life, you don't have anything, If you look at what you have in life, you have everything... "

        P Offline
        P Offline
        P_Elza
        wrote on last edited by
        #8

        i had tried this one also previously. error was occuring the error is

        System.NullReferenceException:Object reference not set to an instance of an object

        what is this? how to rectify this. am new to .net. thats y am asking all these questions. please dont feel bad.

        S 1 Reply Last reply
        0
        • P P_Elza

          i had tried this one also previously. error was occuring the error is

          System.NullReferenceException:Object reference not set to an instance of an object

          what is this? how to rectify this. am new to .net. thats y am asking all these questions. please dont feel bad.

          S Offline
          S Offline
          Sandeep Akhare
          wrote on last edited by
          #9

          It means that you are using the object whose instance is not creaetd yet It will also give the line number. check the line number Which version of ASP.nET are you using ? Please pest the code

          Thanks and Regards Sandeep If If you look at what you do not have in life, you don't have anything, If you look at what you have in life, you have everything... "

          P 1 Reply Last reply
          0
          • S Sandeep Akhare

            It means that you are using the object whose instance is not creaetd yet It will also give the line number. check the line number Which version of ASP.nET are you using ? Please pest the code

            Thanks and Regards Sandeep If If you look at what you do not have in life, you don't have anything, If you look at what you have in life, you have everything... "

            P Offline
            P Offline
            P_Elza
            wrote on last edited by
            #10

            on line number i have writen string val=txtHidden.Value.ToString(); eat is wrong in this? while debuging it is showing the value of txthiden as

            txtHidden: : System.Web.UI.HtmlControls.HtmlInputHidden

            am using .net 2003

            S 1 Reply Last reply
            0
            • P P_Elza

              on line number i have writen string val=txtHidden.Value.ToString(); eat is wrong in this? while debuging it is showing the value of txthiden as

              txtHidden: : System.Web.UI.HtmlControls.HtmlInputHidden

              am using .net 2003

              S Offline
              S Offline
              Sandeep Akhare
              wrote on last edited by
              #11

              OK don't add ToString(); just add string val=txtHidden.Value; And be sure that txtHidden is decalared in your application such as System.Web.UI.HtmlControls.HtmlInputHidden txtHidden;

              Thanks and Regards Sandeep If If you look at what you do not have in life, you don't have anything, If you look at what you have in life, you have everything... "

              P 1 Reply Last reply
              0
              • P P_Elza

                This is the function in javascript function display() { var localpath1 = document.getElementById('lblerr'); lblerr.innerText = localpath; } it is diplaying the value on label. lblerr is the id of label. am calling this function on pageload but when i try to get the value using lblerr.text, it is showing null. y is it so? how to get value on the label

                C Offline
                C Offline
                Christian Graus
                wrote on last edited by
                #12

                P_Elza wrote:

                document.getElementById('lblerr');

                If this works, odds are that lblerr is not a server control. A server control invariably has a longer client side Id than it's server side one.

                Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

                P 1 Reply Last reply
                0
                • S Sandeep Akhare

                  OK don't add ToString(); just add string val=txtHidden.Value; And be sure that txtHidden is decalared in your application such as System.Web.UI.HtmlControls.HtmlInputHidden txtHidden;

                  Thanks and Regards Sandeep If If you look at what you do not have in life, you don't have anything, If you look at what you have in life, you have everything... "

                  P Offline
                  P Offline
                  P_Elza
                  wrote on last edited by
                  #13

                  that all thing i have written and i have tried this as string val=txtHidden.Value; also befor. it ididnt work that time so i added it to string(); i dont know wat is wrong in my code and y it is not working. why it is taking txtHiddenas "undefined"? i have defined it previously na?

                  S 1 Reply Last reply
                  0
                  • P P_Elza

                    that all thing i have written and i have tried this as string val=txtHidden.Value; also befor. it ididnt work that time so i added it to string(); i dont know wat is wrong in my code and y it is not working. why it is taking txtHiddenas "undefined"? i have defined it previously na?

                    S Offline
                    S Offline
                    Sandeep Akhare
                    wrote on last edited by
                    #14

                    Check the id of the hidden control in aspx page and aspx.cs both should be identical. Could you post your whole page ?

                    Thanks and Regards Sandeep If If you look at what you do not have in life, you don't have anything, If you look at what you have in life, you have everything... "

                    P 1 Reply Last reply
                    0
                    • S Sandeep Akhare

                      Check the id of the hidden control in aspx page and aspx.cs both should be identical. Could you post your whole page ?

                      Thanks and Regards Sandeep If If you look at what you do not have in life, you don't have anything, If you look at what you have in life, you have everything... "

                      P Offline
                      P Offline
                      P_Elza
                      wrote on last edited by
                      #15
                      using System;
                      using System.Collections;
                      using System.ComponentModel;
                      using System.Data;
                      using System.Drawing;
                      using System.Web;
                      using System.Web.SessionState;
                      using System.Web.UI;
                      using System.Web.UI.WebControls;
                      using System.Web.UI.HtmlControls;
                      using System.Text;
                      using System.IO;
                      using System.Data.SqlClient;
                      
                      namespace Web
                      {
                      	
                      	public class _3B2Temp : System.Web.UI.Page
                      	{
                      		protected System.Web.UI.WebControls.Button btnclick;
                      		protected System.Web.UI.WebControls.Label lblmsg;
                      		protected System.Web.UI.WebControls.Label lblerr;
                      		protected System.Web.UI.HtmlControls.HtmlInputHidden txtHidden;
                      		
                      	
                      		private void Page_Load(object sender, System.EventArgs e)
                      		{	
                      			
                      		}
                      
                      		#region Web Form Designer generated code
                      		override protected void OnInit(EventArgs e)
                      		{
                      			//
                      			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
                      			//
                      			InitializeComponent();
                      			base.OnInit(e);
                      		}
                      		
                      		/// 
                      		/// Required method for Designer support - do not modify
                      		/// the contents of this method with the code editor.
                      		/// 
                      		private void InitializeComponent()
                      		{    
                      			this.btnclick.Click += new System.EventHandler(this.btnclick_Click);
                      			this.Load += new System.EventHandler(this.Page_Load);
                      
                      		}
                      		#endregion
                      
                      		private void btnclick_Click(object sender, System.EventArgs e)
                      		{
                      			
                      
                      				string val=txtHidden.Value;
                      		}
                      	}
                      }
                      

                      hope this is enough

                      P S 2 Replies Last reply
                      0
                      • P P_Elza
                        using System;
                        using System.Collections;
                        using System.ComponentModel;
                        using System.Data;
                        using System.Drawing;
                        using System.Web;
                        using System.Web.SessionState;
                        using System.Web.UI;
                        using System.Web.UI.WebControls;
                        using System.Web.UI.HtmlControls;
                        using System.Text;
                        using System.IO;
                        using System.Data.SqlClient;
                        
                        namespace Web
                        {
                        	
                        	public class _3B2Temp : System.Web.UI.Page
                        	{
                        		protected System.Web.UI.WebControls.Button btnclick;
                        		protected System.Web.UI.WebControls.Label lblmsg;
                        		protected System.Web.UI.WebControls.Label lblerr;
                        		protected System.Web.UI.HtmlControls.HtmlInputHidden txtHidden;
                        		
                        	
                        		private void Page_Load(object sender, System.EventArgs e)
                        		{	
                        			
                        		}
                        
                        		#region Web Form Designer generated code
                        		override protected void OnInit(EventArgs e)
                        		{
                        			//
                        			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
                        			//
                        			InitializeComponent();
                        			base.OnInit(e);
                        		}
                        		
                        		/// 
                        		/// Required method for Designer support - do not modify
                        		/// the contents of this method with the code editor.
                        		/// 
                        		private void InitializeComponent()
                        		{    
                        			this.btnclick.Click += new System.EventHandler(this.btnclick_Click);
                        			this.Load += new System.EventHandler(this.Page_Load);
                        
                        		}
                        		#endregion
                        
                        		private void btnclick_Click(object sender, System.EventArgs e)
                        		{
                        			
                        
                        				string val=txtHidden.Value;
                        		}
                        	}
                        }
                        

                        hope this is enough

                        P Offline
                        P Offline
                        P_Elza
                        wrote on last edited by
                        #16

                        I tried this in private void Page_Load(object sender, System.EventArgs e) {} also.... please dont stop replaing in between.

                        1 Reply Last reply
                        0
                        • P P_Elza
                          using System;
                          using System.Collections;
                          using System.ComponentModel;
                          using System.Data;
                          using System.Drawing;
                          using System.Web;
                          using System.Web.SessionState;
                          using System.Web.UI;
                          using System.Web.UI.WebControls;
                          using System.Web.UI.HtmlControls;
                          using System.Text;
                          using System.IO;
                          using System.Data.SqlClient;
                          
                          namespace Web
                          {
                          	
                          	public class _3B2Temp : System.Web.UI.Page
                          	{
                          		protected System.Web.UI.WebControls.Button btnclick;
                          		protected System.Web.UI.WebControls.Label lblmsg;
                          		protected System.Web.UI.WebControls.Label lblerr;
                          		protected System.Web.UI.HtmlControls.HtmlInputHidden txtHidden;
                          		
                          	
                          		private void Page_Load(object sender, System.EventArgs e)
                          		{	
                          			
                          		}
                          
                          		#region Web Form Designer generated code
                          		override protected void OnInit(EventArgs e)
                          		{
                          			//
                          			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
                          			//
                          			InitializeComponent();
                          			base.OnInit(e);
                          		}
                          		
                          		/// 
                          		/// Required method for Designer support - do not modify
                          		/// the contents of this method with the code editor.
                          		/// 
                          		private void InitializeComponent()
                          		{    
                          			this.btnclick.Click += new System.EventHandler(this.btnclick_Click);
                          			this.Load += new System.EventHandler(this.Page_Load);
                          
                          		}
                          		#endregion
                          
                          		private void btnclick_Click(object sender, System.EventArgs e)
                          		{
                          			
                          
                          				string val=txtHidden.Value;
                          		}
                          	}
                          }
                          

                          hope this is enough

                          S Offline
                          S Offline
                          Sandeep Akhare
                          wrote on last edited by
                          #17

                          Please be sure that txtHidden value is assigned in aspx Code looks right check my code this is javascript function function HideContent() { var hiddenVar=document.getElementById('hidden'); hiddenVar.value="Sandeep"; } And here is the .cs file code protected void Page_Load(object sender, EventArgs e) { Button1.Attributes.Add("onclick", "javascript:HideContent();"); } protected void Button1_Click(object sender, EventArgs e) { if (!String.IsNullOrEmpty(hidden.Value)) { TextBox1.Text = hidden.Value; } } It is working boss in my machine

                          Thanks and Regards Sandeep If If you look at what you do not have in life, you don't have anything, If you look at what you have in life, you have everything... "

                          P 1 Reply Last reply
                          0
                          • C Christian Graus

                            P_Elza wrote:

                            document.getElementById('lblerr');

                            If this works, odds are that lblerr is not a server control. A server control invariably has a longer client side Id than it's server side one.

                            Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

                            P Offline
                            P Offline
                            P_Elza
                            wrote on last edited by
                            #18

                            Christian Graus wrote:

                            odds are that lblerr is not a server control

                            I didnt understand what u mean. I said this code is not working. and showing error

                            1 Reply Last reply
                            0
                            • S Sandeep Akhare

                              Please be sure that txtHidden value is assigned in aspx Code looks right check my code this is javascript function function HideContent() { var hiddenVar=document.getElementById('hidden'); hiddenVar.value="Sandeep"; } And here is the .cs file code protected void Page_Load(object sender, EventArgs e) { Button1.Attributes.Add("onclick", "javascript:HideContent();"); } protected void Button1_Click(object sender, EventArgs e) { if (!String.IsNullOrEmpty(hidden.Value)) { TextBox1.Text = hidden.Value; } } It is working boss in my machine

                              Thanks and Regards Sandeep If If you look at what you do not have in life, you don't have anything, If you look at what you have in life, you have everything... "

                              P Offline
                              P Offline
                              P_Elza
                              wrote on last edited by
                              #19

                              thank u boss its working now thanks a lot:):rose:

                              1 Reply Last reply
                              0
                              • P P_Elza

                                hi all i have some values on html page in javascript. i want to get that values in C#. how to do that. I tried displaying it on a label, and retreave it from there. but it is displaying the value but label.text is empty. no value in label text.

                                S Offline
                                S Offline
                                Samariya prakash
                                wrote on last edited by
                                #20

                                It may be get by two way 1. Make hidden field and assign your javascript values to that field using javascript. AND then used that hidden field in code behind. 2. Use cookies to get/set values from javascript and codebehind. B'cz as you may know cookies can be operate by javascript and codebehind both side. Prakash Samariya

                                1 Reply Last reply
                                0
                                Reply
                                • Reply as topic
                                Log in to reply
                                • Oldest to Newest
                                • Newest to Oldest
                                • Most Votes


                                • Login

                                • Don't have an account? Register

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • World
                                • Users
                                • Groups