Parsa
Parsa

Reputation: 1309

Post Data from contentpage to Masterpage using ajax technique

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"

Upvotes: 0

Views: 747

Answers (1)

Ali
Ali

Reputation: 2592

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 :

enter image description here

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

Upvotes: 1

Related Questions