views:

29

answers:

2

I am trying to add an instance of UserControl each time on button_click event, it works for the first time only. I am adding IDs to user control by appending integers from ViewState. Why does it not add work for subssequent clicks. Here is the simplified version with the same behavoir.

protected void Button1_Click(object sender, EventArgs e) {

        HtmlButton btnClick = new HtmlButton();
        btnClick.ID = "bt" + Append.ToString();
        btnClick.InnerText = "Button 1";
        MainArea.Controls.Add(btnClick);

}

A: 

Ok, now that the code is posted, I can confirm the problem.

Each time you click the button, a postback is made. On postback, you add the new button, with the name containing your count. But, when the page re-renders on the second button click, it loses all the controls you've added and just adds back the one (from this postback's button click).

So what you want to do is set up a loop, something like:

protected void Button1_Click(object sender, EventArgs e) {
    for(int i = 0; i < Append; i++)
    {
        HtmlButton btnClick = new HtmlButton();
        btnClick.ID = "bt" + i.ToString();
        btnClick.InnerText = "Button " + i;
        MainArea.Controls.Add(btnClick);
    }
}

And that will add one button per previous click, assuming that "Append" is some kind of counter that you're storing in viewstate.

Now that's also assuming you wanted one instance of the control per click. Which is what I think you said.

CodexArcanum
Thanks for the reply! I have another question for you guys. My viewstate do not persist during the postback even though the id of the controls remain the same. I am adding user controls on Button click just as @CodexArcanum mentione. That means on 5th click I am iterating 5 times and adding 5 user controls, ids of previous ones remainng same though. What do I need to do to persist userinput in viewstate.
User_003
A: 

I'm doing something similar with an address user control.

Since you're adding the control dynamically, you will need to re-create the control after each post-back (each time you add another, you need to recreate ALL the previous controls you added).

I keep an arraylist of the control ID's (if you want viewstate to keep the contents, you need to create the controls with the same IDs) which also keeps track of how many controls I've previously added and need to recreate. I then loop through and create the controls again in my page load event:

foreach (string id in AddressItemIDs)
{
   addAddressControl(null, id);
}

and then my "button1_click" event (in my case I'm using a link button...

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        if (AddressCount == 0 || AddressCount < MaxAddresses && ((addressItem)addressContainer.Controls[addressContainer.Controls.Count-1]).HasAddress)
        {
            int i = 0;
            string prefix = "addressItem_";
            while (AddressItemIDs.Contains(prefix + i)) i++;

            AddressCount++;
            addAddressControl(null, prefix + i);

        }
        Button1.Visible = (AddressCount < MaxAddresses);
    }

here is my addAddressControl method:

    protected void addAddressControl(Address address, string id)
    {
        addressItem ai = (addressItem)LoadControl("~/controls/addressItem.ascx");
        ai.RemoveClicked += new EventHandler(ai_RemoveClicked);
        ai.ID = id;

        if (address != null)
        {
            ai.AddressID = address.AddressID;
            ai.Address = address.Street;
            ai.City = address.City;
            ai.State = address.State;
            ai.Zip = address.Zip;
            ai.TypeID = address.TypeID;
        }
        if(!AddressItemIDs.Contains(id))
            AddressItemIDs.Add(id);

        addressContainer.Controls.Add(ai);
    }
senloe
Depending on what is actually being done with these controls, it may be worthwhile to look into persistent storage: primarily I mean a SQL DB, but there are alternatives as well.Then you can use a Repeater control to generate your contents and the DB to store your state between views and sessions.
CodexArcanum
I am using a SQL DB to store the data once it is saved, but I'm using this control and Ajax to add/edit x number of items in the meantime.
senloe
Thanks for the reply! I have another question for you guys. My viewstate do not persist during the postback even though the id of the controls remain the same. I am adding user controls on Button click just as @CodexArcanum mentione. That means on 5th click I am iterating 5 times and adding 5 user controls, ids of previous ones remainng same though. What do I need to do to persist userinput in viewstate.
User_003