页面上有一个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赋值,服务器能够读到这个值,如何达到这种效果?

解决方案 »

  1.   

    "<%=tbAmount.ClientID %>" ) 改成这样document.getElementById("<%=tbAmount.ClientID %>" ).value是在不行就用input吧  HTML控件要好些。
      

  2.   

    document.getElementById("<%=tbAmount.ClientID %>").value
    =document.getElementById("<%=ASSIST1.ClientID %>").value+document.getElementById("<%=ASSIST2.ClientID %>").value+document.getElementById("<%=ASSIST3.ClientID %>").value
      

  3.   

    问题:asp:TextBox的属性设为ReadOnly="true",客户端修改的不能提交到服务器端?可以提交到服务器的,以前做过一个也是要记录的,有设置为ReadOnly="true",客户端提交正常,调试下看看是不是哪里赋值有问题?
      

  4.   

    你可以在浏览器的提交信息中通过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);
            }  
      

  5.   

    textbox 设置为ReadOnly="true"
    服务端 Request.Form[tbAmount.ClientID]
    可以读取数据
      

  6.   

    和ReadOnly没有关系。在保存前,在JS端检查
    document.getElementById("tbAmount") 
    ASSIST1
    ASSIST2
    ASSIST3
    这个是否为null或undefine
      

  7.   

    用这个属性contenteditable="false" autoComplete="Off" BackColor="#cccccc"试试。
      

  8.   

    这个在ASP.Net MVC 中可以
      

  9.   

    不要直接在源代码中设置,改为在.cs代码中设置TextBox的属性
    tbAmount.Attributes["readonly"] = "true"; 
      

  10.   

    哈哈 以前也常遇到这种问题
    解决办法是 在cs文件的page_load事件中加上这句话
    protected void Page_Load(object sender, EventArgs e)
            {
                this.TextBox1.Attributes.Add("ReadOnly","true");
            }
      

  11.   

    你把这个textbox width=0就可以了。或者你可以用一个hiddenvalue隐藏控件来代替。
      

  12.   

    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>
      

  13.   

    不要设置 TextBox 的 ReadOnly="True"  
    它在获取值的时候 会判断 ReadOnly 属性,如果为 true 是不会获取新值的
     TextBoxID.Attributes.Add("readonly","true");
      

  14.   


    Readonly是服务器控件属性,readonly是JS的,这样就可以获取修改后的值。
      

  15.   

    readonly表示控件为只读,你先定义了该属性,你textbox控件的值就不能改了
    用readonly也可以,就是别在页面定义改属性
    而是在后台,textbox赋值后在textbox.readonly=true
      

  16.   

    本帖最后由 net_lover 于 2011-02-28 19:34:31 编辑
      

  17.   

    见http://topic.csdn.net/u/20110228/13/166e1190-5acd-42e2-aa8d-79f12b456600.html