Can I bubble up event from Master page to ASPX - asp.net

Can I bubble up a button click event of a button in master page to be handled by an event handler in the aspx page ?

You can expose the event handler and hookup to it, like this:
In the master:
public event EventHandler ButtonClick
{
add { ButtonThatGetsClicked.Click += value; }
remove { ButtonThatGetsClicked.Click -= value; }
}
In the page:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
((MyMasterType)Master).ButtonClick += MyHandler;
}
private void MyHandler(object sender, EventArgs e)
{
//Do Something
}
Also, you can avoid the Master type cast and have it already appear in intellisense as your Master's type by using the #MasterType directive in the aspx markup.

You can rebroadcast the event. Declare a new corresponding event in your master page, such as HelpClicked and then aspx pages that use this master can subscribe to the event and handle it appropriately. The master can also take a default action if there are no subscribers (or use an EventArgs with a Handled property or something like that).

Related

Custom Image Button control click handler not firing

I've got a bit of an issue with creating a new control based on ASP.NET's ImageButton control. Everything works as expected, except for the click handler that is being hooked up in the control's OnInit override. Basically, clicking the custom image button just refreshes the page, never hitting the handler.
Now, I know this is something stupid I've done or just not understood, but I can't for the life of me figure this out. All the articles, questions and forum posts I've found on event handling issues for controls is for child controls, rather than ones that inherit from existing control types and have their own predefined handlers.
The following code is what I've written:
public class WebPaymentButton : ImageButton
{
public string DisabledImageUrl { get; set; }
public string TermsAcceptClass { get; set; }
protected override void OnPreRender(EventArgs e)
{
Page.ClientScript.RegisterClientScriptResource(typeof (WebPaymentButton), "PaymentModule.Scripts.WebPaymentButtonScript.js");
}
protected override void OnInit(EventArgs e)
{
CssClass = "WebPaymentButton";
if (!string.IsNullOrWhiteSpace(TermsAcceptClass))
{
Attributes["data-TermsClass"] = TermsAcceptClass;
}
if (!string.IsNullOrWhiteSpace(DisabledImageUrl))
{
Attributes["data-DisabledImageUrl"] = ResolveUrl(DisabledImageUrl);
}
Click += WebPaymentButton_Click;
base.OnInit(e);
}
private void WebPaymentButton_Click(object sender, ImageClickEventArgs e)
{
HttpContext.Current.Response.Redirect("http://dummy_payment_page_in_place_of_code", true);
}
}
I've tried hooking the handler up in the OnLoad and also switching it to run after the base.OnInit/OnLoad calls. Nothing has solved the handler issue. Can anyone point me in the right direction?
In case it helps, here is the markup for the button on the page:
<pm:WebPaymentButton runat="server" ImageUrl="~/pay-now.png" DisabledImageUrl="~/not-pay-now.png" TermsAcceptClass="TermsCheckbox" ID="MainPayButton" />
Have you tried overriding the OnClick event handler instead of hooking up to a new event handler?
Remove the Click += WebPaymentButton_Click line from OnInit and remove the WebPaymentButton_Click function, then add the following code to your class instead:
protected override void OnClick(ImageClickEventArgs e)
{
base.OnClick(e);
HttpContext.Current.Response.Redirect("http://dummy_payment_page_in_place_of_code", true);
}

can httphandler fire an event?

I want to check Session in some pages. To do this I am adding the page names which I want to check inside web.config as a appsetting key.
I want to use httpHandler with firing an event after it finds the session is empty or something else.
If I create httpHandler as a dll(another project) and add to a web site, can handler fire an event and web site capture it inside a web page?
What you can do is this:
Your HttpHandler puts a value in the HttpContext.Current.Items collection telling if there was Session or not. Something like
HttpContext.Current.Items.Add("SessionWasThere") = true;
You create a BasePage that checks that value in the Page_Load event and raises a new event telling so:
public abstract class BasePage : Page {
public event EventHandler NoSession;
protected override void OnLoad(EventArgs e){
var sessionWasThere = (bool)HttpContext.Current.Items.Add("SessionWasThere");
if(!sessionWasThere && NoSession != null)
NoSession(this, EventArgs.Empty);
}
}
In your page, you suscribe to that event:
public class MyPage : BasePage{
protected override void OnInit(){
NoSession += Page_NoSession;
}
private void Page_NoSession(object sender, EventArgs e) {
//...
}
}

ASP.NET: Page.Init woudln't fire

I have a custom ASP.NET control. In its Init handler I add a delegate to the Page's Init like this:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if(someCondition())
{
this.Page.Init += delegate(object sender, EventArgs ee)
{
//some stuff
};
}
}
Now, if I add this custom control to the HTML of the page declaratively, every thing works fine, the Page's Init delegate gets called. But if I add this control to the page programmatically like:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
MyControl myControl = new MyControl { ID = "myControl" };
this.Page.Form.Controls.Add(myControl);
}
The Init if the control get's called but the delegate that I attached to the Page.Init does not. What am I doing wrong here?
IT's because when adding the controls in OnLoad the Page's Init has already executed
Move the declaration of your custom control to page's OnInit instead of OnLoad. Instantiate your control and add it to the form before calling base.OnInit(e). This will give the page chance to load your control and actually attach your delegate to page's Init event before the Init gets called by the ASP.NET run time. Your problem is that page's Init was already called when your control's Init gets executed.
Instead of adding control in PageLoad, add it in Page_PreInit event handler.

.NET Public Events not wiring up in nested web user controls

I have C# Web Application that has an aspx page hosting a user control (Review.ascx). Inside that user control there are 5 more user controls, one of which has a public event (Review_Summary.ascx). The problem is no matter what i do I cannot get the event wired up in the parent ascx control (Review.ascx).
Here is what I have in the child control (Review_Summary.ascx)
public event EventHandler forwardStatusChanged;
#region methods
protected void btnForward_Click(object sender, EventArgs e)
{
if (btnForward.Text == "Return")
{
if (forwardStatusChanged != null)
{
forwardStatusChanged(sender, e);
}
removeForward();
}
}
In the parent control (Review.ascx) I have this
public void initReview(string EmployeeNumber)
{
RevSummary.forwardStatusChanged += new EventHandler(RevSummary_forwardStatusChanged);
<more code here>
}
protected void RevSummary_forwardStatusChanged(object sender, EventArgs e)
{
lblReadOnly.Visible = false;
}
RevSummary is the ID of the child control in the parent control. InitReveiw is a method that is called by the aspx page in its Page_Load event.
I get no errors on compile or at runtime. But when I click the button the forwardStatusChanged event is null. The "removeForward()" method that is called after that executes properly. So that fact that the event is always null leads me to believe that the wire up in the parent control is not working. However, I am sure it is executing becasue all of the code after that executes.
How can I figure out why this event is not wiring up?
Where is initReview being called from? Are you sure it's being called because the only reason this happens is that the event handler wasn't truly setup. I've never found a reason other than this, the several times I did this myself.
HTH.

asp.net page's preinit event

I am new to asp.net. I have an aspx page and i have to write some code in its PreInit event.
From where i find PreInit event on the page.
As we double click on button to get button click event(or selecting button and select event from property pane)
Plz reply me ASAP.
Man, why do you need the mouse?
If you need to write some code into PreInit just write the code:
protected virtual void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
//your code
}
or in class constructor add a event handler for it:
...
PreInit += new EventHandler(SomeMethodName)
...
and define the event handler method
private void SomeMethodName(object sender, EventArgs e)
{
//your code
}
And by the way, check a .Net Framework book and a Visual Studio manual.
You need to do some reading:
http://msdn.microsoft.com/en-us/library/ms178472.aspx#lifecycle_events

Resources