How to rebind a repeater inside master page from a content page? - c#

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?

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>";

Yes. You have access to the master page from your web form—using this.Master—which you'd then have to cast as the appropriate type. From there you can access any public methods or properties you've defined.
Simple add a ReBind method there that does what you want, and you should be good to go.
EDIT
It would be something like:
(this.Master as WhateverTypeYourRealMasterPageIs).ReBind();

You can expose the master page control to the children. Make the control public with an accessor in the master page... such as:
public Repeater MasterpageRptr {get;set;}
Then on your child page, add the MasterType definition:
<%# Page Language="C#" Title="Content Page" MasterPageFile="~/MyMaster.master"%>
<%# MasterType TypeName="MyMaster" %>
(where MyMaster is the master page class file)
Then you can call this in your child code-behide using the accessor.
.
.
Please vote if helpful.

The proper way to do so is for the user control to fire an event that the Master page subscribes to. Let me know if you need some sample code
Some sample code:
In your user control, add the following event:
public event EventHandler RefreshRequested;
The user control will throw this event whenever it wants a refresh by calling the following method:
private void OnRefreshRequested()
{
//make sure the event is being listened to. no point raising an event if no one cares!
if (this.RefreshRequested != null)
{
this.RefreshRequested(this, EventArgs.Empty);
}
}
Now, the master page will subscribe to the user control's event and acts accordingly. Subscribing to the event is just like subscribing to any other event (ie: Button_Click).
Let me know if this clears things up.

Related

MasterPage Page_Load hits before Grid_ItemCommand

I am using the session object to store success/error messages based on user actions.
On each postback, the message is set on ItemCommend and retrieved on the Page_Load of the master page. Once retrieved, the message is deleted from the session.
The problem is that the master page's Page_Load gets called before the ItemCommand gets called so the message does not show up until the next refresh or postback.
How is this situation normally handled? Is there some other event we can code against?
It is normal behavior of aspx and master pages. First of all content page's page load get fired after that Master Page's pageload get fired and then all other click etc.
You can use PageLoad Complete event to Solve your problem.
this is a normal behavior. show your message on itemcommand or Page_prerender
Create a Public Method in Code Behind of your master page like this:
public void Set_Value(String SessionValue)
{
//your code here
}
In aspx File of your content Page, Use following Line of code:
<%# MasterType VirtualPath="~/MasterPage.master" %>
Now in Code Behind of your Content Page, You can easily call Master Page's method in Item event of your any Control. In your Master Page's method you can write your required code to update and show values.
Call Master Page's Method on Content Page Like this:
this.Master.Set_Value(Session["abc"].ToString());

access dropdown control from the master page to the content page using asp.net

i have a master page and the content pages in the master page i have the textbox and dropdown
the value in the dropdown may vary according to the content pages
e.g for one content page the dropdown may contain
branchname,
city,
address
and let for other content page under same master page the dropdown may have values like
Contactnumber,
EmailID,
...........
.........
etc.....
so please help me to how can i bind that dropdown from my content page
thanks.
Please do as follows:
I have created a property in the master page and called that on the content page as:
public DropDownList SearchList
{
get { return ddlFilterText; }
}
and on the content page:
// place here the datasource through which the dropdown is biding
Master.SearchList.DataSource = null;
Master.SearchList.DataBind();
You could go and look for the dropdown control from your content page like this:
var yourdropdown = Page.Master.FindControl("ID of the dropdown") as DropDownList;
But be aware of that the masterpage's load event is succeeded by the page's load event so you dont double bind it.
If the values in the drop-down are dictated by the individual content pages, then my suggestion would be to store these in cache, or session, or some state-persistence mechanism.
The cleanest way to accomplish this is by adding a MasterType directive to each of your content pages.
<%# MasterType virtualpath="~/Masters/Master1.master" %>
Then from your codebehind in the content pages you can reference the Master Page controls like so:
DropDownList myDropDownList = Master.TheDropDownList;
Andreas is correct in that you need to be aware of the ASP.NET Event Life Cycle, so you actually bind the dropdown at the correct time.
It's pretty simple. You can 'find' the control from the content page.
Here's a sample where i'm binding to a DropDown control.
Master:
<asp:DropDownList runat="server" ID="ddlMaster" DataTextField="Name" DataValueField="Id" />
Content Page:
var ddlMaster = (DropDownList)Master.FindControl("ddlMaster");
ddlMaster.DataSource = dt;
ddlMaster.DataBind();
If you need to edit a textbox on the master page, you can also find that control and make changes to it from the content page.

accessing control on page A.ASPX from a WEBUSERCONTROL.ASCX

How can I access a control on page a.aspx from a webusercontrol.ascx
I do not know the technical term used to describe this, if any,
the webusercontrol.ascx page contains a button.
onclick of the button, placeholder on main page must display the "required content".
if this were on the same page no problem.
but how to access across pages?
Expose an event on your ASCX control, subscribe an event handler method on the ASPX page to the event on that page's instance of that control, implement the method to make the required changes to the parent page.

Handling MasterPage event in User Control of Content page

On my master page , I have "Search textbox" and "Search Button".
On My content page , I have a "User Control" which has a "GridView".It shows some data about Vendors.
Also, on this User Control's Page Load, i have code written to display all vendors in GridView.
Now, when user enters Vendor Number in "Search textbox" , and hits "Search Button" , i want to handle this event inside my User Control.
How to do this ?
Please help me. Thanks in advance.
Note : i know how to handle the event in content page but not sure how to handle it inside user control placed on content page.
You just need to add logic that passes in the Search Parameters to the User Control.
On the User Control, make a public method to Bind the grid that takes in the search text
public void BindGrid{string searchText)
{
//get datasource with the searchText used as a Where, or whatever suits your current situation
//bind grid
}
Then, on the MasterPage, you should have something like
protected void btnSearch_Click(object sender, EventArgs e)
{
UserControl1.BindGrid(tbSearchText.Text);
}
You just need to make sure that your UserControl doesn't bind data on the PageLoad event if IsPostBack is true. Otherwise, you'll be binding data twice.
If you know how to handle the event in the content page, you can apply that same approach to the control. It will still be the content page that wires the control's handler to the master page's event, since the content page is the entity that knows and can access both the master page and control.

How to access a user control in a masterpage from a content page?

Lets say that I have a header user control in a master page, and want to change a property of the user control depending on what content page is loaded inside of the master page. How might I go about this?
Thanks!
You can use two methods. The first is by using Page.Master.FindControl('controlID'). Then you can cast it to the type of your user control. The second method is by adding a <%# MasterType VirtualPath=""> OR <%# MasterType TypeName=""%> tag to your aspx page. In the VirtualPath add the virtual path to the master page, or the class in the TypeName. You can then access everything with intellisense.
first find the user control in the masterpage as below.Then find the control you need to access their property.
UserControl uch = Page.Master.FindControl("ucHeader1") as UserControl;
PlaceHolder phProxylist= ucHeader1.FindControl("phProxy") as PlaceHolder;
DropDownList ddlproxylist1 = ucHeader1.FindControl("ddlProxyList") as DropDownList;
phProxylist.Visible = false;
Hope this helps.
There's one other method, and that's by making a public property on the master page that exposes the user control.
Using a public property would work. In the content page's FormLoad method, you could do something like this (VB):
Dim myMaster as MyMasterPage = CType(Page.Master, MyMasterPage)
myMaster.MyUserControl.Text = "Hello!"

Categories

Resources