动态加载的 Web 用户控件在回发时隐藏 [英] Dynamically loaded web user control hides on postback

查看:20
本文介绍了动态加载的 Web 用户控件在回发时隐藏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网络自定义控件

i have a web custom control

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs"
    Inherits="WebApplication5.WebUserControl1" %>
<asp:DropDownList ID="ddlnew" AutoPostBack="true" runat="server" 
    onselectedindexchanged="ddlnew_SelectedIndexChanged">
    <asp:ListItem Text="text1" />
    <asp:ListItem Text="text1" />
    <asp:ListItem Text="text2" />
    <asp:ListItem Text="text1" />
    <asp:ListItem Text="text2" />
    <asp:ListItem Text="text2" />
</asp:DropDownList>

在 Default.aspx 页面上

and on the Default.aspx page

<asp:Button Text="PostBack" ID="btnPost" runat="server" 
onclick="btnPost_Click" />

在 Default.aspx.cs 上

and on the Default.aspx.cs

    protected void btnPost_Click(object sender, EventArgs e)
    {
        WebUserControl1 uc =  (WebUserControl1)Page.LoadControl("~/WebUserControl1.ascx");
        PlaceHolder.Controls.Add(uc);
    }

当我们从下拉列表中选择任何项目时,它会回发并且控件会从页面中隐藏

and when we select any item from dropdown it postback and the control hide from the page

所以请帮助如何防止隐藏

so please help how it can be prevented from hide

谢谢

推荐答案

动态创建的控件必须在每次回发时重新创建,当您意识到每个回发都会创建 Page 的新实例时 类,在这个实例中你必须每次都重新创建所有控件,这变得更加明显.

Dynamically created controls must be recreated on every postback, when you realise that each postback creates a new instance of the Page class, and within this instance you must re-create all of the controls each and every time, this becomes more obvious.

这是一篇很好的文章

另一个在我回答的这个帖子上发帖

和另一个

要维护回发之间的状态,您可以使用 ViewStateControlState

To maintain state between the postbacks, you can use ViewState or ControlState

MSDN 控制状态与视图状态示例

这篇关于动态加载的 Web 用户控件在回发时隐藏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆