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
Related
I have below structure in my asp.net application.
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<div id="overlay"></div>
<asp:Panel ID="pnlMain" runat="server" Visible="true">
<asp:UpdatePanel ID="upProfile" runat="server" UpdateMode="Always">
<ContentTemplate>
<div>
<table>
</table>
</div>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="upAttachment" runat="server" UpdateMode="Always">
<ContentTemplate>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Panel>
<asp:Panel ID="pnlSuccessMessage" runat="server" >
<asp:Label ID="lblSuccessMessage" runat="server"></asp:Label>
</asp:Panel>
I have different textfields and dropdowns in the table of upProfile updatepanel and file upload control in upAttachment updatepanel. Now when I select one radio from the same update panel, I am getting below error and the upAttachment does not show up on page.
Uncaught Error: Sys.InvalidOperationException: Could not find UpdatePanel with ID 'MainContent_upAttachment'. If it is being updated dynamically then it must be inside another UpdatePanel.
I have searched a lot and tried below different things, but still it is not working
1. Set the upAttachment UpdateMode to Conditional.
- In this case, it does not throw error, but the file upload control which is inside that updatepanel, it does not show up
2. Place the upAttachment updatepanel inside another updatepanel, but that too is not working.
I don't know what to do in this case. Can anyone help me on this?
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>
I have the below Place holder between the head tags on a master page which all pages at some stage inherit from:
<asp:ContentPlaceHolder ID="cphHead" runat="server" />
I then have the below content on a page that inherits from the master page:
<asp:Content ID="Content1" ContentPlaceHolderID="cphHead" Runat="Server">
<asp:Literal ID="litMetaTitle" runat="server" />
<asp:Literal ID="litMetaDescription" runat="server" />
<asp:Literal ID="litMetaKeywords" runat="server" />
<asp:ContentPlaceHolder ID="CphHeaderContent" runat="server" />
</asp:Content>
As you can see this has another place holder in it, from this page inherits another page with the below content:
<asp:Content ID="ContentHeader" runat="server" ContentPlaceHolderID="CphHeaderContent">
<asp:Literal ID="TheId" runat="server" />
</asp:Content>
The literal is being built with a string builder in the code behind:
private void xxx(Order order)
{
//...Building string with string builder
TheId.Text = xxx.ToString();
}
This literal is not appearing in the head of this page when I view it in the browser? Does anyone have any idea as to why? If I add something like below between the content tags:
<asp:Content ID="ContentHeader" runat="server" ContentPlaceHolderID="CphHeaderContent">
<asp:Literal ID="TheId" runat="server" />
<script src="/Media/Javascript/xxx.js"></script><--ADDED!
</asp:Content>
The <script src="/Media/Javascript/xxx.js"></script> will appear in the head? Its just the literal that wont? Why is this?
I have a page that works fine with multiple a grid and multiple buttons. The page works fine until I add an asp:UpdatePanel. Then I get the following message pushing any of my buttons:
Sys.WebForms.PageRequestManagerServerErrorException: Input string was not in a correct format.
There is no javascript on the page just straight html.
Here is the page:
<%# Page Title="" Language="C#" MasterPageFile="~/MasterPages/Site.Master" AutoEventWireup="true"
CodeBehind="TestUpdatePanel.aspx.cs" Inherits="ASCWeb.TestUpdatePanel" %>
<asp:Content ID="mHeadContent" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="mBodyContent" ContentPlaceHolderID="MainContent" runat="server">
<asp:UpdatePanel ID="pnlUpdate" runat="server">
<ContentTemplate>
<asp:TextBox ID="txtUser" runat="server" />
<asp:ImageButton ID="btnAdd" runat="server" ImageUrl="~/Images/Add.png" />
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
If I take the TextBox out, it works fine. Nothing is in the code behind.
What would cause this?
Thanks
As per experience, I only encounter that exception when calling Javascript from codebehind, like when using ScriptManager.RegisterClientScriptBlock() to call window.alert(), for instance. But for this issue, I think this resolves it: http://forums.asp.net/t/1823287.aspx/2/10.
I'm having trouble with a combination of an UpdatePanel and MultiView.
I have an UpdatePanel at my top level, inside I have a bunch of imagebuttons - their click events set the view on the Multiview, and each View has an UpdatePanel inside of it with my binding.
Everything works great - but I am trying to set the View via the Querystring, so I can send a user to a particular view.
When I try and set the View from PageLoad - it says 'object does not exist'. So I figured I'd try it in Page_LoadComplete, which works great - but then my imagebuttons don't work to switch the view like they originally did.
What am I missing! Thanks!
void Page_LoadComplete()
{
tabSelect= Request.QueryString["tab"];
if (tabSelect.Contains("Community"))
{
MultiView1.SetActiveView(Community);
btnCommunity.ImageUrl = "images/tabCommunity_on.png";
}
}
<asp:ScriptManager id="ScriptManager1" runat="server"/>
<asp:UpdatePanel id="UpdatePanel1" childrenastriggers="true" updatemode="Always" runat="server">
<ContentTemplate>
<asp:ImageButton id="btnCommunity" onclick="" runat="server">
<asp:MultiView ID="MultiView1" ActiveViewIndex="0" runat="server">
<asp:View ID="Community" runat="server">
<asp:UpdatePanel id="UpdatePanel1" childrenastriggers="true" updatemode="Always" runat="server">
//data controls in here
</asp:UpdatePanel>
</asp:View>
<asp:View id="tabFriends" runat="server">
<asp:UpdatePanel id="UpdatePanel2" childrenastriggers="true" updatemode="Always" runat="server">
//data controls in here
</asp:UpdatePanel>
</asp:View>
</asp:MultiView>
</ContentTemplate>
</asp:UpdatePanel>
UPDATE: After reviewing your code in further detail, I believe figured out the problem.
I made the following adjustments to the code:
If the querystring is not passed, set tabSelect to empty string and thus avoiding a null object reference exception on the next line.
Set the ImageUrl path to include ~ (for root).
Please try the code below:
void Page_LoadComplete()
{
string tabSelect = Request.QueryString["tab"] ?? string.Empty;
if (tabSelect.Contains("Community"))
{
MultiView1.SetActiveView(Community);
btnCommunity.ImageUrl = "~/images/tabCommunity_on.png";
}
}