简体   繁体   中英

Maintaining Dynamically added controls in ASP.net

I am stuck on how to maintain the viewstate of my dynamically added usercontrols in a Webforms app. I have created a User control named Set.ascx:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Set.ascx.cs" Inherits="CustomControlLab.Set" %>
&nbsp
<asp:Label ID="BodyArea" CssClass="SetLabel" runat="server" Text="Label"></asp:Label>
&nbsp
<asp:Label ID="Exercise" CssClass="SetLabel" runat="server" Text="Label"></asp:Label>
&nbsp
<asp:Label ID="SetNo" CssClass="SetLabel" runat="server" Text="Label"></asp:Label>
&nbsp
<asp:Label ID="Reps" CssClass="SetLabel" runat="server" Text="Label"></asp:Label>

A user will select items from drop down lists to set value for each label. This is set on a buttons click event as follows:

    protected void AddExercise_Click(object sender, EventArgs e)
    {
            Set set = (Set)Page.LoadControl("Set.ascx");
            int currentSet = (int)ViewState["Sets"];
            set._bodyArea = AreaSelect.SelectedValue;
            set._exercise = ExerciseSelect.SelectedValue;
            set._reps = RepNumbers.SelectedValue;
            set._setNumber = currentSet.ToString();
            int idCount = (int)ViewState["Sets"];
            set.ID = string.Format("set{0}", idCount);
            currentSet++;
            ViewState["Sets"] = currentSet;
            WeightList.Controls.Add(set);
     }

This correctly adds my user Control to my Panel control WeightList. What I cant work out is how to successfully recreate these controls in the page init event? I presume it is something to do with the control ID? Or perhaps my approach is never going to work. Any feed back would be really helpful!!

Following Tims Comment I am not sure how I re-load the UserCOntrol using the ID. I tried this in the Page_Load event if post back:

      else
        {
            if(ViewState["Sets"] != null)
            {
            int sets = (int)ViewState["Sets"];
            for (int i = 0; i < sets; i++)
            {
                Set set = new Set();
                set = (Set)Page.FindControl(string.Format("MainContent_set{0}", i + 1));
                WeightList.Controls.Add(set);

            }
            }


        } 

but it is not recreating the Set object. Looking at the page when it is loaded each Set UC renders the following:

&nbsp
<span id="MainContent_set1_BodyArea" class="SetLabel">All Exercises</span>
&nbsp
<span id="MainContent_set1_Exercise" class="SetLabel">Bench Press</span>
&nbsp
<span id="MainContent_set1_SetNo" class="SetLabel">1</span>
&nbsp
<span id="MainContent_set1_Reps" class="SetLabel">3</span>

Each label on the control has a public property for setting its value:

public partial class Set : System.Web.UI.UserControl
{
    public string _exercise { get { return Exercise.Text; } set { Exercise.Text = value; } }
    public string _bodyArea { get { return BodyArea.Text; } set { BodyArea.Text = value; } }
    public string _setNumber { get { return SetNo.Text; } set { SetNo.Text = value; } }
    public string _reps { get { return Reps.Text; } set { Reps.Text = value; } }

    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

Following what is rendered to the page I though I could do the following (which doesnt work!!)

      else
    {
        if(ViewState["Sets"] != null)
        {
        int sets = (int)ViewState["Sets"];
        for (int i = 0; i < sets; i++)
        {
            Set set = new Set();
            Label templbl = (Label)Page.FindControl(string.Format("MainContent_set{0}_BodyArea", i + 1));
            set._bodyArea = templbl.Text;
            //Other code to get rest of control values 
            WeightList.Controls.Add(set);

        }
        }


    } 

but this doesn't work either. I think I am missing something obvious here?

Try this in your page load:

if(ViewState["Sets"] != null)
{
    int sets = (int)ViewState["Sets"];
    for (int i = 0; i < sets; i++)
    {
        Set set = new Set();
        set.ID =  string.Format("set{0}", i);
        WeightList.Controls.Add(set);
     }
 }

As long as you have a control with the right Id on the page then the values will be loaded from the viewstate for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM