将自定义控件添加到ASP.Net面板 [英] Add custom control to ASP.Net Panel

查看:73
本文介绍了将自定义控件添加到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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/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面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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