Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Value and Text properties in asp.net TextBox (Value gets overwritten by Text )

I have TextBox like below.

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

in code behind.

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 am keeping text property I am not able to access Value field. How to get the value field when text property is present?

like image 842
Jay Avatar asked Oct 25 '13 15:10

Jay


People also ask

Which attribute of TextBox server control is used to get the value that is typed in it?

The TextBox server control is an input control that lets the user enter text. By default, the TextMode property of the control is set to TextBoxMode.

What is the text property of TextBox?

TextBox PropertiesTextAlign– for setting text alignment. ScrollBars– for adding scrollbars, both vertical and horizontal. Multiline– to set the TextBox Control to allow multiple lines. MaxLength– for specifying the maximum character number the TextBox Control will accept.

What is the use of TextBox in asp net?

The TextBox control is used to create a text box where the user can input text.

What is value asp net?

Asp.Net Code document.getElementById('<%= txt_id.ClientID %>').value; The value property sets or returns the value of the value attribute of a text field. It contains the default value OR the value a user types in.


1 Answers

Don't use the value property. If you are using asp.net's TextBox you must use Text.

When you add properties that don't exists in the TextBox class, asp.net will render those properties to the resulting html. So

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

Will render to something like this

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

If you omit the TextBox's Text property and write the value property, then the value property will be rendered.

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

To

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

TextBox doesn't has a Value property. When the TextBox instance is created, the HTML value property will be assigned to the Text property, and that's why you access the Text property it has the "aaaa" value.

Summary: Don't use value property when you use ASP.NET controls. Use the controls specific properties.

like image 180
Agustin Meriles Avatar answered Oct 06 '22 01:10

Agustin Meriles