简体   繁体   中英

setting an asp variable via java script

i have an aspx.cs page:

public partial class multiIndex : System.Web.UI.Page
{

    public string Link = "";
    public string board = "";
    public string against = "";

    protected void Page_Load(object sender, EventArgs e)
    {
    }
}

and an aspx page and in it a script and this code:

<script>
var newBoard = "<%=board%>";
<%UpdateDataBase(board)%>
</script>

now this is working but i can't use it in reverse:

var board = "something";
<%board=%>= board;
<%UpdateDataBase(board)%>

so how do i do such a thing without php or any other libarary like node.js, i want to keep it simple

I guess you are getting confused with the point of execution (whether Server or Browser) here. ".aspx" page is rendered by Server and it will be able to edit the contents of the page using the logic you have written. So when you have a code like this var newBoard = "<%=board%>"; , Server evaluates the value of board and assigns the constant value to newBoard before rendering the page. You can check this using the developer tools in browser.

But if you want to set the value of "newBoard" in "Board" that is not possible in Javascript as it is executed in browser and the variable "Board" has meaning only in Server in the appropriate scope of program execution. If your intention is to do some Server side processing, then you can use API calls to the Server to do the necessary processing.

要设置和获取asp变量(服务器端),您只需要在page.aspx.cs页中创建一个cookie,并在page.aspx中使用java脚本来获取它,因为服务器和客户端上都存在cookie,对它的任何更改将在客户端和服务器上注册。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM