在 UpdatePanel 内回发后添加用户控件,然后再进行另一个回发 - 如何获取用户控件内控件的输入?

发布于 2024-12-06 00:17:40 字数 1304 浏览 2 评论 0原文

我有一个用户控件,在 UpdatePanel 中执行回发后,该控件会动态加载到页面。

然后,我执行另一个回发以进行保存,其中我想访问该用户控件中控件的用户输入(例如,每行都有一个复选框的网格)。

如果此场景如下所示,则为一个简化示例:

<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <div><asp:Button ID="ButtonAdd" runat="server" Text="Add User Control" OnClick="ButtonAdd_OnClick" /></div>
        <div id="divContent" runat="server"></div>
    </ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
    <ContentTemplate>
        <div><asp:Button ID="ButtonSave" runat="server" Text="Save" OnClick="ButtonSave_OnClick" /></div>
    </ContentTemplate>
</asp:UpdatePanel>

并且:

protected void ButtonAdd_OnClick(object sender, EventArgs e)
{
    MyUserControl uc = Page.LoadControl("MyUserControl.ascx") as MyUserControl;
    divContent.Controls.Add(uc);
}

protected void ButtonSave_OnClick(object sender, EventArgs e)
{
    // Hopefully, get controls from within the user control's grid and save their input
}

问题是单击“保存”按钮后,用户控件消失了。这是可以理解的,我再次创建和添加它没有问题 - 但我确实想访问用户输入并保存它。我怎样才能做到这一点?

I have a user control which is being loaded to the page dynamically after performing a postback within an UpdatePanel.

Then I'm performing another postback for saving, where I would like to access the user input for the controls within that user control (for example, a grid with a checkbox in every row).

A simplified example if this scenario would be something as follows:

<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <div><asp:Button ID="ButtonAdd" runat="server" Text="Add User Control" OnClick="ButtonAdd_OnClick" /></div>
        <div id="divContent" runat="server"></div>
    </ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
    <ContentTemplate>
        <div><asp:Button ID="ButtonSave" runat="server" Text="Save" OnClick="ButtonSave_OnClick" /></div>
    </ContentTemplate>
</asp:UpdatePanel>

And:

protected void ButtonAdd_OnClick(object sender, EventArgs e)
{
    MyUserControl uc = Page.LoadControl("MyUserControl.ascx") as MyUserControl;
    divContent.Controls.Add(uc);
}

protected void ButtonSave_OnClick(object sender, EventArgs e)
{
    // Hopefully, get controls from within the user control's grid and save their input
}

The problem is that after the save button is clicked, the user control is gone. That's understandable and I have no problem creating and adding it again - but I do want to access the user input and save it. How can I do that?

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

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

发布评论

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

评论(2

鼻尖触碰 2024-12-13 00:17:40

如果您想以编程方式添加控件,请选择Page_Load(更推荐)或Page_Init事件。您可以通过使用 visible=false “静态”(设计时)添加控件来解决您的问题,稍后您可以打开其可见性。

如果您想像帖子中那样坚持当前的方法,则必须使用一个技巧

MyUserControl uc;

protected void Page_Load()
{
   if(ViewState["isAdd"]!=null)
    {
       AddControl();
    }
}

void AddControl()
{
    uc = Page.LoadControl("MyUserControl.ascx") as MyUserControl;
    divContent.Controls.Add(uc);
}

protected void ButtonAdd_OnClick(object sender, EventArgs e)
{
    if(ViewState["isAdd"]==null)
        AddControl();
    ViewState["isAdd"]="yes";
}

If you want to add controls programatically then choose Page_Load (more preferred) or Page_Init event. You can solve your problem by adding control "statically" (design time) with visible=false and later you may turn on its visibility.

In case you want stick with current approach as in your post, you have to use a trick.

MyUserControl uc;

protected void Page_Load()
{
   if(ViewState["isAdd"]!=null)
    {
       AddControl();
    }
}

void AddControl()
{
    uc = Page.LoadControl("MyUserControl.ascx") as MyUserControl;
    divContent.Controls.Add(uc);
}

protected void ButtonAdd_OnClick(object sender, EventArgs e)
{
    if(ViewState["isAdd"]==null)
        AddControl();
    ViewState["isAdd"]="yes";
}
对岸观火 2024-12-13 00:17:40

要访问内部控件,您必须使用属性公开它们或使用FindControl

另外,正如您所提到的,您已在每次回发时将动态控件添加回页面。

动态 Web 控件、回发和查看状态

To access the internal controls, you have to expose them with properties or use FindControl.

Also, as you mentioned, you have add dynamic controls back to the page on each postback.

Dynamic Web Controls, Postbacks, and View State

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