C# .NET UserControl 事件问题

发布于 2024-08-04 08:42:45 字数 1096 浏览 2 评论 0原文

我有一个 UpdatePanel,其中有一个常规面板。在面板中,我动态添加简单的用户控件。用户控件有一个按钮和一个标签。当我单击控件中的按钮时,它会删除我动态添加的面板中的所有控件。 有人可以帮忙吗?

    int controlID = 0;
    List<Control> cc = new List<Control>();
    if (Session["ControlsCompleted"] != null)
    {
        cc = Session["ControlsCompleted"] as List<Control>;
        for (int i = 0; i < cc.Count; i++)
        {
            pnlCompletedEducation.Controls.Add(cc[i]);
        }
        controlID = cc.Count;
    }
    Controls_TestWebUserControl ct = LoadControl(@"Controls\TestWebUserControl.ascx") as Controls_TestWebUserControl;
    ct.ID = controlID.ToString();
    cc.Add(ct);
    ct.EnableViewState = true;
    pnlCompletedEducation.Controls.Add(ct);
    txtInstitutionName.Text = controlID.ToString();
    List<Control> lc = new List<Control>();
    for (int i = 0; i < pnlCompletedEducation.Controls.Count; i++)
    {
        lc.Add(pnlCompletedEducation.Controls[i]);
    }
    Session["ControlsCompleted"] = lc;

这就是我将控件添加到面板的方法。我必须将它们保存在某个地方,而我无法使用 ViewState 来做到这一点,所以我使用了 Session,这是一个坏主意。

I have an UpdatePanel and in it a regular Panel. In the Panel I dynamically add simple UserControls. The Usercontrol has a Button and a Label. When I click on a button in a control it removes all controls in the Panel which I have added dynamically.
Can anyone help?

    int controlID = 0;
    List<Control> cc = new List<Control>();
    if (Session["ControlsCompleted"] != null)
    {
        cc = Session["ControlsCompleted"] as List<Control>;
        for (int i = 0; i < cc.Count; i++)
        {
            pnlCompletedEducation.Controls.Add(cc[i]);
        }
        controlID = cc.Count;
    }
    Controls_TestWebUserControl ct = LoadControl(@"Controls\TestWebUserControl.ascx") as Controls_TestWebUserControl;
    ct.ID = controlID.ToString();
    cc.Add(ct);
    ct.EnableViewState = true;
    pnlCompletedEducation.Controls.Add(ct);
    txtInstitutionName.Text = controlID.ToString();
    List<Control> lc = new List<Control>();
    for (int i = 0; i < pnlCompletedEducation.Controls.Count; i++)
    {
        lc.Add(pnlCompletedEducation.Controls[i]);
    }
    Session["ControlsCompleted"] = lc;

This is how I add the controls to the panel. I had to keep them somewhere, and i couldn't do it with the ViewState, so i used a Session, which is a bad idea.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千秋岁 2024-08-11 08:42:45

您说您正在动态添加用户控件。您是否有这样的代码:

void Page_Load(...)    
{
     if (!IsPostback)
        // AddUserControl here.
}

您需要在每个请求以及回发期间添加用户控件,因为它不会存储在您修改了控件树的视图状态中。

You say that you are adding the user control dynamically. Are you having code like this:

void Page_Load(...)    
{
     if (!IsPostback)
        // AddUserControl here.
}

You need to add the user control during every request, also postbacks, because it will not be stored in the view state that you have modified the control tree.

最舍不得你 2024-08-11 08:42:45

您的问题是您没有重新创建(例如在 Page_Load 处)动态添加的控件。
确保在 IsPostBack 上重新创建控件

You problem that you have not recreated (for example at Page_Load) dynamically added control.
Make sure that control is recreated on IsPostBack

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文