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>
Related
I have a master page Header.master which contains a logout button, due to this reason I have to include a form with run at server in my master form.
Again I am using a form in my page because I want to use controls and asp.net compiler says that you cant use controls with form tag with run at server.
Header.master
<form runat="server"><asp:ImageButton ID="btn_logout" runat="server" OnClick="btn_logout_Click" Height="22px" ImageUrl="~/img/logout.png" Width="43px" /></form>
My Page
<%# Page Title="" Language="C#" MasterPageFile="~/Header.Master" AutoEventWireup="true" CodeFile="student_registration.aspx.cs" Inherits="SCMS.student_registration" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<form id="frm_sr" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
</asp:UpdatePanel>
</form>
</asp:Content>
Remove Form tag from your page (My Page)
<form id="frm_sr" runat="server">
Form tag in master page is already enclosing all the code written in page itself
(<asp:Content ID=...>)
I'm working on web application based on ASP.Net and C#.
When i click the button in Content page, I want to pass data(string) to Masterpage and show it to the user. I know the technique and codes but the problem is with Ajax.
Since the button in content page is inside of Updatepanel, when i click the button, message will not be appear in masterpage. Because only part of the page wil be refrsh and i do not know, how to refesh master page
Code inside contentpage:
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder_Body_Member" ...>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:TextBox ... ></asp:TextBox>
<asp:Button ID="ShowText" ... />
</ContentTemplate>
</asp:UpdatePanel>
Code inside masterPage (actually this is a nested masterPage):
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder_TopRightSide" runat="server"> ... </asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder_Menu" runat="server"> ... </asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder_Body" runat="server">
<asp:ContentPlaceHolder ID="ContentPlaceHolder_Body_Member" runat="server"> </asp:ContentPlaceHolder>
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="ContentPlaceHolder_Footer" runat="server">
<asp:UpdatePanel runat="server" id="UpdatePanel" updatemode="Conditional">
<ContentTemplate>
<asp:Label ID="DataFromPage" ... ></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
Button is insde of "ContentPlaceHolder_Body_Member" and the label is inside of "ContentPlaceHolder_Footer"
To achieve the result that you need you just need to make a little change on your Master page design like the code below:
On your master-page place the content-place-holder inside the update panel first:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="DataFromPage" runat="server" Text="Label"></asp:Label>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server"></asp:ContentPlaceHolder>
</ContentTemplate>
</asp:UpdatePanel>
Then in your Content page you will have something like this (almost the same as yours)
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
Finally in your code-behind on button-click event you could assign your value to the master page's label control like the code shown below:
protected void Button1_Click(object sender, EventArgs e)
{
Label myLabel = Page.Master.FindControl("UpdatePanel1").FindControl("DataFromPage") as Label;
myLabel.Text = "this is a message from my content page";
}
And here is the result :
UPDATE 1
Ok remember if you have a nested update panel then the way you find the control in that one is a bit different also if you need to make changes on the other content page you need to force the update panel to make the changes in your code behind, so your code should be like this:
//your button control's event needs to find the update panel and the label both:
protected void Button1_Click(object sender, EventArgs e)
{
UpdatePanel myUpdatePanel = Page.Master.Master.FindControl("ContentPlaceHolder_Footer").FindControl("UpdatePanel") as UpdatePanel;
Label myLabel = Page.Master.Master.FindControl("ContentPlaceHolder_Footer").FindControl("DataFromPage") as Label;
myLabel.Text = "this is a message from my content page";
myUpdatePanel.Update(); //force the panel to get updated
}
Cheers
I have written an aspx page in c# that uses a master page for a menu. On the child page, I have a scriptmanager control with an update panel. Everything works fine but the master page menu is displayed as a table instead of the menu format. Once the scriptmanager is removed from the child page, the menu is restored.The scriptmanger code is the first item under the content2 section.
<asp:Content ID = "Content2" ContentPlaceHolderID = "MainContent" runat="server">
<h1> Test page 1</h1>
<asp:ScriptManager ID="ScriptManager" runat="server" />
<asp:UpdatePanel ID="UpdatePanel" runat="server" />
<ContentTemplate>
Any help would be appreciated. Thanks!
In your master page at the top try this:
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager> // remove this script manager if you have included script manager in your other page.
// do other stuff.
</form>
</body>
For separate pages include your master file and try this:
<%# Page Title="" Language="C#" MasterPageFile="~/yourmasterfile.Master" AutoEventWireup="true"
CodeBehind="yourfile.aspx.cs" Inherits="yourproject.yourfile" culture="auto" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MasterPageContent" runat="server">
<asp:ScriptManager ID="ScriptManager2" runat="server">
</asp:ScriptManager> // remove this script manager if you have included script manager in master page
<asp:UpdatePanel ID="updatePannel1" runat="server">
<ContentTemplate>
// do your stuff here
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
I have 2 master pages that are nested.this is main master page code for example:
<%# Master Language="C#" AutoEventWireup="true" CodeFile="MasterPageMaster.master.cs" Inherits="MasterPageMaster" %>
<!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></title>
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtMasterPageMaster" ClientIDMode="Static" runat="server"></asp:TextBox>
<div style="background-color:Aqua;height:40px;">
Some Text
</div>
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
and the nested master page:
<%# Master Language="C#" MasterPageFile="~/MasterPageMaster.master" AutoEventWireup="true"
CodeFile="MasterPageNested.master.cs" Inherits="MasterPageNested" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<asp:Panel runat="server" ID="panelMain" BackColor="lightyellow">
<h2>
Child master</h2>
<asp:Panel runat="server" ID="panel1" BackColor="lightblue">
<p>
This is child master content.</p>
<asp:ContentPlaceHolder ID="ChildContent1" runat="server" />
</asp:Panel>
<asp:Panel runat="server" ID="panel2" BackColor="pink">
<p>
This is child master content.</p>
<asp:ContentPlaceHolder ID="ChildContent2" runat="server" />
</asp:Panel>
<br />
</asp:Panel>
</asp:Content>
and I create a page based on this nested master page:
<%# Page Title="" Language="C#" MasterPageFile="~/MasterPageNested.master" AutoEventWireup="true" CodeFile="PageMasterPageNested.aspx.cs" Inherits="PageMasterPageNested" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ChildContent1" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ChildContent2" Runat="Server">
<asp:Button ID="Button1" runat="server" Text="Button" Height="66px"
onclick="Button1_Click" Width="196px" />
</asp:Content>
I want in click of Button1 get text of main master page.
How I can do this?
In PageMasterPageNested.aspx:
TextBox txtBox = this.Master.Master.FindControl("txtMasterPageMaster") as TextBox;
Should work. Give it a try. Hope it helps.
This works in any case. especially if you dont know or care how many master pages you nested. Hope it helps :)
MasterPage tmp = this.Master;
while (tmp.Master != null)
{
tmp = tmp.Master;
}
var control = tmp.FindControl("form1");
I am building an ASP.NET application and would like to use a master page to display branding and navigation info and move through several content pages. However, every time I try to load new content the entire page flickers. Is there anyway to avoid this?
Master page:
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="Interface.Site" %>
<%# Register TagPrefix="customControl" TagName="NavigationBar" Src="NavigationControl/NavigationControl.ascx" %>
<!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>Registration</title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
<link href="Stylesheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="RegistrationMasterForm" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager" runat="server" EnablePartialRendering="true">
</asp:ScriptManager>
<%-- Contents of this div span the top of the page for branding --%>
<div id="BrandingBanner">
<asp:Image ID="Banner" runat="server"
ImageUrl="Images/BrandingBanner.png"/>
</div>
<%-- Navigation Bar, the contents of this div should remain unchanged --%>
<div id="NavigationBar">
<customControl:NavigationBar ID="navBar" runat="server"/>
</div>
<%-- Contains any forms or controls that may be uniquie to the page --%>
<div id="FormControls">
<div id="FormContent">
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</div>
</div>
</form>
</body>
</html>
Content:
<%# Page Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="Interface.WebForm2" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
Check me out! I am content for the master page!
<asp:Button ID="Button1" runat="server" Text="Next" onclick="Button1_Click" />
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
content code behind:
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("WebForm3.aspx");
}
}
The control event triggers a post back to your page, which typically creates the "blanking" effect that you see. To mitigate this, you may want to consider using some sort of Ajax solution to cause a partial postback or async postback to prevent the "blanking".
However in the particular case of your button click, if all you are trying to do is bring the use to another page, you really should just use an <a href="WebForm3.aspx"> tag and avoid using Response.Redirect.
You are redirecting the page in your codebehind.
This causes the browser to change pages, and redraw the whole thing, at least IIRC. I haven't used updatepanels in ages.
Isnt this only possible if you're using frames? As far as I'm aware the master page and content pages bind together on the serverside and push down the the client as a whole.
Where what you want sounds like using an page for branding and navigation with an inline frame to serve up your convent. This would prevent the outer page from "flickering" on the client side.
Edit: Though it is not the new way to do things. You'd want to look at something using maybe an UpdatePannel or other AJAX style design.
You don't need to move the updatepanel, your redirect is causing the browser to load a new page.