1

我正在处理一个非常不寻常的情况,这让我努力寻找一些答案。

我正在尝试根据以下文档重新生成一些控件,

http://www.codeproject.com/Articles/3684/Retaining-State-for-Dynamically-Created-Controls-i

这种方法适用于 TextBox 控件,但似乎无法响应 Label、Literal、UploadFile 或其他我不知道的控件。

由于安全原因,我确实理解这不适用于 UploadFile,但为什么不适用于其他非 TextBox 控件?

上面的文章建议,如果我们维护一个控件的 ID,我们可以在回发后保留它们,但在下面的实现中,我只让 TextBox 响应这个解决方案。在这种情况下,“标签”和“文字”控件在 PostBack 后丢失,这是不可取的,因为我几乎逐行遵循配方。

有人可以看看下面的实现,看看我哪里出错了,或者我是否在某种程度上把整个概念弄错了?

这是一个跟踪生成的控制集数量的计数器,

protected int NumberOfControls
{
    get { return (int)ViewState["NumControls"]; }
    set { ViewState["NumControls"] = value; }
}

这是 Page_Load 事件,它在初始页面加载时从数据库中获取以前的幻灯片,并在回发时重新生成相同的页面。AddSlide 是一个“占位符”控件,我将其他控件发布到其中。

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        this.NumberOfControls = PopulateCarouselSettingFields(AddSlides);

    }
    else
    {
        this.GenerateControls();
    }
}

这是“PopulateCarouselSettingsFields”的定义。

public static int PopulateCarouselSettingFields(PlaceHolder _AddSlides)
{
    string result = string.Empty;
    int counter = 0;

    string CS = ConfigurationManager.ConnectionStrings["someconn"].ConnectionString;

    using (SqlConnection conn = new SqlConnection(CS))
    {
        SqlCommand SqlCmd = new SqlCommand();
        conn.Open();

        SqlCmd.Connection = conn;
        SqlCmd.CommandType = CommandType.StoredProcedure;
        SqlCmd.CommandText = "storedprocedure";

        SqlDataReader dReader;

        dReader = SqlCmd.ExecuteReader();

        while (dReader.Read())
        {
            Literal lit1 = new Literal();
            lit1.ID = "Lit1_" + counter;
            lit1.Text = "<div class=\"controls controls-row\"><div class=\"span3\">";
            _AddSlides.Controls.Add(lit1);

            Label CarouselTextLabel = new Label();
            CarouselTextLabel.ID = "CarouselTextLabel" + counter;
            CarouselTextLabel.Text = "Carousel Text";
            CarouselTextLabel.Font.Bold = true;
            CarouselTextLabel.CssClass = "control-label";
            _AddSlides.Controls.Add(CarouselTextLabel);

            TextBox CarouselText = new TextBox();
            CarouselText.ID = "CarouselText" + counter;
            CarouselText.TextMode = TextBoxMode.MultiLine;
            CarouselText.Height = 50;
            CarouselText.Text = dReader["CarouselText"].ToString();
            _AddSlides.Controls.Add(CarouselText);

            Literal Lit2 = new Literal();
            Lit5.ID = "Lit2_" + counter;
            Lit5.Text = "</div></div></div><br />";
            _AddSlides.Controls.Add(Lit2);

            counter++;

            }
    }
    return counter;
}

这应该在从 Page_Load 事件调用 PostBack 时使用它们的 ID 重新生成或重新声明所有控件。

protected void GenerateControls()
{
    int count = this.NumberOfControls;

    for (int i = 0; i < count; i++)
    {
        Literal lit1 = new Literal();
        lit1.ID = "Lit1_" + i.ToString();
        AddSlides.Controls.Add(lit1);

        Label CarouselTextLabel = new Label();
        CarouselTextLabel.ID = "CarouselTextLabel" + i.ToString();
        AddSlides.Controls.Add(CarouselTextLabel);

        TextBox CarouselText = new TextBox();
        CarouselText.ID = "CarouselText" + i.ToString();
        AddSlides.Controls.Add(CarouselText);

        Literal Lit2 = new Literal();
        Lit2.ID = "Lit2_" + i.ToString();
        AddSlides.Controls.Add(Lit2);
    }
}

以下代码将一组新控件添加到占位符“AddSlides”容器中。

protected void AddMoreSlidesToCarousel(object sender, EventArgs e)
{
    Literal lit1 = new Literal();
    lit1.ID = "Lit1_" + NumberOfControls.ToString();
    lit1.Text = "<div class=\"controls controls-row\"><div class=\"span3\">";
    AddSlides.Controls.Add(lit1);

    Label CarouselTextLabel = new Label();
    CarouselTextLabel.ID = "CarouselTextLabel" + NumberOfControls.ToString();
    CarouselTextLabel.Text = "Carousel Text";
    CarouselTextLabel.Font.Bold = true;
    CarouselTextLabel.CssClass = "control-label";
    AddSlides.Controls.Add(CarouselTextLabel);

    TextBox CarouselText = new TextBox();
    CarouselText.ID = "CarouselText" + NumberOfControls.ToString();
    CarouselText.TextMode = TextBoxMode.MultiLine;
    CarouselText.Height = 50;
    AddSlides.Controls.Add(CarouselText);

    Literal Lit2 = new Literal();
    Lit2.ID = "Lit2_" + NumberOfControls.ToString();
    Lit2.Text = "</div></div></div><br />";
    AddSlides.Controls.Add(Lit2);

    this.NumberOfControls++;
}
4

1 回答 1

1

I'm sorry to put it like this, but the article you are relying on is doing it wrong.

Dynamically created controls MUST be created in Page_Init, so that they exist before any ViewState stuff. Also, dynamically controls must be recreated each and every time the Page is initialized, PostBack or not.

ViewState/PostBack do not "retain" controls, only the state of such controls.

Please read this article: ASP.NET Page Life Cycle Overview

于 2013-07-09T09:33:57.587 回答