简体   繁体   English

无法在动态添加的用户控件中获取文本框以维持状态

[英]Cannot get Textboxes in dynamically added usercontrol to maintain state

I have tried what seems like everything - I've done similiar things many times before, but I'm obviously missing something. 我尝试了所有看起来似乎都是的东西-之前我做过很多次类似的事情,但是显然我缺少一些东西。

I have a UserControl (ucA) - on ucA is a LinkButton that loads a different UserControl (ucB) programatically into a panel on ucA. 我有一个UserControl(ucA)-在ucA上是一个LinkBut​​ton,可通过编程将不同的UserControl(ucB)加载到ucA面板上。 ucB has TextBoxes, etc. ucB具有TextBoxes等。

Why isn't my control maintaining it's state? 为什么我的控件不保持其状态? ie: The textboxes are loosing thier value on postback - the control tree shows the name of the control and the Form values show the value (in trace.axd) 即:文本框在回发时失去其值-控件树显示控件的名称,窗体值显示该值(在trace.axd中)

Here is the code for ucA (basically) 这是ucA的代码(基本上)

public int SlideCount
{
    get { return Convert.ToInt32(ViewState["SlideCount"]); }
    set { ViewState["SlideCount"] = value; }
}

protected void LinkButton1_Click(object sender, EventArgs e)
{
    SlideCount += 1;
    LoadSlideControls();
}

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
    LoadSlideControls();
}

private void LoadSlideControls()
{
    this.pnlAnnouncementHolder.Controls.Clear();
    for (int i = 0; i < SlideCount; i++)
    {
        this.pnlAnnouncementHolder.Controls.Add(
            LoadControl("AnnouncementSlide.ascx"));
    }
}

Heres a full example of what im trying to do: http://keithsblogs.com/ControlTest2.zip 这是我要执行的操作的完整示例: http : //keithsblogs.com/ControlTest2.zip

The problem Vyrotek points out is that the control is added a little to late to the lifecycle on the click event - anyone have any ideas? Vyrotek指出的问题是,在click事件的生命周期中添加了控件的时间太晚了-有人有什么想法吗?

I just tried to recreate what you have setup and I dont seem to experience the same problem. 我只是尝试重新创建您已设置的内容,但似乎没有遇到相同的问题。 Would you like me to send you my Solution somehow? 您希望我以某种方式向您发送我的解决方案吗?

Final Edit, I promised - 我保证最后的编辑-

Try this solution: http://www.vyrotek.com/code/ControlTest2.zip 尝试以下解决方案: http : //www.vyrotek.com/code/ControlTest2.zip

I am installing express edition as I type. 我输入时正在安装Express Edition。 By that time, why should you clear the controls on click of the button? 到那时,为什么还要单击按钮才能清除控件? cant you just add/load the uc once control on click of the link? 一旦点击链接,您能否添加/加载uc?

By doing this, you would not be clearing the old controls and they would retain the value. 这样,您将不会清除旧的控件,它们将保留该值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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