EziData Solutions

Web, NET and SQL Server

Raising Events from Master Pages

In the post on Adding Custom Events to Web User Controls we looked at how we could access events raised by controls within a Web User Control. This worked perfectly on our single default.aspx page, but in more complex websites using Master and Content Pages, we need a slightly different approach. In the first scenario, imagine we have a Web User Control located on a Master Page and we want the content to change when an event is raised.

To get started, review the previous posts. Then add a new Master Page called Default.Master to your project by selecting Add New Item from the Website menu.

 

Drag and drop the CustomDropDown Web User Control onto the design surface of Default.master. Put it above the ContentPlaceHolder as we will be changing the results selected in the ContentPlaceHolder depending on the value selected in CustomDropDown.

Next create a new blank web page called Results.aspx, making sure to select Default.master as the master page.

Next we will add a label on the design surface of Results.aspx. The label will be used to display the name selected in the CustomDropDown, similarly to how the label operated in Adding Custom Events to Web User Controls. To be able to access the selected value of CustomDropDown, we need to make a few changes.

Firstly, we need to add some extra code to create a constructor for our CustomDropDownEventArgs class so that we can raise events using delegation.

Public Sub New(ByVal EmployeeID As String, ByVal EmployeeName As String)
     _payrollID = EmployeeID
     _fullname = EmployeeName
End Sub

Raising the event in Default.aspx is similar to how we raised the event in the single page in previous posts. We need to create a method that handles ListChanged - for now, simply cut and paste the code from the previous post into the code behind for Default.master.

Private Sub CustomDropDown1_ListChanged(ByVal sender As Object, ByVal e As CustomDropDownEventArgs) Handles CustomDropDown1.ListChanged
     Me.Label1.Text = e.FullName
End Sub

Switch to source view and register the CustomDropDown control

<%@ Register TagPrefix="myControl1" Namespace="Acme" Assembly="App_Web_customdropdown.ascx.6bb32623" %>

Then add the custom control and a label that you will use to show that the event has been raised.

<myControl1:CustomDropDown ID="CustomDropDown1" runat="server" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>     

If you run Results.aspx, you will see that all the functionality is already working in the master page, but as yet we haven't done anything to display the results in the content page.

To achieve this, add a reference into the Source view of Results.aspx, so that we can directly refer to the master page programmatically. Insert the reference tag just below the page directive.

<%@ MasterType virtualpath="~/Default.master %>

So that we can access the details of the event raised by the CustomDropDown control on our master page we need to create a publically accessible method that returns the Event.

Public Event SearchCompleted As StaffSearchEvent

We can then re-point the event handler in Default.master to the newly delegated ListChanged Event.

Imports Acme.CustomDropDown
Partial Class _Default Inherits System.Web.UI.MasterPage
     Private Sub CustomDropDown1_ListChanged(ByVal sender As Object, ByVal e As CustomDropDownEventArgs) Handles CustomDropDown1.ListChanged
         Me.Label1.Text = e.FullName

         RaiseEvent ListChanged(Me, New CustomDropDownEventArgs(e.PayRollID, e.FullName))
     End Sub
     Public Event ListChanged As CustomDropDownEvent

End Class

Now to refer to the events arguments in the content page, we add add an event handler into the Page_Init sub of Results.aspx and create a call to a private function that displays the selected name in the label on out content page.

Imports Acme.CustomDropDown
Partial Class Results
     Inherits System.Web.UI.Page
     Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
          AddHandler Master.ListChanged, AddressOf ListChanged
     End Sub
    Private Sub ListChanged(ByVal obj As Object, ByVal e As CustomDropDownEventArgs)
        Label1.Text = e.FullName
    End Sub
End Class

Now when we run Results.aspx and select a value from the drop down list, three actions are being taken. First within the Web Custom Control the drop down list SelectedItemChanged event is raised, displaying the employee ID in a label. Next, the same event is captured by Default.master and the selected name is displayed in a label to the right of the Web Custom Control. Lastly, the content page also captures the same event and displays the selected name in a label below the Web Custom Control.

Posted: Oct 28 2009, 00:19 by Admin | Comments (0) RSS comment feed |
  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Filed under: ASP.NET | VB.NET

Filter records in a DataSet/DataTable

There are times when working with disconnected data that your DataSet can be quite large and you need some way of filtering the records you are working with, much as you would use a SELECT statement to filter the rows you want to return from a database. Thankfully the .NET framework contains a Filter mthods that can filter the records in a DataTable to achieve just such an outcome.

To illustrate this we will create a DataSet containing one DataTable and populate the DataTable with some dummy data. We won't be connecting to a database in this example, but as you know form previous posts, it is very easy to create your DataSets from SQL Server, Access or XML-based data sources.

    //create Dataset and DataTable

    DataSet ds = new DataSet();

    DataTable dt = new DataTable();

    dt.Columns.Add("FirstName", string.Empty.GetType());

    dt.Columns.Add("LastName", string.Empty.GetType());

 

    //create some dummy data

    DataRow dr = dt.NewRow();

    dr["FirstName"] = "Kim";

    dr["LastName"] = "Abercrombie";

    dt.Rows.Add(dr);

 

    dr = dt.NewRow();

    dr["FirstName"] = "Gustavo";

    dr["LastName"] = "Achlong";

    dt.Rows.Add(dr);

 

    dr = dt.NewRow();

    dr["FirstName"] = "Catherine";

    dr["LastName"] = "Abel";

    dt.Rows.Add(dr);

 

    dr = dt.NewRow();

    dr["FirstName"] = "Humberto";

    dr["LastName"] = "Acevedo";

    dt.Rows.Add(dr);

 

    dr = dt.NewRow();

    dr["FirstName"] = "Robert";

    dr["LastName"] = "O'Hara";

    dt.Rows.Add(dr);

 

    //add the DataTable to our DataSet

    ds.Tables.Add(dt);

Once we have created our dummy DataSet, we can now perform some filtering. The .Filter method of the DataTable object returns an array of DataRow objects.

    //perform some filtering

    string name = "O'Hara";

    DataRow[] filtered = ds.Tables[0].Select("LastName ='" + name.Replace("'", "''") + "'");

    if (filtered.Length > 0)

    {

        Console.WriteLine("Found " + filtered.Length + " records.");

    }

The filtering all happens with just one line of code, which is pretty straight forward. The only gotcha that you may encounter especially when dealing with names is the apostrophe. If you don't replace the single apostrophe with a double, the code will throw an error Syntax error: Missing operand after 'Hara' operator when trying to carry out the Filter method.

You can filter using a range of valid expressions, including using the LIKE operator. The following code returns two records.

    string name = "Ac";

    DataRow[] filtered = ds.Tables[0].Select("LastName Like'" + name.Replace("'", "''") + "%'");

Posted: Jun 10 2006, 08:12 by CameronM | Comments (0) RSS comment feed |
  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Filed under: ASP.NET | C#

Read Excel 2000-2003 into Dataset

There are times when you need to deal with data stored in an Excel spreadsheet directly, without first importing it into your database of choice (Access or SQL Server for instance).

One easy way is to use the OleDB Data Provider in NET to retrieve the data from Excel and load it into a DataSet. If you have been using Access 2000-2003 as a data source in your .NET projects then you may have already come across the OleDB Namespace. According to Microsoft, the OleDb Namespace provides a number of classes used to access OLE data sources.

The basic steps required to connect to Excel using OleDB is as follows:

  1. Create a connection
  2. Create a command (such as a SELECT) to retrieve the data you want
  3. Create a data adapter using this command
  4. Fill a dataset with the results

    public DataSet OpenExcel(string FileName)

    {

        //read excel file using OleDB and return results in a DataSet

 

        DataSet ds = new DataSet();

 

        //FileName contains the full filepath

        string connstr = "Provider=Microsoft.Jet.Oledb.4.0;Data Source=" + FileName + ";Extended Properties=Excel 8.0";

        OleDbConnection conn = new OleDbConnection(connstr);

 

        //select everything from the Worksheet named Merge Data

        string strSQL = "SELECT * FROM [Merge Data$]";

 

        OleDbCommand cmd = new OleDbCommand(strSQL, conn);

 

        //use the command to fill out DataSet

        OleDbDataAdapter da = new OleDbDataAdapter(cmd);

        da.Fill(ds);

 

        return ds;

    }

In the code snippet above we are feeding in the full filename, including path into our function. The SELECT statement grabs everything from a Worksheet named Merge Data and returns the results in a DataSet.

Posted: Feb 28 2006, 07:22 by CameronM | Comments (0) RSS comment feed |
  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Filed under: ASP.NET | C#

Read Excel 2000-2003 into Dataset

There are times when you need to deal with data stored in an Excel spreadsheet directly, without first importing it into your database of choice (Access or SQL Server for instance).

One easy way is to use the OleDB Data Provider in NET to retrieve the data from Excel and load it into a DataSet. If you have been using Access 2000-2003 as a data source in you r.NET projects then youmay have already come across the OleDB Namespace. According to Microsoft, the OleDb Namespace provides a number of classes used to access OLE data sources.

The basic steps required to connect to Excel using OleDB is as followd:

  1. Create a connection
  2. Create a command specifying what data you want
  3. Use this command to fill a DataSet (via a DataAdapter)

    public DataSet OpenExcel(string FileName)

    {

        //read excel file using OleDB and return results in a DataSet

 

        DataSet ds = new DataSet();

 

        //FileName contains the full filepath

        string connstr = "Provider=Microsoft.Jet.Oledb.4.0;Data Source=" + FileName + ";Extended Properties=Excel 8.0";

        OleDbConnection conn = new OleDbConnection(connstr);

 

        //select everything from the Worksheet named Merge Data

        string strSQL = "SELECT * FROM [Merge Data$]";

 

        OleDbCommand cmd = new OleDbCommand(strSQL, conn);

 

        //use the command to fill out DataSet

        OleDbDataAdapter da = new OleDbDataAdapter(cmd);

        da.Fill(ds);

 

        return ds;

    }

Posted: Feb 28 2006, 07:22 by CameronM | Comments (0) RSS comment feed |
  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Filed under: ASP.NET | C#