如何格式化文本框以显示“Dist / 0000001”
问题描述:
<asp:TextBox ID="TextBox1" runat="server">
<asp:TextBox ID="TextBox2" runat="server" AutoPostBack="True"
ontextchanged="TextBox2_TextChanged">
<asp:TextBox ID="TextBox3" runat="server">
C#
C#
protected void TextBox2_TextChanged(object sender, EventArgs e)
{
TextBox3.Text = TextBox1.Text + "/" + TextBox2.Text;
}
问题
我希望TextBox3显示例如 Dist / 0000001NOTDist / 1如果在TextBox1中输入Dist并且在TextBox2中输入1。
请帮助我。
QUESTION
I would like TextBox3 to display for example "Dist/0000001" NOT "Dist/1" if Dist is entered in TextBox1 and 1 entered in TextBox2.
Help me Please.
答
验证TextBox2中的文本是否为数字,如果是,则将其转换为数字类型(如整数),然后使用自定义格式模型返回字符串。请考虑以下代码
Validate that the text in TextBox2 is a number and if it is, convert it to a numeric type such as integer and then back to string using custom format model. Consider the following code
string numberText = "1";
int numberInt;
if (int.TryParse(numberText, out numberInt)) {
System.Diagnostics.Debug.WriteLine(numberInt.ToString("###0000000"));
} else {
System.Diagnostics.Debug.WriteLine("Input value was not an integer");
}
有关格式字符串的更多信息,请参阅自定义数字格式字符串 [ ^ ]
总的来说,你可能会有像
这样的东西
For more information about the format string, refer to Custom Numeric Format Strings[^]
So in overall you could have something like
int numberInt;
if (int.TryParse(TextBox2.Text, out numberInt)) {
TextBox3.Text = string.Format("{0}/{1}", TextBox1.Text, numberInt.ToString("###0000000"));
} else {
// inform the user about invalid input
}