Store value in asp.net Application Variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sweetneel
    New Member
    • Oct 2008
    • 42

    Store value in asp.net Application Variable

    hi all,

    can any one give me an idea how could i store a Asp.net Application variable by javascript.
  • CroCrew
    Recognized Expert Contributor
    • Jan 2008
    • 564

    #2
    Hello sweetneel,

    JavaScript is client side and application variables are used server side. So, I don’t know of any way to use “raw” JavaScript to seed a value in an application variable with out doing a post back to the server to seed the value in the application variable.

    There is a Script Callback that you could look into. Here is an old article: http://www.developer.com/net/asp/art...-ASPNET-20.htm

    Happy Coding,
    CroCrew~

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Honestly it's pretty simple.
      Use an ASP.NET HiddenField control. This control is accessible in your VB.NET code so you can easily set it's value property to be the Application Variable. You can then access this HiddenField in your JavaScript. The one thing that you have to remember is that the HiddenField control is like any other ASP.NET control when it comes to ClientIDs. This means that if you want to access this control using the JavaScript document.getEle mentByID() method you have to supply the method with the HiddenField's ID as it appears "client-side". To do this you can use the HiddenField.Cli entID property to write the ID of the HiddenField into the JavaScript.

      For example, if you had an ASP.NET HiddenField control defined as such:
      Code:
      <asp:HiddenField ID="myApplicationVarField" runat="server" />
      You would have to write the ClientID of "myApplicationV arField" into the JavaScript method.

      There are a few ways to do this. I'm pretty sure I've already shared them with you. One such way is to use ASP in your ASPX file to call the Response.Write method in order to write the ID of the control into your JavaScript....i n this case your JavaScript would exist in your ASPX page:
      Code:
      function myFunction(){
        var myApplicationVarField = document.getElementById('<%=myApplicationVarField.ClientID %>');
      }
      Or if you were to generate your script server side you'd have something like (VB code):
      Code:
      Dim theScript As New StringBuilder()
      theScript.Append("function myFunction(){")
      theScript.Append(Environment.NewLine)
      theScript.Append("  var myApplicationVarField = document.getElementById('")
      theScript.Append(myApplicationVarField.ClientID)
      theScript.Append("');")
      theScript.Append(Environment.NewLine)
      theScript.Append("}")
      'If you aren't using Ajax then you register the script with the Page:
      Page.ClientScript.RegisterClientScriptBlock(Me.Page.GetType,"theScript",theScript)
      'if you are using Ajax then you need to register 
      'the script with ScriptManager instead of with the page
      You could also use external JavaScript. If you embed the external JavaScript into your solution then you can specify that the JavaScript should be compiled in such a way that it evaluates ASP code. I've only ever done this in CSS files...but I know it's possible.

      Hmm, now after all of that I think that you could simplify this even further and skip the HiddenField control (so long as you don't need to pass information from JavaScript back to the server)....you could just write the value of the variable directly into the JavaScript function....... ...

      -Frinny

      Comment

      Working...