ASP.Net TextBox 只讀(ReadOnly)時後臺不能賦值取值

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">	</span><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">當把ASP.Net的自帶控件TextBox設置爲只讀時,後臺無法正常取值(取到的結果爲“”)。解決辦法如下:</span>


1. 不設置ReadOnly屬性,添加事件onfocus=this.blur()

<asp:TextBox ID="TextBox1" runat="server" onfocus=this.blur()></asp:TextBox> 

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

string Text = Request.Form["TextBox1"].Trim(); 

3、在Page_Load()正設置文本框的只讀屬性,能正常讀取,如下:

protected void Page_Load(object sender, EventArgs e)  
{  
    if (!Page.IsPostBack)  
    {  
        TextBox1.Attributes.Add("readonly","true");  
    }  
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章