【问题标题】:How to create Invoice Number in Text box and auto increment it on button click?如何在文本框中创建发票编号并在单击按钮时自动递增?
【发布时间】:2016-09-01 04:53:38
【问题描述】:

我正在使用 Asp.net(C#) 和 SQL Server。在 Web 应用程序中,我想要文本框中的 5 位数发票编号,单击按钮后,gridviews 数据将保存在数据库中,然后文本框中的发票编号应增加一……等等。我用了这段代码=>

protected void Page_Load(object sender, EventArgs e)
{
    double ID = 00001;
    txtinvoiceno.Text = Convert.ToString(ID);

和

protected void btnsavef1_Click(object sender, EventArgs e)
{
    ID++;
    var no = Convert.ToInt32(ID);
    txtinvoiceno.Text = Convert.ToString(no);
}

但它在输出中仅显示单个数字 1,并且仅增加一次,即 2,并且进一步增加不起作用。

有人对此有任何想法吗??

感谢和问候,

【问题讨论】:

  • 改用INT 数据类型...

标签: c# sql-server web-applications


【解决方案1】:

试试这个:

public partial class WebForm1 : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
           if (Page.IsPostBack == false)
           { 
               int ID = 1; 
               txtinvoiceno.Text = ID.ToString("D5");
             }
        }

        protected void btnsavef1_Click(object sender, EventArgs e)
        {
            int ID = Convert.ToInt16(txtinvoiceno.Text );
            ID++;
            txtinvoiceno.Text = ID.ToString("D5");
        }

    }

【讨论】:

    【解决方案2】:

    您正在寻找格式化,在您的情况下这很容易:

       //DONE: int, not double - do you want to deal with round-up errors?
       //DONE: ID should be a field, not a local variable
       private int ID = 0;
    
       protected void Page_Load(object sender, EventArgs e)
       {
           // whenever you want 5 digits - just put 5 zeroes when formatting
           txtinvoiceno.Text = ID.ToString("00000");
           ...
       } 
    
       protected void btnsavef1_Click(object sender, EventArgs e)
       {
           ID += 1;
           // once again: want 5 digits - put 5 zeroes when formatting
           txtinvoiceno.Text = ID.ToString("00000");
       }
    

    恕我直言,更好的选择是将 ID 转换为 property 并在此处隐藏格式:

       private int m_ID;
    
       public int ID {
         get {
           return m_ID;
         }
         set {
           m_ID = value;
           txtinvoiceno.Text = m_ID.ToString("00000"); 
         }
       }      
       ...
       protected void Page_Load(object sender, EventArgs e) {
         // Just assign
         ID = 0;
         ... 
       }
    
       protected void btnsavef1_Click(object sender, EventArgs e) {
         // Just increment 
         ID += 1;
       } 
    

    【讨论】:

    • 好主意。允许始终自动将“ID”与其文本字段同步。
    • 我使用了这段代码,但是在 000002 之后它仍然没有增加。它只是第一次增加..
    • @chirag:我无法重现错误;需要更多代码:您是否尝试在例程的其他部分更改/递增/递减ID?
    • 看,先生,当 http 网页是无状态的时,当我点击按钮时它第一次增加,然后当页面回发时它得到它的初始值,即 1。因此它不能移动到 3 然后4等等……这就是我遇到的问题……你有什么解决办法吗??
    【解决方案3】:

    您只需要格式化递增后的结果编号。

    protected void btnsavef1_Click(object sender, EventArgs e)
    {
      ID++;
      var no = Convert.ToInt32(ID);
      txtinvoiceno.Text = no.ToString().PadLeft(5, '0');
    }
    

    如果 ID=7 你的结果将在 Increment 之后 将是 00008

    【讨论】:

      【解决方案4】:

      试试这个:

          public partial class Form2 : Form
          {
              private double ID=0;
              public Form2()
              {
                    InitializeComponent();
              }
              protected void Page_Load(object sender, EventArgs e)
               {
               double ID = 00001;
               txtinvoiceno.Text = Strings.Format(ID, "000000")
              }
      
         protected void btnsavef1_Click(object sender, EventArgs e)
         {
           ID++;
           var no = Convert.ToInt32(ID);
            txtinvoiceno.Text = Strings.Format(no, "000000")
          }
      }
      

      【讨论】:

      • 亲爱的,Beldi 先生,您的回答是正确的,但是您给出了关于窗口应用的回答。我正在开发网络应用程序...
      • 是的,但是一样,你只是复制了程序!
      • 看,先生,当 http 网页是无状态的时,当我点击按钮时它第一次增加,然后当页面回发时它得到它的初始值,即 1。因此它不能移动到 3 然后4等等……这就是我遇到的问题……你有什么解决办法吗??
      • 是的,我有你的解决方案,看看我的第二个答案,如果解决了你的问题,请标记答案
      【解决方案5】:

      我怀疑这甚至会编译,因为在btnsavef1_Click 中,变量ID 没有定义。您可能希望 ID 成为实例变量:

      class MyClass {
          private int ID = 0;
      
          protected void Page_Load(object sender, EventArgs e)
          {
              txtinvoiceno.Text = this.ID.ToString("D5");
          }
      
          protected void btnsavef1_Click(object sender, EventArgs e)
          {
              this.ID++;
              txtinvoiceno.Text = this.ID.ToString("D5");
          }
      }
      

      此外,您实际上不会存储格式化的数字。只需将数字存储为整数,然后使用 ToString 和 format-specifiers. 在事件代码中进行格式化

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-02-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-17
        • 2012-08-11
        • 2019-05-10
        相关资源
        最近更新 更多