ASP.Net TextBox唯讀時不能通過後台賦值取值

來源:互聯網
上載者:User

標籤:blog   http   ar   os   sp   for   java   strong   on   

給頁面的TextBox設定ReadOnly="True"時,在後台代碼中不能賦值取值,下邊幾種方法可以避免:

1、不設定ReadOnly,設定onfocus=this.blur()

C#代碼
  1. <asp:TextBox ID="TextBox1" runat="server" onfocus=this.blur()></asp:TextBox>  
[c#] view plaincopyprint?
  1. <asp:TextBox ID="TextBox1" runat="server" onfocus=this.blur()></asp:TextBox>  
<asp:TextBox ID="TextBox1" runat="server" onfocus=this.blur()></asp:TextBox>


文字框不變灰色,但也無法手動修改內容,可以在後台通過Text屬性正常賦值取值

2、設定了ReadOnly屬性後,通過Request來取值,如下:

前台代碼:
C#代碼
  1. <asp:TextBox ID="TextBox1" runat="server" ReadOnly="True" ></asp:TextBox>  
[c#] view plaincopyprint?
  1. <asp:TextBox ID="TextBox1" runat="server" ReadOnly="True" ></asp:TextBox>  
<asp:TextBox ID="TextBox1" runat="server" ReadOnly="True" ></asp:TextBox>


後台代碼:

C#代碼
  1. string Text = Request.Form["TextBox1"].Trim();  
[c#] view plaincopyprint?
  1. string Text = Request.Form["TextBox1"].Trim();  
string Text = Request.Form["TextBox1"].Trim();


3、在Page_Load()正設定文字框的唯讀屬性,能正常讀取,如下:

C#代碼
  1. protected void Page_Load(object sender, EventArgs e)   
  2. {   
  3.     if (!Page.IsPostBack)   
  4.     {   
  5.         TextBox1.Attributes.Add("readonly","true");   
  6.     }   
  7. }  

ASP.Net TextBox唯讀時不能通過後台賦值取值

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.