簡體   English   中英

如何在文本框中創建發票編號並在按鈕單擊時自動增加它?

[英]How to create Invoice Number in Text box and auto increment it on button click?

我使用的是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,並且僅在時間ie2遞增它,並且進一步增量不起作用。

任何人都對此有任何想法?

感謝和問候,

我懷疑這甚至會編譯,因為在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在事件代碼中進行格式化。

你正在尋找格式化 ,這在你的情況下很容易:

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

試試這個 :

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

    }

試試這個 :

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

您需要在增量后僅格式化結果編號。

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

如果ID = 7,您的結果將在增量為00008之后

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM