js取得后台代码值的方法

1、将要获取的值用public属性公开,然后在js中用变量var xx = <%= PubProperty %>
2、将值保存到一个html控件的属性中,这个属性可以自定义,例如<input type="text" id="test1"
xxx=<%=PubProperty%>
JS中取值 var y = document.activeElement.getAttribute("xxx");
 var y = document.getElementById('test1').getAttribute("xxx");

例如:在aspx.cs中定义属性 ,要Page_Load()事件中要给这个属性赋值

    public string CurrentID
        {
            get { return ViewState["currID"] == null ? string.Empty : (string)ViewState["currID"]; }
            set { ViewState["currID"] = value; }
        }

在 js中取值 的方法

A方法:

function OpenWindow()
        {
            var currid = <%= CurrentID %>
           // var strUrl = "AddProduct.aspx?cateId="+document.activeElement.getAttribute("cateid");
            var strUrl = "AddProduct.aspx?cateId="+document.getElementById('btnAddProduct').getAttribute("cateid");
            window.open(strUrl,"Product","statusbar=yes,width=600px,height=550px,top=0,left=0");
        }

B:方法;在一个Html控件中自定义一个属性 cateid

<input id="btnAddProduct" type="button" value="添加产品" cateid='<%= CurrentID %>' onclick="OpenWindow();"  /></div>

  function OpenWindow()
        {
            
           //a var strUrl = "AddProduct.aspx?cateId="+document.activeElement.getAttribute("cateid");
            //b  var strUrl = "AddProduct.aspx?cateId="+document.getElementById('btnAddProduct').getAttribute("cateid");
            window.open(strUrl,"Product","statusbar=yes,width=600px,height=550px,top=0,left=0");
        }

a,b 都可以取得此值

原文地址:https://www.cnblogs.com/vihone/p/1629929.html