问题描述
我有一个网络自定义控件
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.
要维护回发之间的状态,您可以使用 ViewState
或 ControlState
To maintain state between the postbacks, you can use ViewState
or ControlState
这篇关于动态加载的 Web 用户控件在回发时隐藏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!