Reputation: 2070
I have buttons that display dynamically when the page loads. What is suppose to do is:
If I click on the next dynamic button it should go thru the same process and it should print Ticket 2.
The issue is that is not incrementing in value. I believe is because the page postback everytime I click the Issue Ticket therefore resetting the ticket value back to 1. Anyway to go around this.
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;
}
Upvotes: 0
Views: 525
Reputation: 62290
You need to have ID for a control if it is created dynamically.
Otherwise, they will become null when the page is posted back to server.
rotected void Page_Load(object sender, EventArgs e)
{
string buttonName;
try
{
...
CreateButton(transID, buttonName); // Pass transID
...
}
}
private void CreateButton(int transID, string buttonName)
{
transbutton = new Button();
transbutton.Text = buttonName;
transbutton.ID = transID.ToString(); // ID is required
...
}
Besides, if you want to keep track the number of tickets, you need to save it in ViewState
to persist the data.
public int Ticket
{
get { return Convert.ToInt32(ViewState["Ticket"] ?? 0); }
set { ViewState["Ticket"] = value; }
}
// Usage
protected void btnIssueTicket_Click(object sender, EventArgs e)
{
Ticket++;
tbPrint.Text = Ticket.ToString();
}
Upvotes: 1
Reputation: 63105
Since you having integer value as tbPrint.Text
You can do as below
Set initial tbPrint.Text
1 and
int no;
if (int.TryParse(tbPrint.Text, out no))
{
tbPrint.Text = no + 1;
}
Otherwise You can use Session, Viewstate, or a hidden field.
Upvotes: 0
Reputation: 3105
When you declare a variable inside your page class, it is initialized to its value every time a request hits the server.
So each time, your ticket
value is initialized to 0
.
If you want to keep this value over multiple requests, you should use the ViewState
and keep that value in it.
The ViewState
will be rendered as a hidden field and sent back to the server at every postback, and you'll be able to keep the precedent value.
To have a better understanding of which values are kept and which are lost, take a look at the asp.Net page lifecycle: http://msdn.microsoft.com/en-us/library/ms178472(v=vs.80).aspx
and also there is a very complete description of the viewstate here: http://msdn.microsoft.com/en-us/library/ms972976.aspx
and also here: http://www.codeproject.com/Articles/31344/Beginner-s-Guide-To-View-State
Upvotes: 0