Home > Article > Backend Development > ASP.Net TextBox cannot obtain value through background assignment when it is read-only
Today when I was developing an asp.net page, I encountered a situation where the ReadOnly attribute was set in the TextBox. After assigning a value in js, the background code could not get the value. After searching on the Internet, I found several solutions.
Collect it.
1. Do not set ReadOnly, set onfocus=this.blur()
C# code
The text box remains gray, but the content cannot be modified manually. You can pass it in the background The Text attribute is assigned and obtained normally
2. After setting the ReadOnly attribute, obtain the value through Request, as follows:
Front-end code:
Backend code:
string Text = Request.Form[" TextBox1"].Trim();
string Text = Request.Form["TextBox1"].Trim();
3. The read-only attribute of the text box is being set in Page_Load(), and it can be read normally, as follows:
C# Code
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
TextBox1.Attributes.Add("readonly","true");
}
}