页面上有一个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赋值,服务器能够读到这个值,如何达到这种效果?
所以,我想达到这样的效果: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赋值,服务器能够读到这个值,如何达到这种效果?
=document.getElementById("<%=ASSIST1.ClientID %>").value+document.getElementById("<%=ASSIST2.ClientID %>").value+document.getElementById("<%=ASSIST3.ClientID %>").value
如:
<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);
}
服务端 Request.Form[tbAmount.ClientID]
可以读取数据
document.getElementById("tbAmount")
ASSIST1
ASSIST2
ASSIST3
这个是否为null或undefine
tbAmount.Attributes["readonly"] = "true";
解决办法是 在cs文件的page_load事件中加上这句话
protected void Page_Load(object sender, EventArgs e)
{
this.TextBox1.Attributes.Add("ReadOnly","true");
}
{
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>
它在获取值的时候 会判断 ReadOnly 属性,如果为 true 是不会获取新值的
TextBoxID.Attributes.Add("readonly","true");
Readonly是服务器控件属性,readonly是JS的,这样就可以获取修改后的值。
用readonly也可以,就是别在页面定义改属性
而是在后台,textbox赋值后在textbox.readonly=true