Default value of textbox

2019-02-25 15:10发布

问题:

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.

回答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


回答2:

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



回答3:

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>


回答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()%>'


回答5:

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

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