Value and Text properties in asp.net TextBox text box (value is replaced with text)

I have a TextBox as shown below.

<asp:TextBox runat="server" ID="Name" value="aaaa" text="bbbb"/> 

in code.

 Dim str As String = Name.Text.Trim() ' value as bbbb 

If I removed the text property.

 <asp:TextBox runat="server" ID="Name" value="aaaa" /> <%--text="bbbb"--%> Dim str As String = Name.Text.Trim() ' value as aaaa 

whenever I save the text property, I cannot access the Value field. How to get a value field with a text property?

+6
source share
3 answers

Do not use the value property. If you are using asp.net TextBox , you must use Text .

When you add properties that do not exist in the TextBox class, asp.net will map these properties to the resulting html. So

 <asp:TextBox runat="server" ID="Name" text="bbbb" mycustomproperty="hi" /> 

Something like this will be displayed

 <input type="text" value="bbbb" id="..." name="..." mycustomproperty="hi"/> 

If you omit the TextBox Text property and write the value property, then the value property will be displayed.

 <asp:TextBox runat="server" ID="Name" value="aaaa" /> 

For

 <input type="text" value="aaaa" id="..." name="..."/> 

TextBox does not have a Value property. When an instance of the TextBox is created, the HTML value property will be assigned to the Text property, and therefore, to access the Text property, it has the value "aaaa".

Summary Do not use the value property when using ASP.NET controls. Use the special properties of controls.

+6
source
0
source

If you are trying to save the data associated with a control on a page, consider using the ASP.NET HiddenField to store values ​​that can be read through feedback on the server, for example:

 <asp:HiddenField runat="server" id="HiddenFieldValue" /> 

Then, in the reverse code, you can get and set the value through the Value property, for example:

 ' Storing value Me.HiddenFieldValue.Value = "value you want to keep" ' Retrieving value Dim str As String = Me.HiddenFieldValue.Value 
0
source

Source: https://habr.com/ru/post/956728/


All Articles