Dynamically loaded web user control hides on postback(动态加载的 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.
这是一篇很好的文章
另一个在我回答的这个帖子上发帖
和另一个
要维护回发之间的状态,您可以使用 ViewState 或 ControlState
To maintain state between the postbacks, you can use ViewState or ControlState
MSDN 控制状态与视图状态示例
这篇关于动态加载的 Web 用户控件在回发时隐藏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:动态加载的 Web 用户控件在回发时隐藏
基础教程推荐
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- WPF 模态进度窗口 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
