Default value of textbox

2019-02-25 14:38发布

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.

5条回答
疯言疯语
2楼-- · 2019-02-25 15:03

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
查看更多
我想做一个坏孩纸
3楼-- · 2019-02-25 15:07

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()%>'
查看更多
戒情不戒烟
4楼-- · 2019-02-25 15:11

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>
查看更多
三岁会撩人
5楼-- · 2019-02-25 15:18

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

var val1 = Number(document.getElementById("TextBox1").value);
查看更多
劫难
6楼-- · 2019-02-25 15:19

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

查看更多
登录 后发表回答