.NET后台代码如何获取javascript定义的变量值
如题。我百度了下,有说javascript先把值赋给hidden控件,然后后台获取,我试了下,貌似没成功。还有说用ajax,但是没找到案例。谁还有更好的办法啊?
------解决方案--------------------<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
<script type="text/javascript">
function setVal(){
var obj=document.getElementById("<%=HndVal.ClientID%>");
obj.value="这是赋的值";
}
</script>
</head>
<body onload="setVal()">
<form id="form1" runat="server">
<asp:HiddenField ID="HndVal" runat="server" />
<br />
<asp:Button ID="BtnObtain" runat="server" onclick="BtnObtain_Click"
Text="获得隐藏值" />
<br />
<asp:Label ID="LbShow" runat="server" Text="后台得到的值显示在这里"></asp:Label>
</form>
</body>
</html>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void BtnObtain_Click(object sender, EventArgs e)