Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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>


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!
Posted

Hi,


Check this[^]


and below code

XML
<%@ 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();
    }
}
 
Share this answer
 
Comments
DominicZA 13-Jul-11 9:24am    
Works, kind of. I need to keep adding my custom text box as many times as its needed. I removed the line that clears the placeholder, the problem is when ever that after it adds the second control it keeps overwritting that control? Now what?
I found it here
 
Share this answer
 
Comments
Syed Salman Raza Zaidi 13-Jul-11 5:33am    
but i think ur problem has now resolved
Monjurul Habib 13-Jul-11 6:01am    
why you are asking me?? ask OP :)
DominicZA 13-Jul-11 10:05am    
Works, kind of. I need to keep adding my custom text box as many times as its needed. I removed the line that clears the placeholder, the problem is when ever that after it adds the second control it keeps overwritting that control? Now what?
Monjurul Habib 13-Jul-11 11:18am    
Share your recent code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900