js给C#控件赋值

前台代码

 

Html代码 复制代码
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title>无标题页</title>  
  8.     <script type="text/javascript">  
  9.     function fz(){   
  10.         var std = "2";   
  11.         alert(std);   
  12.         document.getElementById('HiddenField1').value=std;   
  13.     }   
  14.     </script>  
  15. </head>  
  16. <body>  
  17.     <form id="form1" runat="server">  
  18.     <div>  
  19.     项目中有个小功能,在Js中修改一个服务器标签的值,如 document.getElementById("lblClothIndex").innerHTML = result;(比如由“1”修改成“2”),可是在后台通过this.lblClothIndex.Text获得的还是"1"这个老值。后来发现通过调用隐藏控件的方法可以获得最新值,代码如下:   
  20.   
  21.         1. 添加一个隐藏控件  <asp:HiddenField ID="HiddenField1" runat="server" Value="" />  
  22.            
  23.         <asp:TextBox ID="TextBox1" runat="server">0</asp:TextBox>  
  24.             <a href="#" onclick="fz()">赋值</a>  
  25.   
  26.         <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />  
  27.  2. 用JS为服务器标签赋值的时候也给隐藏控件赋值    
  28.         3. 在后台通过 this.HiddenField1.Value代码获得最新值   
  29.   
  30.     </div>  
  31.     </form>  
  32. </body>  
  33. </html>  
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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 fz(){
        var std = "2";
        alert(std);
        document.getElementById('HiddenField1').value=std;
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    项目中有个小功能,在Js中修改一个服务器标签的值,如 document.getElementById("lblClothIndex").innerHTML = result;(比如由“1”修改成“2”),可是在后台通过this.lblClothIndex.Text获得的还是"1"这个老值。后来发现通过调用隐藏控件的方法可以获得最新值,代码如下:

        1. 添加一个隐藏控件  <asp:HiddenField ID="HiddenField1" runat="server" Value="" />
        
        <asp:TextBox ID="TextBox1" runat="server">0</asp:TextBox>
            <a href="#" οnclick="fz()">赋值</a>

        <asp:Button ID="Button1" runat="server" οnclick="Button1_Click" Text="Button" />
 2. 用JS为服务器标签赋值的时候也给隐藏控件赋值 
        3. 在后台通过 this.HiddenField1.Value代码获得最新值

    </div>
    </form>
</body>
</html>

 

后台代码

 

C#代码 复制代码
  1. using System;   
  2. using System.Collections;   
  3. using System.Configuration;   
  4. using System.Data;   
  5. using System.Linq;   
  6. using System.Web;   
  7. using System.Web.Security;   
  8. using System.Web.UI;   
  9. using System.Web.UI.HtmlControls;   
  10. using System.Web.UI.WebControls;   
  11. using System.Web.UI.WebControls.WebParts;   
  12. using System.Xml.Linq;   
  13.   
  14. public partial class _Default : System.Web.UI.Page   
  15. {   
  16.     protected void Page_Load(object sender, EventArgs e)   
  17.     {   
  18.            
  19.     }   
  20.     protected void Button1_Click(object sender, EventArgs e)   
  21.     {   
  22.         TextBox1.Text = HiddenField1.Value;   
  23.   
  24.     }   
  25. }  

转自:http://www.ok22.org/art_detail.aspx?id=75

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值