Reputation: 191
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.
Upvotes: 7
Views: 56386
Reputation: 1
Converting a String into a Number - in your case the value available in textbox.
var val1 = Number(document.getElementById("TextBox1").value);
Upvotes: 0
Reputation: 1
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()%>'
Upvotes: 0
Reputation: 107
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>
Upvotes: 1
Reputation: 655
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
Upvotes: 0
Reputation: 103358
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
Upvotes: 18