When the TextBox has set ReadOnly="true", if a value is added to the control in the foreground, the background cannot be retrieved, and the value is "empty"
The principle is not clear, and it is hard to explain what Microsoft is thinking. However, sometimes we need to fill in the value through the front desk script, and we do not want the user to modify the content of their controls, which is more embarrassing.
At first I changed it to <input type="text" readonly="readonly" > in Html, but later I found that the workload was very large, so I searched online and couldn't find out the reason why the TextBox ReadOnly="true" page fill value could not be obtained, but the problem was still solved.
text:
For the specification and security of page code in vs2005, my personal opinion is to use the second method
If you know why the value principle cannot be obtained after TextBox has set the ReadOnly property, please explain it, thank you!
Under .NET 2.0, when a TextBox on the page sets the property ReadOnly="True", after assigning a value to it through a client script, accessing its Text property in the background code but failing to obtain the value. After trying, I found that this problem can be solved in the following ways:
Method 1: Do not set the ReadOnly property, simulate it by onfocus=(), as follows:
<asp:TextBox ID="TextBox1" runat="server" onfocus=()></asp:TextBox>
In this case, the text box is immediately lost when it gains focus, so it cannot manually modify its content. It can simulate ReadOnly, and can also use the Text attribute in the background code to obtain the values set by the script normally;
Method 2: After setting the ReadOnly property, the value is obtained through Request, as follows:
Front Desk Code:
<asp:TextBox ID="TextBox1" runat="server" ReadOnly="True" ></asp:TextBox>
Background code:
string Text = ["TextBox1"].Trim();
Method 3: The read-only property of the text box is set in Page_Load(), and it is not set in the foreground. It can be read normally, as follows:
protected void Page_Load(object sender, EventArgs e)
{
if (!)
{
("readonly","true");
}
}
The principle is not clear, and it is hard to explain what Microsoft is thinking. However, sometimes we need to fill in the value through the front desk script, and we do not want the user to modify the content of their controls, which is more embarrassing.
At first I changed it to <input type="text" readonly="readonly" > in Html, but later I found that the workload was very large, so I searched online and couldn't find out the reason why the TextBox ReadOnly="true" page fill value could not be obtained, but the problem was still solved.
text:
For the specification and security of page code in vs2005, my personal opinion is to use the second method
If you know why the value principle cannot be obtained after TextBox has set the ReadOnly property, please explain it, thank you!
Under .NET 2.0, when a TextBox on the page sets the property ReadOnly="True", after assigning a value to it through a client script, accessing its Text property in the background code but failing to obtain the value. After trying, I found that this problem can be solved in the following ways:
Method 1: Do not set the ReadOnly property, simulate it by onfocus=(), as follows:
Copy the codeThe code is as follows:
<asp:TextBox ID="TextBox1" runat="server" onfocus=()></asp:TextBox>
In this case, the text box is immediately lost when it gains focus, so it cannot manually modify its content. It can simulate ReadOnly, and can also use the Text attribute in the background code to obtain the values set by the script normally;
Method 2: After setting the ReadOnly property, the value is obtained through Request, as follows:
Front Desk Code:
Copy the codeThe code is as follows:
<asp:TextBox ID="TextBox1" runat="server" ReadOnly="True" ></asp:TextBox>
Background code:
Copy the codeThe code is as follows:
string Text = ["TextBox1"].Trim();
Method 3: The read-only property of the text box is set in Page_Load(), and it is not set in the foreground. It can be read normally, as follows:
Copy the codeThe code is as follows:
protected void Page_Load(object sender, EventArgs e)
{
if (!)
{
("readonly","true");
}
}