Sign in to follow this  
agm_ultimatex

[.net] problem with dynamic checkboxes asp.net c#

Recommended Posts

Im not sure if this should perhaps go in the web section instead. Anyways, heres my question/problem. I'm building a simple cart app. I currently have 4 items that are loaded on page_load. Checkboxes are created with each item in the C# code. Im trying to refer to them later, when the form is submitted, so the program knows which items a person wants to buy. Code:
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitializePage();
        }
    }

    public void InitializePage()
    {
        Item item1 = new Item("The Bourne Identity", 8.99);
        Item item2 = new Item("The Da Vinci Code", 7.99);
        Item item3 = new Item("War and Peace", 12.99);
        Item item4 = new Item("Paradise Lost", 9.45);

        Item[] items = new Item[4];
        items[0] = item1;
        items[1] = item2;
        items[2] = item3;
        items[3] = item4;
        Session["items"] = items;
        for (int i = 0; i < items.Length; i++)
        {
            Item item = items[i];
            HtmlTableRow tr = new HtmlTableRow();
            switch (i)
            {
                case 0:
                    tr.BgColor = "#dddddd";
                    break;
                case 1:
                    tr.BgColor = "#bbbbbb";
                    break;
                case 2:
                    tr.BgColor = "#dddddd";
                    break;
                case 3:
                    tr.BgColor = "#bbbbbb";
                    break;
            };

            HtmlTableCell tdName = new HtmlTableCell();
            tdName.Controls.Add(new LiteralControl(item.Name));

            HtmlTableCell tdPrice = new HtmlTableCell();
            tdPrice.Controls.Add(new LiteralControl("$" + item.Price.ToString()));

            HtmlTableCell tdAddToCart = new HtmlTableCell();

            CheckBox cb = new CheckBox();
           
            cb.ID = "cb" + i.ToString();
            
            tdAddToCart.Controls.Add(cb);
            tr.Cells.Add(tdName);
            tr.Cells.Add(tdPrice);
            tr.Cells.Add(tdAddToCart);
            Products.Rows.Add(tr);
        }
    }

    public void View_Btn(object sender, EventArgs e)
    {
        Item[] items = (Item[])Session["items"];
        Cart<Item> cart = new Cart<Item>();
        CheckBox cb0 = (CheckBox)FindControl("cb0");
        if (cb0 != null)
        {
            if (cb0.Checked)
            {
                cart.Push(items[0]);
            }
        }
        CheckBox cb1 = (CheckBox)FindControl("cb1");
        if (cb1 != null)
        {
            if (cb1.Checked)
            {
                cart.Push(items[1]);
            }
        }
        CheckBox cb2 = (CheckBox)FindControl("cb2");
        if (cb2 != null)
        {
            if (cb2.Checked)
            {
                cart.Push(items[2]);
            }
        }
        CheckBox cb3 = (CheckBox)FindControl("cb3");
        if (cb3 != null)
        {
            if (cb3.Checked)
            {
                cart.Push(items[3]);
            }
        }
        Session["cart"] = cart;
        Response.Redirect("cart.aspx");
    }


Without the null checks in the View_Btn method, the checkbox elements come up as a nullreference. Thanks

Share this post


Link to post
Share on other sites
Hi There,

I found this post very useful. i have created something silimar but my dynamic checkboxes are behaving like "ghosts" on the page. They always return a checked value "false". even if i know they are checked. They are not valid checkbox objects. Do you know why?

Thanks

Share this post


Link to post
Share on other sites
The problem is that a webpage is basically stateless. Once you reload/postback the state is cleared. The asp.net viewstate is one solution to this, however controls that you dynamically add does not save/restore the viewstate automatically. You have to do this yourself, and in my experience it's not entirely trivial.

My suggestion is to look into the DataList control (alternatively DataGrid/GridView/Repeater) as you can design a template used for each row which is automatically saved into the viewstate.

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datalist.aspx

Share this post


Link to post
Share on other sites
Can I ask why you are not using the markup to generate this sort of thing?

It seems like that would solve half of your problems right there. You can make an array of books, add a gridview or a repeater, and set the datasource on it to the array, then databind. Both controls give you the ability to set alternating colors too.

Then you'll have controls that persist across postbacks without any mess

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this