我有在页面加载时动态显示的按钮。假设要做的是:
- 单击动态按钮,显示到文本框
- 点击出票
- 打印票 1。
如果我单击下一个动态按钮,它应该通过相同的过程,它应该打印票 2。
问题是价值没有增加。我相信是因为每次我单击问题票时页面回发,因此将票值重置为 1。无论如何要解决这个问题。
int ticket = 0;
protected void Page_Load(object sender, EventArgs e)
{
string buttonName;
try
{
btnIssueTicket.Enabled = false;
using (SqlConnection connStr = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectString"].ConnectionString))
{
connStr.Open();
SqlCommand select = new SqlCommand("SELECT TransTypeId, TransTypeDesc from tblTransType", connStr);
SqlDataReader reader = select.ExecuteReader();
//Reads all records
while (reader.Read())
{
transID = Convert.ToInt32(reader["TransTypeId"].ToString());
buttonName = reader["TransTypeDesc"].ToString();
CreateButton(buttonName);
}
connStr.Close();
reader.Close();
}
}
catch (Exception ex)
{
lblStatus.Text = "Error: " + ex.Message.ToString();
}
}
}
//Buttons properties. Creates buttons dynamically inside the Layout Panel
private void CreateButton(string buttonName)
{
transbutton = new Button();
transbutton.Text = buttonName;
transbutton.ID = transID.ToString();
transbutton.CssClass = "transButtons"; //CSS property for buttons
transbutton.Click += new EventHandler(transbutton_Click); //Event Handler for dynamic buttons
panelButtons.Controls.Add(transbutton); //Adds button to Layout Panel
}
//When Dynamic buttons clicked.
private void transbutton_Click(object sender, EventArgs e)
{
//Displays Transactions in TextBox
tbList.Text += ((Button)sender).Text + "\r\n";
btnIssueTicket.Enabled = true;
lblStatus.Text = "";
}
protected void btnIssueTicket_Click(object sender, EventArgs e)
{
tbPrint.Text = ticket + 1;
}