如何在文本框中创建发票编号并在按钮单击时自动增加它?

时间:2016-05-06 07:37:44

标签: c# sql-server web-applications

我使用的是Asp.net(C#)和SQL Server。在Web应用程序中,我想在文本框中输入5位数的发票号,并在点击按钮后将gridviews数据保存在数据库中,然后文本框中的发票号应该加1 ......依此类推。我用过这段代码=>

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,并且进一步增量不起作用。

有人对此有任何想法吗?

谢谢&的问候,

5 个答案:

答案 0 :(得分:4)

我怀疑这甚至会编译,因为在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");
    }
}

此外,您实际上不会存储格式化的数字。只需将数字存储为整数,然后使用ToStringformat-specifiers.

在事件代码中进行格式化

答案 1 :(得分:1)

您正在寻找格式,这在您的情况下非常简单:

   //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转换为属性并隐藏格式:

   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;
   } 

答案 2 :(得分: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");
        }

    }

答案 3 :(得分:0)

试试这个:

    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")
    }
}

答案 4 :(得分:0)

您需要在增量后仅格式化结果编号。

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

如果 ID = 7 你的结果将在增量之后 将 00008