将自定义控件添加到ASP.Net面板

编程入门 行业动态 更新时间:2024-10-19 06:20:35
本文介绍了将自定义控件添加到ASP.Net面板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的代码如下:

My code is as follows:

<asp:UpdatePanel runat="server" ID="upPanel1"> <ContentTemplate> <asp:Panel ID="pnlPartners" runat="server"> </asp:Panel> <asp:LinkButton ID="btnAddControl" OnClick="btnAddControl_Click" runat="server" Text="Add Control" /> </ContentTemplate> </asp:UpdatePanel>

现在,当我单击AddControl按钮时,我想为此添加自己的自定义文本框控件.这是我的btnAddControl方法的代码:

Now I want to add my own custom textbox control to this when I click the AddControl button. Here is the code for my btnAddControl method:

CustomTextbox ct = new CustomTextbox(); ct.LoadControl("/Controls/CustomTextbox.ascx"); pnlPartners.Controls.Add(ct);

我没有收到任何错误,我看到更新面板执行了回发操作,但是控件从未添加?任何帮助将不胜感激!

I get no errors, I see that the update panel does a post back but the control never gets added? Any help will be greatly appreciated!

推荐答案

选中此 [ ^ ] 及以下代码 Hi, Check this[^] and below code <%@ Page Language="C#" AutoEventWireup="true" CodeFile="SampleMenu1.aspx.cs" Inherits="SampleMenuPage1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "www.w3/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="www.w3/1999/xhtml" > <head runat="server"> <title>Sample Menu</title> </head> <body> <form id="form1" runat="server"> <asp:Menu ID="Menu1" runat="server" OnMenuItemClick="Menu1_MenuItemClick"> <Items> <asp:MenuItem Text="File"> <asp:MenuItem Text="Load Control1"></asp:MenuItem> <asp:MenuItem Text="Load Control2"></asp:MenuItem> <asp:MenuItem Text="Load Control3"></asp:MenuItem> </asp:MenuItem> </Items> </asp:Menu> <br /> <br /> <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager> <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"> <ContentTemplate> <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder> </ContentTemplate> <Triggers> <asp:AsyncPostBackTrigger ControlID="Menu1" /> </Triggers> </asp:UpdatePanel> </form> </body> </html>

using System; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class PlainSampleMenuPage : System.Web.UI.Page { private const string BASE_PATH = "~/DynamicControlLoading/"; private string LastLoadedControl { get { return ViewState["LastLoaded"] as string; } set { ViewState["LastLoaded"] = value; } } private void LoadUserControl() { string controlPath = LastLoadedControl; if (!string.IsNullOrEmpty(controlPath)) { PlaceHolder1.Controls.Clear(); UserControl uc = (UserControl)LoadControl(controlPath); PlaceHolder1.Controls.Add(uc); } } protected void Page_Load(object sender, EventArgs e) { LoadUserControl(); } protected void Menu1_MenuItemClick(object sender, MenuEventArgs e) { MenuItem menu = e.Item; string controlPath = string.Empty; switch (menu.Text) { case "Load Control2": controlPath = BASE_PATH + "SampleControl2.ascx"; break; case "Load Control3": controlPath = BASE_PATH + "SampleControl3.ascx"; break; default: controlPath = BASE_PATH + "SampleControl1.ascx"; break; } LastLoadedControl = controlPath; LoadUserControl(); } }

我在此处找到了它

更多推荐

将自定义控件添加到ASP.Net面板

本文发布于:2023-11-15 02:50:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1590487.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   控件   面板   ASP   Net

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!