asp:TextBox的属性设为ReadOnly="true",客户端修改的不能提交到服务器端?

haisheng 2011-02-28 11:24:26
页面上有一个TextBox,名字tbAmount,它的值是其他的TextBox相加后得到的。
所以,我想达到这样的效果:tbAmount的值用户不能修改,用户填了其他TextBox后把值赋给tbAmount。

我把TextBox的属性设为ReadOnly="true",然后在javascript中,加上document.getElementById("tbAmount").value = ASSIST1 + ASSIST2 + ASSIST3; 其中ASSIST1、ASSIST2、ASSIST3是其他TextBox的值。
在服务器端,读tbAmount的值却是空。
就是如果TextBox的属性ReadOnly="true",客户端修改的东西,是无法提交给服务器端的。

请问,用户不能修改TextBox的值,在javascript会为TextBox赋值,服务器能够读到这个值,如何达到这种效果?
...全文
593 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
haisheng 2011-03-01
  • 打赏
  • 举报
回复
见http://topic.csdn.net/u/20110228/13/166e1190-5acd-42e2-aa8d-79f12b456600.html
haisheng 2011-03-01
  • 打赏
  • 举报
回复
enabled="false"更不行
笨妞妞 2011-02-28
  • 打赏
  • 举报
回复
function Compute()
{

var sumValue=0;

var arrayValue = new Array("JuheInWater","JiangjunguanInWater","RuheInWater","JinjiheInWater");

for(var i=0;i<arrayValue.length;i++)
{
if(document.getElementById(arrayValue[i]).value!="")
sumValue+=parseFloat(document.getElementById(arrayValue[i]).value);
else
{
if(sumValue==0)
{
sumValue="";
}
else
{
sumValue=sumValue;
}
}
}
document.getElementById("InWater").value = sumValue;
}

<tr>
<td class="lanzi" height="32" style="width: 21%">
入水量(万m<sup>3</sup>)</td>
<td class="heizi" align="left" style="width: 190px"><asp:textbox id="in" runat="server" style="ime-mode:disabled;" MaxLength=4></asp:textbox><span style="color: #ff0000">*</span></td>
<td class="lanzi" height="32" style="width: 21%">
出水量(万m<sup>3</sup>)</td>
<td class="heizi" align="left" style="font-size: 12pt"><asp:textbox id="out" runat="server" style="ime-mode:disabled;" MaxLength=4></asp:textbox></td>
</tr>
<tr style="font-size: 12pt">
<td class="lanzi" height="32" style="width: 21%">
入水量1(万m<sup>3</sup>)</td>
<td align="left" class="heizi" style="width: 190px">
<asp:TextBox ID="in1" runat="server" style="ime-mode:disabled;" onblur="Compute();" MaxLength=4></asp:TextBox><span style="color: #ff0000">*</span></td>
<td class="lanzi" height="32" style="width: 21%">
入水量2(万m<sup>3</sup>)</td>
<td align="left" class="heizi">
<asp:TextBox ID="in2" runat="server" style="ime-mode:disabled;" onblur="Compute();" MaxLength=4></asp:TextBox><span style="color: #ff0000">*</span></td>
</tr>
<tr style="font-size: 12pt">
<td class="lanzi" height="32" style="width: 21%">
入水量3(万m<sup>3</sup>)</td>
<td align="left" class="heizi" style="width: 190px">
<asp:TextBox ID="in3" runat="server" style="ime-mode:disabled;" onblur="Compute();" MaxLength=4></asp:TextBox><span style="color: #ff0000">*</span></td>
<td class="lanzi" height="32" style="width: 21%">
入水量4(万m<sup>3</sup>)</td>
<td align="left" class="heizi">
<asp:TextBox ID="in4" runat="server" style="ime-mode:disabled;" onblur="Compute();" MaxLength=4></asp:TextBox><span style="color: #ff0000">*</span></td>
</tr>
a260881071 2011-02-28
  • 打赏
  • 举报
回复
你把这个textbox width=0就可以了。或者你可以用一个hiddenvalue隐藏控件来代替。
cykb518 2011-02-28
  • 打赏
  • 举报
回复
哈哈 以前也常遇到这种问题
解决办法是 在cs文件的page_load事件中加上这句话


protected void Page_Load(object sender, EventArgs e)
{
this.TextBox1.Attributes.Add("ReadOnly","true");
}

gdjlc 2011-02-28
  • 打赏
  • 举报
回复
不要直接在源代码中设置,改为在.cs代码中设置TextBox的属性
tbAmount.Attributes["readonly"] = "true";
ENetLive 2011-02-28
  • 打赏
  • 举报
回复
这个在ASP.Net MVC 中可以[Quote=引用 7 楼 kissesyan 的回复:]

textbox 设置为ReadOnly="true"
服务端 Request.Form[tbAmount.ClientID]
可以读取数据
[/Quote]
PitTroll 2011-02-28
  • 打赏
  • 举报
回复
用这个属性contenteditable="false" autoComplete="Off" BackColor="#cccccc"试试。
Rock870210 2011-02-28
  • 打赏
  • 举报
回复
和ReadOnly没有关系。在保存前,在JS端检查
document.getElementById("tbAmount")
ASSIST1
ASSIST2
ASSIST3
这个是否为null或undefine
kissesyan 2011-02-28
  • 打赏
  • 举报
回复
textbox 设置为ReadOnly="true"
服务端 Request.Form[tbAmount.ClientID]
可以读取数据
wangchangming 2011-02-28
  • 打赏
  • 举报
回复
你可以在浏览器的提交信息中通过Request.Form[""]获取.
如:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function Add() {
var i1 = document.getElementById("TextBox1").value;
var i2 = document.getElementById("TextBox2").value;
document.getElementById("txtBox1").value = i1 + i2;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtBox1" runat="server" ReadOnly="true"></asp:TextBox>
<asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server" ></asp:TextBox>
<input type="button" id="btn1" name="btn1" value="Add" onclick="Add()" />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />

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

protected void Button1_Click(object sender, EventArgs e)
{
string s = Request.Form["txtBox1"];
Response.Write(s);
}
仅此而已 2011-02-28
  • 打赏
  • 举报
回复
问题:asp:TextBox的属性设为ReadOnly="true",客户端修改的不能提交到服务器端?

可以提交到服务器的,以前做过一个也是要记录的,有设置为ReadOnly="true",

客户端提交正常,调试下看看是不是哪里赋值有问题?
xmlife 2011-02-28
  • 打赏
  • 举报
回复
document.getElementById("<%=tbAmount.ClientID %>").value
=document.getElementById("<%=ASSIST1.ClientID %>").value+document.getElementById("<%=ASSIST2.ClientID %>").value+document.getElementById("<%=ASSIST3.ClientID %>").value
kissesyan 2011-02-28
  • 打赏
  • 举报
回复
是 enabled="false"
kissesyan 2011-02-28
  • 打赏
  • 举报
回复
enabled="true"
用这个试下
子夜__ 2011-02-28
  • 打赏
  • 举报
回复
"<%=tbAmount.ClientID %>" )

改成这样

document.getElementById("<%=tbAmount.ClientID %>" ).value

是在不行就用input吧 HTML控件要好些。
孟子E章 2011-02-28
  • 打赏
  • 举报
回复
  • 打赏
  • 举报
回复
你中间用个隐藏控件不就万事大吉了
一路奔跑1314 2011-02-28
  • 打赏
  • 举报
回复
readonly表示控件为只读,你先定义了该属性,你textbox控件的值就不能改了
用readonly也可以,就是别在页面定义改属性
而是在后台,textbox赋值后在textbox.readonly=true
一路奔跑1314 2011-02-28
  • 打赏
  • 举报
回复
用这个enabled="false"啦
加载更多回复(5)

62,046

社区成员

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

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

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

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