ASP.NET 2.0裡textbox的Readonly屬性為真時如何取值?

來源:互聯網
上載者:User

readonly為真就是唯讀,也就是你輸入的值根本textbox沒有接受,取出來的當然是空咯。
或者你用其他的方式賦值給textbox和一般取值一樣。

[來源:AppDev-SYSK 118] 有時候,我們不希望使用者直接編輯TextBox,而是希望通過用戶端指令碼的方式來設定內容,一般的做法是設定TextBox的屬性ReadOnly為true。但在ASP.NET 2.0裡有了變化,設定了ReadOnly為true的TextBox,在伺服器端不能通過Text屬性擷取在用戶端設定的新內容,在Reflector裡比較一下LoadPostData的實現

.NET 1.1中,

bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)
{
      string text1 = this.Text;
      string text2 = postCollection[postDataKey];
      if (!text1.Equals(text2))
      {
            this.Text = text2;
            return true;
      }
      return false;
}

.NET 2.0中,

protected virtual bool LoadPostData(string postDataKey, NameValueCollection postCollection)
{
      base.ValidateEvent(postDataKey);
      string text1 = this.Text;
      string text2 = postCollection[postDataKey];
      if (!this.ReadOnly && !text1.Equals(text2, StringComparison.Ordinal))
      {
            this.Text = text2;
            return true;
      }
      return false;
}

就可以看出,如果設定了ReadOnly為true,從用戶端傳回的新的值是不被設定到Text屬性的。

想要保持.NET 1.*中的行為,建議的做法是設定用戶端屬性ContentEditable=false,參考

SYSK 118: ReadOnly or ContentEditable?
http://blogs.msdn.com/irenak/archive/2006/05/03/589085.aspx

其實如果是設定用戶端屬性的話,設定用戶端的readonly屬性應該也是可以的:

TextBox1.Attributes["readonly"] = "true";

SYSK 118: ReadOnly or ContentEditable?
Consider this:  you want a text box on a web page to be not editable by the user, but you want to be able to change the text box’s contents in client side script and see the updated text on the server.

Did you know that if you set TextBox1.ReadOnly = true, the value set by the client side script will not be visible on the server?    Try it for yourself… Here is the code:

<form id="form1" runat="server">
    <div>
        <input id="Button2" type="button" value="Change Text via Client-Side Script" onclick="ChangeText();" />
    </div>
    <asp:TextBox ID="TextBox1" runat="server">initial text</asp:TextBox>
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="See Value on the Server-Side" />
</form>
<script language="javascript" type="text/javascript">
<!--
function ChangeText()
{   
    form1["TextBox1"].setAttribute("innerText", "abc");
}   
-->   
</script>

public partial class MyForm : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {           
        TextBox1.ReadOnly = true;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Write(TextBox1.Text + "<br>");
    }
}

However, if instead of setting TextBox1.ReadOnly property you set ContentEditable attribute to false, you’ll get the behavior you’re looking for:

TextBox1.Attributes["contentEditable"] = "false";

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.