Default value of textbox

How to set value of textbox?

I have this 1 textbox, and i want to set the default value is 0 so that when user not entering anythng my calculation still can.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

This can either be done in the markup (.aspx) like:

<asp:TextBox id="txt" runat="server" Text="0" />

Or in the code-behind (.aspx.cs):

txt.Text = 0;

Or if you’re using VB.NET:

txt.Text = 0

However, if you’re doing a calculation based on this, you shouldn’t rely on there always being a value in the textbox.

You could do:

Dim txtValue as Integer = 0
If IsNumeric(txt.Text) Then
   txtValue = txt.Text
End If

Method 2

If you want to use it with numbers, then you can tell the textbox that it should be with numbers, then “0” will be default.

<asp:TextBox ID="TB_Numbers" TextMode="Number" Text="0" runat="server"></asp:TextBox>

Method 3

As Curt mentioned, you can put default value directly in asp control or in code behind however if you don’t wish to show a 0 in textbox always then once you call the calculation method you can do something like this.

short valueForTextBox1;
if(txt.Text == String.Empty)
{
txt.Text = 0;
valueForTextBox1 = 0;
}
else
{
if(Int16.TryParse(txt.Text, out valueForTextBox1))
{
//Do Calculation
}
else
{
//Show some error that user has entered invalid value.
}
}

This will help the user understand that they left some boxes empty when a zero will appear in them as calculation is called. Hope this helps

Method 4

Just use this on the line of the textbox control to populate your date and time and make not visible if you want 

Value='<%# Now()%>'

Method 5

Converting a String into a Number – in your case the value available in textbox.

var val1 = Number(document.getElementById("TextBox1").value);


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x