I have to find a panel in content page and need to add a drop downlist on that panel.I have searched but i got only for adding controls to master page.Below is my code, Note:I have to add control from that page itself not from master page
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Salesorder.aspx.cs" Inherits="Salesorder" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
<script src="assets/plugins/jquery-1.10.2.js"></script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<div class="row">
<asp:Panel ID="Panel1" runat="server">
</asp:Panel>
</div>
</asp:Content>
I need to add another panel to Panel1 from codebehind.
protected void Page_PreInit(object sender, EventArgs e)
{
//Create a Dynamic Panel
Panel pnlDropDownList;
pnlDropDownList = new Panel();
pnlDropDownList.ID = "pnlDropDownList";
pnlDropDownList.BorderWidth = 1;
pnlDropDownList.Width = 300;
ContentPlaceHolder cph = (ContentPlaceHolder)this.Page.FindControl("ContentPlaceHolder1");
Panel panel = (Panel)cph.FindControl("Panel1");
cph.Controls.Add(pnlDropDownList);
}
Yes, you need to find your control in Master instead of Page.
Like below.
ContentPlaceHolder cph = (ContentPlaceHolder)this.Master.FindControl("ContentPlaceHolder1");
this should work.
Add a PlaceHolder control in the panel you want the new DropDownList panel to be added in the aspx page
<asp:Panel ID="Panel1" runat="server">
<asp:PlaceHolder ID="placeholder1" runat="server"></PlaceHolder>
</asp:Panel>
In the code behind, to add the control to the page use:
placeholder1.Controls.add(pnlDropDownList);
I can't understand why don't you add controls like this
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
<asp:Panel ID="Panel1" runat="server">
<asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
</asp:Panel>
</asp:ContentPlaceHolder>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.