Skip to content Skip to sidebar Skip to footer

Saving Javascript Variable In Server Side Variable (vbscript)

I know you cant save javascript variables into server side variables (vbscript) directly, but is there a way around this like saving java script variables into html hidden inputs t

Solution 1:

What you're looking for is called ajax. You can do it manually, or better use a JavaScript library such as MooTools, jQuery, or Prototype.

Check out Google University's Ajax tutorial. I would avoid w3schools' tutorials.

Just to cover all the bases, why can't you just have the user submit the form?

Also, you could do this with cookies, though you won't get the cookie values on the server until the next GET or POST from the user.


Solution 2:

It is Possible to store javascript variable values into server side variable. All you have to do is to implement "System.Web.UI.ICallbackEventHandler" class.

Below is the code demonstrating how to do it.


  • In aspx Page:

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Client Calback Example</title>
    <script type="text/ecmascript">
    function LookUpStock()
    {
        var lb=document.getElementById("tbxPassword");
        var product=lb.value;
        CallServer(product,"");
    }

    function ReceiveServerData(rValue){
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <input type="password" id="tbxPassword" />
            <input type="Button" onclick="LookUpStock">Submit</button>
        </div>
    </form>
</body>

**

  • In Code Behind (CS) Page

**

public partial class _Default : System.Web.UI.Page,System.Web.UI.ICallbackEventHandler
{
protected String returnValue;
protected void Page_Load(object sender, EventArgs e)
{
    String cbReference = Page.ClientScript.GetCallbackEventReference
    (this,"arg", "ReceiveServerData", "context");
    String callbackScript;
    callbackScript = "function CallServer(arg, context)" +
    "{ " + cbReference + ";}";
    Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
    "CallServer", callbackScript, true);
}
public void RaiseCallbackEvent(String eventArgument)
{
    if(eventArgument == null)
    {
        returnValue = "-1";
    }
    else
    {
        returnValue = eventArgument;
    }
}
public String GetCallbackResult()
{
    return returnValue;
}
}

Now you can get the JavaScript variable "product" value into Server side variable "returnValue".


Post a Comment for "Saving Javascript Variable In Server Side Variable (vbscript)"