Search code examples
c#asp.netmaster-pages

How to rebind a repeater inside master page from a content page?


Is it possible to rebind a repeater (inside master page) from content page?
My pages are base on master and content pages and I have some links (anchors) for download files!

After click on those links page_load of content page fires, and will show download window. But we will never get page_Load of master page and in master page I have a summary for showing download counts.

How can I rebind that summary (inside master page) from content page before showing download window?


Solution

  • Yes, it's pretty simple. You can 'find' the control from the content page. Here's a sample where i'm binding to a GridView control.

    Master:

    <asp:GridView runat="server" ID="gridViewMaster" AutoGenerateColumns="true" />
    

    Content Page:

    var gridView = (GridView) Master.FindControl("gridViewMaster");
    
    gridView.DataSource = dt;
    gridView.DataBind();
    

    Just replace the grid view object and control id with your repeater...and bind it to whatever object you want.

    Edit - Here's the code to find a server side div:

    var divMaster = (System.Web.UI.HtmlControls.HtmlGenericControl)Master.FindControl("divMaster");
    divMaster.InnerHtml = "<h2>Hello World</h2>";