views:

496

answers:

5

Is it possible to perform a postback and have the viewstate remember the selected value on the following code?

It seems placeholder1.controls.clear() is deleting it.

  protected void Page_Load(object sender, EventArgs e)
{
    bind();
}
protected void bind()
{
    PlaceHolder1.Controls.Clear();        
    DropDownList ddl = new DropDownList() { AutoPostBack = true, ID="ddl" };
    ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);
    ddl.Items.Add("hi");
    ddl.Items.Add("bye");
    PlaceHolder1.Controls.Add(ddl);
}
void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
    bind();
}
A: 

Why not just hide the PlaceHolder by setting Visible to false? Also, see Truly Understanding Dynamic Controls to get your head around when you need to dynamically inject stuff as dynamic controls are far from straightforward.

Wyatt Barnett
+1  A: 

Try removing the PlaceHolder1.Controls.Clear(); method,

and move your Bind() call out of Page_Load and into OnInit

You're going to run into Life Cycle Problems the way you're doing things there as the load viewstate & event firing code will have already occured before you recreate you're controls

Eoin Campbell
Removing the placeholder1.controls.clear() will mean it will error after trying to create a control with the same id twice.
maxp
no it won't. The controls are dynamically recreated on each subsequent refresh postback. PlaceHolder1.Controls doesn't contain anything in the first place. try it
Eoin Campbell
I interpreted your message as moving the Bind(); from Page_Load(o,e) to Page_Init(o,e). After the ddl would cause a postbackould, Page_Init() would fire Bind();, then SelectedIndexChanged() would be hit causing Bind() to run again(); Therefore causing an exception with two controls both in placeholder1 with the same id.Have i misinterpreted your 'OnInit?'
maxp
sorry, missed that last line... you shouldn't need to rebind inside your SelectedIndexChanged()
Eoin Campbell
If you bind only once, in the page_init(), and anything is relevant in that method on the selectedvalue/index then you still have the old value.
maxp
reliant not relevant
maxp
A: 

Try calling bind() from Page_Init().

ViewState is loaded after Page_Init() but before Page_Load(), so when you call bind() from Page_Load(), you're calling it after .NET has tried and failed to set the selected value of your DropDownList.

Lobstrosity
A: 

Is it possible to ... have the viewstate remember the selected value ... ?

ViewState data is restored on postbacks before the load event. If you want ViewState to remember anything for a control, that control must also exist on the page before the load event.

I suspect part of your problem is that you re-create your dropdownlist control when it's selectedindex changes, but nowhere in that code do you set the selectedindex and you therefore destroy the selection every time you set it.

Joel Coehoorn
A: 

Hi

I having an InvoiceLineitem that holds qty, unitprice and a description. I need to write a code that update the qty item/Value.

Here is my snippet code:

foreach (InvoiceLineItem lineitem in invoice.lineItems) { TableRow trnew = new TableRow();

            TableCell tablecell;

            tablecell = new TableCell();
            tablecell.Text = "" + lineitem.description;
            trnew.Cells.Add(tablecell);

            tablecell = new TableCell();
            tablecell.Text = Utils.formatAsRand(lineitem.unitprice);
            tablecell.HorizontalAlign = HorizontalAlign.Right;
            trnew.Cells.Add(tablecell);

            tablecell = new TableCell();
            tablecell.Text = "" + lineitem.code;
            trnew.Cells.Add(tablecell);

            tablecell = new TableCell();
            TextBox txtbox = new TextBox();
            txtbox.Text = "" + lineitem.qty;
            txtbox.Width = 20;
            tablecell.Controls.Add(txtbox);
            trnew.Cells.Add(tablecell);

tablecell = new TableCell(); Button btnupdate = new Button(); btnupdate.Text = "Update ";

            if (invoice != null)
            {
                foreach (InvoiceLineItem inv in invoice.lineItems)
                {
                    txtbox.Text = ""+lineitem.qty;
                    //inv.qty = txtbox.Text;
                    int result = Convert.ToInt32(lineitem.qty) * Convert.ToInt32(lineitem.unitprice);
                    lblTotal.Text = "" + result;
                }
            }

            tablecell.Controls.Add(btnupdate);
            trnew.Cells.Add(tablecell);
Thabo
hmmmmmmm.........
Thabo