如何在CS中获得一个被javascript改变过的Label的Text

zengxie 2008-06-30 10:44:18
如何在CS中获得一个被javascript改变过的Label的Text
比如一个Label的初始值已经在界面上设置好了,比如下面代码,我先改变Label1的值,在CS中如何得到Label1的Text

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
<script type="text/javascript">
function changeValue()
{
document.getElementById("Label1").innerHTML="aaaaa";
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
<input id="Button1" type="button" value="客户端改变Label1的值" onclick="changeValue()" />
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="服务器端得到label的值" />
</div>
</form>
</body>
</html>




public partial class coolMenu : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button2_Click(object sender, EventArgs e)
{
string aa = this.Label1.Text.ToString();//这里要得到Label1被改变后的值(是先点击Button1改变Text,再点击Button2得到Text)

}
}



...全文
409 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
stenvenhao 2009-12-10
  • 打赏
  • 举报
回复
我要。邮箱:cuilulumvp@126.com
另附:那个前台lable赋值。后台获取的方法怎么实现的。请一并发到我的邮箱理,感谢啊。
brz97 2008-07-01
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 ouyang532 的回复:]
建议你用input type=text runat="server",这样就可以取到了,用label取不到
[/Quote]
事实上你用TextBox也是取不到的
因为javascript改变的内容跟直接输入是不一样的
ouyang532 2008-07-01
  • 打赏
  • 举报
回复
建议你用input type=text runat="server",这样就可以取到了,用label取不到
zengxie 2008-07-01
  • 打赏
  • 举报
回复
控件做好了,需要这个控件的给我留言,控件的效果是象DropDownList但不是出现下拉菜单,而是把整个屏幕刷黑,弹出高亮显示层,选择后屏幕还原
Navymk 2008-06-30
  • 打赏
  • 举报
回复
<input id="Button1" type="button" value="客户端改变Label1的值" onclick="changeValue()" runat="server" />

后台脚本取Button1.value
sunpowerkill 2008-06-30
  • 打赏
  • 举报
回复
label是web控件,你可以用html控件呀,
比如:
html:
<script type="text/javascript">
function changeValue()
{
document.getElementById("Label1").innerHTML="aaaaa";
}
</script>
//这里换成input控件
<input id="Label1" name="Label1" type="hidden" runat=server/>

<input id="Button1" type="button" value="客户端改变Label1的值" onclick="changeValue()" />
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="服务器端得到label的值" />
cs:
在后台文件里要声明(一般系统会自动生成)
系统:protected System.Web.UI.HtmlInputHidden Label1;

手动:HtmlInputHidden Label1;

string aa = Request.From["Label1"];

string aa = Label1.Value;
lihuinihao6315 2008-06-30
  • 打赏
  • 举报
回复
innerHTML改成INNERTEXT, 用ajax可以。不用ajax,則 調用 lable change 事件(不知有沒有) 改變lable值時。將值放到 隱藏欄中。
BUTTON 點 後 將隱藏欄值放回 代碼。
hubblebubblepig 2008-06-30
  • 打赏
  • 举报
回复
好像需要把改变后的值与后台进行交互才能得到 不然点了Button2后页面重新加载 这个值就刷没有了 得用ajax吧
hubblebubblepig 2008-06-30
  • 打赏
  • 举报
回复
先说点儿别的
Label的text和那个js方法里面的innerHTML不是对应的关系吧好像
还有这个label是服务器端的控件 你要确定一下它在页面形成的代码的id是否仍然是"Label1" 之后才能getElementById
carl974 2008-06-30
  • 打赏
  • 举报
回复
楼上的说得很对,肯定可以实现
brz97 2008-06-30
  • 打赏
  • 举报
回复
这个怎么说呢....
不知道楼主是否能得到这个Label对象
我记得获得服务器端控件必须要这样写


<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
<script type="text/javascript">
function changeValue()
{
var lab = document.getElementById("<%=Label1.ClientID%>");
.....
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
<input id="Button1" type="button" value="客户端改变Label1的值" onclick="changeValue()" />
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="服务器端得到label的值" />
</div>
</form>
</body>
</html>



其次服务器端控件的值是靠ViewState来维持的
后台在读取数据时,实际侍从ViewState中读取的
所以只要ViewState不变,值就不变
也就是说,如果楼主没有在后台改变过Label1的值,那么Label1的值就永远是默认的"Label1"
如果楼主一定要获得改变后的值,可以尝试变通一下


<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>无标题页</title>
<script type="text/javascript">
function changeValue()
{
var lab = document.getElementById("<%= Label1.ClientID%>");
lab.innerText = 'aaaaaaaa';
var txt = document.getElementById('Label1Text');
txt.value = lab.innerText;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
<input type="hidden" name="Label1Text" id="Label1Text" />
<input id="Button1" type="button" value="客户端改变Label1的值" onclick="changeValue()" />
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="服务器端得到label的值" />
</div>
</form>
</body>
</html>


后台代码


protected void Button2_Click(object sender, EventArgs e)
{
string str = Request.Form["Label1Text"];
}



zengxie 2008-06-30
  • 打赏
  • 举报
回复
以上答案都不行,其实我做的是一个复合控件,里面有一个<TB>这个复合控件的TEXT在初始时是显示在<TB>上,但在使用控件时会用JAVASCRIPT改变<TB>的值,但在
后台只能用这个复合控件的TEXT来取值

62,241

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧