如何格式化文本框以显示“Dist / 0000001” [英] HOW TO FORMAT A TEXTBOX TO DISPLAY "Dist/0000001"

查看:86
本文介绍了如何格式化文本框以显示“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
}


这篇关于如何格式化文本框以显示“Dist / 0000001”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆