Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
When the page load ( OR on the first click of the button) then the hidden field is empty. When I click on the button again, then the value is populated. Can someone please indicate what the problem is when i call java script variable from aASP.NET cs page.

Simply i want to asked server name from the user(when login page loading) via java script message and derect user to appropriate SQL server.. but always at the first time hidden variable returns the Empty value and only 2nd time it delivers the actual valuewhich which is entered by the user.can someone give me a suggestion for avoid this matter????


My code : In Index.aspx


XML
<head runat="server">
    <title></title>

<link rel="stylesheet" href="Resources/Login.css" type="text/css" media="all">


 <script type="text/javascript">


     function ServerReg() {
         var serverName = prompt("Enter Server Name?", 'Testserver');

         if (serverName) document.getElementById('<%= getServer.ClientID %>').value = serverName;

         else alert('You pressed Cancel or no value was entered!');

     }
</script>


</head>
<body ">


    <form id="form1" runat="server">



     <div>

    <asp:HiddenField ID="getServer" runat="server"
     onvaluechanged="getServer_ValueChanged" />
// No difference occurs when i called hidden variable  from Page load event.

    </div>




In Index.cs page:


C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using DAL;
using BAL;
using System.IO;

namespace PAL
{
    public partial class Index : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack )
            {


                    string filename = "d:\\myXmFile.xml";


                    if (File.Exists(filename))
                    {
                       
                        System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE='JavaScript'&     gt;alert('file exists')</SCRIPT>");

                    }
                    else
                    {
                        
                        
                      ClientScript.RegisterStartupScript(GetType(), "load", "ServerReg();", true);

                      Server_Config.getserver(getServer.Value);
                          // if i pass this value it will write empty value to      the xml.
                    }

            }






In Hidden variable control:


C#
protected void getServer_ValueChanged(object sender, EventArgs e)
        {
            if (getServer.Value != "")
            {
                Server_Config.getserver(getServer.Value);
            }
        }



So can someone give solution for this matter???
Posted
Updated 31-Mar-12 21:37pm
v2
Comments
deepakaitr12345 1-Apr-12 5:42am    
One thing Is very important when you are using java script using java script you can not maintain the state of the control Thanks

1 solution

Hi,

Place the script content, after the html body.
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900