Saving javascript variable in server side variable (vbscript)

限于喜欢 提交于 2019-12-11 15:35:53

问题


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 then using javascript to post. Is this possible? If not what else can i do? Below is my code so far get the value of a drop down list - javascript

        function selectedDatabase() {

            select_temp = form1.elements["selection"];
            select_index = select_temp.selectedIndex;
            select_text = select_temp.options[select_index].text;                                                              

        }    

Below is the HTML code

<center><select id="selection" onchange="selectedDatabase()">
<option>Movies</option>
<option>Movies 2</option>
<option>New Movies</option>
<option>New Movies 2</option>
</select></center>
</td></tr>

回答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.




回答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".



来源:https://stackoverflow.com/questions/6399464/saving-javascript-variable-in-server-side-variable-vbscript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!