js assigns a value to asp:TextBox, and the problem that the value cannot be obtained in the background is solved

When developing an asp.net page, I encountered the situation that the TextBox set the ReadOnly attribute. After assigning a value in js, the background code could not get the value. After searching on the Internet, I found a few solutions.

Bookmark it.

1. Do not set ReadOnly, set οnfοcus=this.blur()
C# code 
<asp:TextBox ID="TextBox1" runat="server" οnfοcus=this.blur()></asp:TextBox> < 
asp:TextBox ID= "TextBox1" runat="server" οnfοcus=this.blur()></asp:TextBox> The
text box remains gray, but the content cannot be manually modified, and the value can be normally assigned and retrieved through the Text property in the background

2. After setting the ReadOnly attribute, get the value through Request, as follows:
Front desk code:
<asp:TextBox ID="TextBox1" runat="server" ReadOnly="True"></asp:TextBox> 
<asp:TextBox ID ="TextBox1" runat="server" ReadOnly="True" ></asp:TextBox>
Code behind:
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(), which can be read normally, as follows:
C# code 
protected void Page_Load(object sender, EventArgs e)  
{  
    if (!Page.IsPostBack)  
    {  
        TextBox1.Attributes.Add(" readonly","true");  
    }  
}

Guess you like

Origin blog.csdn.net/LookingTomorrow/article/details/126959077