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

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

问题描述

我有一个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

所以请大家帮忙怎么能说是从隐藏pvented $ P $

so please help how it can be prevented from hide

感谢

推荐答案

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

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.

下面是对这个一个很好的文章

<一个href=\"http://stackoverflow.com/questions/5432413/dynamically-create-controls-and-save-the-controls-values-across-postback-asp-n/5432613#5432613\">Another张贴在此,我回答

<一个href=\"http://stackoverflow.com/questions/5434521/adding-a-user-control-to-a-page-programatically-while-$p$pserving-controls-alread/5434572#5434572\">And另一个

要保持回发之间的状态,你可以使用的ViewState 了ControlState-

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

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

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

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