在ASP.net中维护动态添加的控件

时间:2012-11-25 00:53:31

标签: c# asp.net webforms

我被困在如何在Webforms应用程序中维护动态添加的用户控件的视图状态。我创建了一个名为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>

用户将从下拉列表中选择项目以设置每个标签的值。这是按钮单击事件设置,如下所示:

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

这正确地将我的用户控件添加到我的Panel控件WeightList。我无法解决的是如何在页面init事件中成功重新创建这些控件?我认为它与控件ID有关?或许我的方法永远不会奏效。任何反馈都会非常有用!!

以下Tims评论我不确定如何使用ID重新加载UserCOntrol。我在Page_Load事件中尝试了这个,如果回发:

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

            }
            }


        } 

但它没有重新创建Set对象。加载页面时,每个Set UC都会显示以下内容:

&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>

控件上的每个标签都有一个公共属性,用于设置其值:

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)
    {

    }
}

按照呈现给页面的内容我虽然可以执行以下操作(但不起作用!!)

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

        }
        }


    } 

但这也不起作用。我想我在这里遗漏了一些明显的东西?

1 个答案:

答案 0 :(得分:0)

在页面加载中尝试此操作:

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

只要您在页面上有一个带有正确ID的控件,那么这些值将从您为视图状态加载。