In relation to this thread,
http://p2p.wrox.com/topic.asp?TOPIC_ID=8875 I seem to be having the same problem, and I cannot find a solution.
Here's what I got:
In page_load I draw a table based on a collection I have in a session object.
In page_load I also draw an update button, and the button_click method updates the objects in the collection.
When I click the button the first time, the table and button is redrawn in page_load (this has to be done so that the button still exists and the button_click can be executed).
Then the button_click is executed, changing the objects in the session collection, and the table and its content is drawn again (I moved the drawing of the page to a method called ShowContentOfBasket, which I call from both page_load and button_click).
The weird thing is, the first time the page is shown, the dynamically created update button has the following name (shown when I view source on the page) name="_ctl0" .. after clicking the button once, this is changed to name="_ctl1" .. and then forth and back between these two names constantly.
I have no idea how to get past this problem so I'm hoping anyone can give me a hint..
My own suggestion is to not call the ShowContentOfBasket method from the button_click but instead somehow force another postback. This would of course increase bandwidth usage, but that is still better than having to click the button twice.
//here's the code
private void Page_Load(object sender, System.EventArgs e) {
ShowContentOfBasket();
}
private void ShowContentOfBasket() {
WebshopLocal.Classes.Basket basket = ((WebshopLocal.Classes.Basket)Session["basket"]);
int j = 0;
tblBasket.Rows.Clear();
foreach (WebshopLocal.Classes.Vare objVare in basket) {
tblBasket.Rows.Add(new TableRow());
// add cell with articleno
tblBasket.Rows[j].Cells.Add(new TableCell());
tblBasket.Rows[j].Cells[0].CssClass = "ws_searchres_cell_artno";
tblBasket.Rows[j].Cells[0].Text = objVare.ArticleNo.ToString();
// add cell with descshort
tblBasket.Rows[j].Cells.Add(new TableCell());
tblBasket.Rows[j].Cells[1].CssClass = "ws_searchres_cell_descshor";
tblBasket.Rows[j].Cells[1].Text = objVare.DescShort.ToString();
// add cell with producent
tblBasket.Rows[j].Cells.Add(new TableCell());
tblBasket.Rows[j].Cells[2].CssClass = "ws_searchres_cell_prod";
tblBasket.Rows[j].Cells[2].Text = objVare.Manufacturer.ToString();
// add cell with price
tblBasket.Rows[j].Cells.Add(new TableCell());
tblBasket.Rows[j].Cells[3].CssClass = "ws_searchres_cell_pris";
tblBasket.Rows[j].Cells[3].Text = objVare.Price.ToString();
// add cell with textbox for #ofitems
tblBasket.Rows[j].Cells.Add(new TableCell());
tblBasket.Rows[j].Cells[4].CssClass = "ws_searchres_antal";
TextBox txtAntal = new TextBox();
txtAntal.CssClass = "ws_searchres_txtantal";
txtAntal.Text = objVare.Antal.ToString();
txtAntal.ID = "txt" + objVare.ArticleNo.ToString();
tblBasket.Rows[j].Cells[4].Controls.Add(txtAntal);
j++;
}
//last row in table containing update button
TableRow tr = new TableRow();
//empty cells to make the button appear in same column as the textboxes
tr.Cells.Add(new TableCell());
tr.Cells.Add(new TableCell());
tr.Cells.Add(new TableCell());
tr.Cells.Add(new TableCell());
TableCell tc = new TableCell();
tc.CssClass = "ws_searchres_cell_updatebtn";
Button btnUpdate = new Button();
btnUpdate.Text = "Opdatér";
btnUpdate.Click += new System.EventHandler(this.update_click);
tc.Controls.Add(btnUpdate);
tr.Cells.Add(tc);
tblBasket.Rows.Add(tr);
}
private void update_click(object sender, System.EventArgs e) {
int antal = 0;
int articleno = 0;
WebshopLocal.Classes.Basket basket = ((WebshopLocal.Classes.Basket)Session["basket"]);
for (int j = 0; j <= tblBasket.Rows.Count -2; j++) { //-2 because the last row contains the updatebutton
articleno = int.Parse(tblBasket.Rows[j].Cells[0].Text);
try {
antal = int.Parse(((TextBox)tblBasket.Rows[j].Cells[4].Controls[0]).Text);
} catch (Exception ex) {
antal = 1;
}
//find object in basket and update #ofitems, remove object if #ofitems <= 0
int i = 0;
bool found = false;
while (!found && i <= basket.Count -1) {
if (basket.Item(i).ArticleNo == articleno) {
if (antal <= 0) {
basket.remove(basket.Item(i));
} else {
basket.Item(i).Antal = antal;
}
found = true;
} else {
i++;
}
}
}
ShowContentOfBasket();
}