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.
Related
I'm making an ASP.NET application that performs some globalization in the Render event of the Master Page. Basically my code uses regular expressions to look for placeholders (text patterns, not actual PlaceHolder controls) in the markup and replaces them with the corresponding translations found in an XML file.
This works as intended when the page is loaded normally, but on a partial postback the Render event does not seem to trigger at all for the Master Page, leaving the rendered page full of placeholders. The event does trigger for the current Page however, but I don't want to alter the output HTML separately in every single page's Render event.
The ContentPlaceHolder (containing the current Page) of the Master Page is located inside an UpdatePanel, and I assume this has something to do with what is happening. I still don't understand why the event is not triggered, however.
Is there a way to force the Master Page Render event to trigger on partial postback, or should I consider a different solution?
I have Implemented the Globalization in my Page. I have specified the Flags for changing one language to another.
When I click the Flag I am setting the Application["LangId"]="1"; Now the content of the master page is refreshing but the content of the child page does not refresh. Because I have called setting the Language in every child Page load. According to the terminology Child page load fires first and after Master page load and the Click event. If I call the Language setting method in click event I will get the refreshed content.
My question is how we can call the child page method in master page button click event.
You can define a property in your master.
public flag{get;set;}
And load it in page load. Then you can access that from child pages like this:
var flag=((MasterPage)Master).flag;
If you load your child pages this way, they will be refreshed whenever that property changes on MasterPage (if page is refreshed of course)
Got a very frustrating problem that I'm trying to solve regarding C#.NET's way of handling dynamically added usercontrols.
The basic rundown of my page setup is that I have an ASPX page which has a Master Page. The Master Page has some content placeholders. On the codebehind in the ASPX page, I call the Master.FindControl method to find the content placeholders, and then use the LoadControl method to load a UserControl into the placeholder.
On the UserControl, I have a series of textboxes, and a submit button. In this implementation, when I click on the submit button, and put a breakpoint into the OnClick event, I find that the OnClick event is never called.
However, if, instead of using this approach, I remove the Master Page, and add the content placeholders directly on the ASPX page, then use Placeholder.Controls.Add to add the UserControl instead of Master.FindControl, the button works perfectly.
Does anyone have any suggestions as to how I can resolve this without removing the Master Page?
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());
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.