Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old February 16th, 2004, 05:45 AM
Registered User
 
Join Date: Feb 2004
Location: , , Denmark.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default forcing a page_load ?

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



  #2 (permalink)  
Old February 16th, 2004, 10:20 AM
Registered User
 
Join Date: Feb 2004
Location: , , Denmark.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

nevermind, I dragged a button object onto the form in designermode instead of creating it dynamically, and vupti now everything works.

  #3 (permalink)  
Old February 16th, 2004, 10:53 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

For future reference:

As you have found, strange things happen when you start adding controls to the page dynamically. The first thing you should do if you have to do this is to give the control an explicitly defined ID. Just like in the markup where you declaratively specify id="cmdMyButton" you can programmatically set it: cmdMyButton.ID="cmdMyButton". Even though you have a variable name for an object, it's ID is not implicitly set. This would stop the back and forth change of the name of the button in the resulting HTML.

You should try to avoid building controls on the fly in a regular form or user control. Coming from ASP programming I recognize the desire to write ASPX pages in a similar linear method. Due to the model of ASP.net this method often causes problems. I have found that I often have to think about form and control design in a backwards manner. In ASP we would "construct" a page of HTML at runtime. In ASP.net it's actually much easier to "deconstruct" the page at runtime. You put all the things you are going to need on the page in design time so all the objects are in their place. Then you use the runtime code to hide controls that aren't needed. One of the beauties of ASP.net is the ability to set controls to hidden initially so you can design the page to its initial state. Because of the workings of ASP.net control state management, you can hide and show controls based on events and you let the .net runtime deal with maintaining those states.

When you need to build controls on-the-fly, you should look at using some listing control (repeater, datalist, datagrid). However, there may be times that you need to truly construct controls on the fly because of the nature of the application. This is a much more complicated task and would be best left to another thread.

Peter
------------------------------------------------------
Work smarter, not harder.


Similar Threads
Thread Thread Starter Forum Replies Last Post
Forcing A New Line Tre XSLT 3 June 25th, 2007 01:08 PM
forcing files to download problem harpua Classic ASP Professional 2 July 4th, 2006 09:45 AM
Forcing an ActiveX Version to download deepak.vasudevan HTML Code Clinic 0 September 2nd, 2005 02:19 AM
forcing button to go the server shoakat Classic ASP Databases 4 September 10th, 2004 10:36 AM
forcing page breaks emprise Crystal Reports 0 July 26th, 2004 03:51 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.