Solved

ASP.net content page event called before page load

Posted on 2008-06-19
6
1,165 Views
Last Modified: 2013-11-07
I have a master page with a content page container.  On the master page I have a toolbar.  When a button is clicked on the toolbar I raise an event and then capture it in my content page.  This part works perfectly.  My problem is that Iwoudl like to perform some action on the content page based on whcih toolbat buttobn the user clicks on the master page.  For example, I woudl liek to set some text on the content page to what the text is on the toolbar button they click.  My problem is that the page load event of the content page is called before my custom event handler.  So when I set the text on the form it never gets updated.

Is there any way areound this?
0
Comment
Question by:akobes05
  • 4
6 Comments
 
LVL 18

Expert Comment

by:Richard Lee
Comment Utility
You may want to have an event in your master page that your content pages can attach to (Event, Delegates). The event handler events will be raised within toolbar button event. This should allows the desired result you wish.
0
 
LVL 6

Expert Comment

by:rafayali
Comment Utility
Umm, not sure . Can you paste some code? It is sometimes easier to look at code then try to depict through words.
0
 
LVL 18

Expert Comment

by:Richard Lee
Comment Utility
You might want to have a look at these links:

http://www.codeproject.com/KB/cs/events.aspx
http://msdn.microsoft.com/en-us/library/17sde2xt(VS.71).aspx

If you haven't cracked it I willcontinue the post in the morning. I'm off to bed since its 00:30.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:akobes05
Comment Utility
I am using delegates.  I do not knwo why the text of label1 is always blank.  LIek I said the event is raised before the page_load.

Here is the code for my master/content  pages:

>> MASTER <<

Public Delegate Sub MasterPageToolbarClickHandler(ByVal sender As Object, ByVal Idx As Integer)

Partial Class MasterPage
    Inherits System.Web.UI.MasterPage
    Public Shared Event ToolbarClick As MasterPageToolbarClickHandler

    Protected Sub Toolbar_ButtonClicked(ByVal sender As Object, ByVal be As Infragistics.WebUI.UltraWebToolbar.ButtonEvent) Handles Toolbar.ButtonClicked

        Select Case be.Button.Key
            Case "Button1"
                OnToolbarClick(1)
            Case "Button2"
                OnToolbarClick(2)
            Case "Button3"
                OnToolbarClick(3)
            Case "Button4"
                OnToolbarClick(4)
            Case "Button5"
                OnToolbarClick(5)
            Case "Button6"
                OnToolbarClick(6)
            Case "Button7"
                OnToolbarClick(7)
        End Select

    End Sub

    Protected Overridable Sub OnToolbarClick(ByVal idx As Integer)
        RaiseEvent ToolbarClick(Me, idx)
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    End Sub


>> CONTENT <<


Imports Infragistics.WebUI.UltraWebToolbar
Imports IBM.Data.DB2.iSeries
Imports System.Data

Partial Class Login
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

       
        If Not Page.IsPostBack Then
            SetupToolbar()
        End If
    End Sub

    Private Sub SetupToolbar()
        Dim Toolbar As UltraWebToolbar

        Toolbar = Master.FindControl("Toolbar")
        With Toolbar
            .Items(.Items.IndexOf("Button2")).text = "A"
            .Items(.Items.IndexOf("Button3")).text = "B"
            .Items(.Items.IndexOf("Button4")).text = "C"
            .Items(.Items.IndexOf("Button5")).text = "D"
        End With

        AddHandler MasterPage.ToolbarClick, AddressOf ToolbarClicked
    End Sub


    Private Sub ToolbarClicked(ByVal sender As Object, ByVal Idx As Integer)

        Dim Toolbar As UltraWebToolbar

        Select Case Idx
            Case 1
                Label1.Text = Idx
            Case 2
                Label1.Text = Idx
            Case 3
                Label1.Text = Idx
            Case 4
                Label1.Text = Idx
            Case 5
                Label1.Text = Idx
            Case 6
                Label1.Text = Idx
            Case 7
                Label1.Text = Idx
        End Select


    End Sub
End Class
0
 
LVL 18

Accepted Solution

by:
Richard Lee earned 500 total points
Comment Utility
Okay here is the sample as promised mate. Since I am having trouble attaching the ZIP file because of the code file (*.cs) within it. You should have no problem converting to VB since there is only a few lines of c#.

If you need me to send this file as a ZIP then let me know how. VS2008 Solution. I can convert to VS2005 if you need.

I have put the code for the following files:

~/App_Code/Common.cs
~/Masters/BaseMasterPage.master
~/Masters/BaseMasterPage.master.cs
~/Default.aspx
~/Default.aspx.cs
Common.cs

--------------------
 

using System;
 

/// <summary>

/// The delegate defines the parameters sent to the event handlers

/// </summary>

public delegate void CustomEventHandler(object o, EventArgs e);
 
 

BaseMasterPage.master (located: ~/Masters)

--------------
 

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="BaseMasterPage.master.cs" Inherits="BaseMasterPage" %>
 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>Untitled Page</title>

    <asp:ContentPlaceHolder id="head" runat="server">

    </asp:ContentPlaceHolder>

</head>

<body>

    <form id="form1" runat="server">

    <div style="width: 100%;">

		<div style="float: left; padding: 5px; width: 100%; background-color: Silver; color: Green; font-weight: bold; text-align: left;">

			<asp:Label ID="SampleText" runat="server"></asp:Label>&nbsp;

		</div>

		

		<div style="height: 5px;">&nbsp;</div>

		

		<div style="float: left; padding: 5px; font-weight: bold; color: Blue; width: 100%; text-align: left; vertical-align: top; border: dashed 1px Black;">

			<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">

        

			</asp:ContentPlaceHolder>

		</div>

		

		<div style="height: 5px;">&nbsp;</div>

			

		<div style="float: left; padding: 5px; width: 100%; background-color: Silver; color: Black; font-weight: bold; text-align: left;">

			<asp:Button ID="Submit" runat="server" Text="Submit" OnClick="Submit_Click" />

		</div>

    </div>

    </form>

</body>

</html>
 
 

BaseMasterPage.master.cs

-------------
 

using System;
 

public partial class BaseMasterPage : System.Web.UI.MasterPage

{

	protected void Page_Load(object sender, EventArgs e)

	{
 

	}
 

	public event CustomEventHandler btnUpdateEvent;
 

	protected void Submit_Click(object sender, EventArgs e)

	{

		SampleText.Text = String.Format("Master Page: {0} {1}", DateTime.Now.ToLongDateString(), DateTime.Now.ToLongTimeString());
 

		// Raise events

		if (btnUpdateEvent != null)

			btnUpdateEvent(sender, e);

	}

}
 
 

Default.aspx

--------------
 

<%@ Page Language="C#" MasterPageFile="~/Masters/BaseMasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Title="Untitled Page" %>
 

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">

</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

	<asp:Label ID="ContentPageText" runat="server"></asp:Label>

</asp:Content>
 
 

Default.aspx.cs

----------------
 

using System;
 

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

		BaseMasterPage currentMasterPage = (BaseMasterPage)this.Master;

		currentMasterPage.btnUpdateEvent += new CustomEventHandler(UpdateText);

    }
 

	public void UpdateText(object o, EventArgs e)

	{

		ContentPageText.Text = String.Format("Content Page: {0} {1}", DateTime.Now.ToShortDateString(), DateTime.Now.ToShortTimeString());

	}

}

Open in new window

0
 
LVL 18

Expert Comment

by:Richard Lee
Comment Utility
I have ZIP the website and placed it here: http://www.avantprime.com/Profile/Richard/Samples/EE-ID23500816-Website.zip

I have t included the solution file therefore you need to select open exisitng website from visual studio.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now