.Net Hive

A blog about Microsoft .Net Platform & other industry happenings

Custom Events in ASP.NET User Controls

Hello Everyone,

Yesterday I was working on a user control and I came across a situation where I wanted to notify the parent page when ever the button in the user control get clicks.

This situation calls for a requirement of having a custom event associated with the user control I can then use in the host page, which encapsulates the user control.

Here is the core source code to required to achieve this functionality:

CustomUserControl.ascx.cs

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class CustomUserControl : System.Web.UI.UserControl
{
public event EventHandler SubmitClick;

protected void btnSubmit_Click(object sender, EventArgs e)
{
this.OnSubmitClick(e);
}

protected void OnSubmitClick(EventArgs e)
{
if (SubmitClick != null)
{
SubmitClick(this, e);
}
}
}

Because I needed the event to be executed when ever the submit button get executed, there for I invoked the method at the Submit button click. You can change it according to your requirement.

HostWebPage.aspx.cs

using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data; 

public partial class HostWebPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
userCustomControl.SubmitClick += new EventHandler(userCustomControl_SubmitClick);
}

void userCustomControl_SubmitClick(object sender, EventArgs e)
{
//DO ANY WORK
}
}

I tried to make this example as simple as possible. Let me know if this helped. Any suggestion or queries are welcome.

Happy Reading

Waqas Ahmed

Filed under: ASP.NET, , , ,

jQuery Controls for ASP.NET

I know all of you like to use jQuery in your asp.net applications. And why not, it’s the best thing ever happened in past few time. But managing things up do get out of control. Today I am introducing set of server side controls which help you to easily implement jQuery in ASP.NET Application. You can use these controls as you use your other server side controls.

I came across this toolkit written by Sike Mulliven. And he really did this job perfectly.

In the toolkit, you will find four different controls along with designer and another control inspired from Rick Strahl’s article on the jQueryCalender.

To use the controls all you have to do is just add CSS file references to your page and drag & drop the controls from the ToolBox. Some Screen captures attached for your reference.

jQuery Controls

jQuery Controls

jQuery TabView

jQuery TabView (Design)

 

jQuery TabView (Runtime)

jQuery Dialog

jQuery Dialog (Design)

jQuery Dialog (Runtime)

jQuery Slider

jQuery Slider (Design)

jQuery Slider (Runtime)

jQuery Accordion

jQuery Accordion (Design)

jQuery Accordion (Runtime)

You can download these control from the following location

http://cid-cce8a29919c8f422.skydrive.live.com/self.aspx/Public/jQuery/Mullivan.Web.zip

The solution contains the source code for the controls and the test project for it.

I hope these jQuery controls will really add a spice in your UI development for ASP.NET Application. Thanks to Sike Mullivan.

Let me know about your comments and how helpful it can be for you.

Waqas Ahmed


Filed under: ASP.NET, , , ,

If the internet is a nation? who would be its president?

Just came across a very interesting poll at TechRepublic. So I though why not share with you and have your opinion added to it. To cast you vote.

The poll is at the following URL

http://blogs.techrepublic.com.com/hiner/?p=1894&tag=nl.e101

This poll is create by Jason Hiner of TechRepublic and he is a editor in chief.

It will open on Saturday, July 4, 2009 and then the poll will close promptly on July 11, 2009 at 11:59 PM Eastern (8:59 PM Pacific).

Do give your feedback about your potential candidate for this run up and why?

Waqas Ahmed

Filed under: Others

Socket / Network Programming in C#

Today I have decided to give a quick tutorial on Sockets/Network programming in C#. For this I have taken an example of a simple Client/Server application scenario. Where a Socket listen on a specific port and waits for the client to be connected. Once the client is connected, The client application will send a test message to the server application. I will try to achieve this functionality using minimal code and to give a concept of how socket programming works.

Hope this will be helpful. Following is the server application code.

using System;
using System.Text;
using System.Net;
using System.Net.Sockets;

class SocketServer
{
  public static void Main()
  {
    StreamWriter streamWriter;
    StreamReader streamReader;
    NetworkStream networkStream;
    TcpListener tcpListener = new TcpListener(3333);
    tcpListener.Start();
    Console.WriteLine("Server Started. Waiting for Client Connection at port 3333");
    Socket serverSocket = tcpListener.AcceptSocket();
    try
    {
        if (serverSocket.Connected)
        {
            while (true)
            {
                Console.WriteLine("Client Connected at port 3333");
                networkStream = new NetworkStream(serverSocket);
                streamWriter = new StreamWriter(networkStream);
                streamReader = new StreamReader(networkStream);
                Console.WriteLine(streamReader.ReadLine());

            }
        }
        if (serverSocket.Connected)
            serverSocket.Close();
        Console.Read();
    }
    catch (SocketException ex)
    {
        Console.WriteLine(ex);
    }
  }
}

When the above code is executed, it will try to listen on port 3333 and will wait for an incoming connection from a client application. Upon start, server application will display a message “Server Started. Waiting for Client Connection at port 3333”. As soon as the server application recives the connection from client application, it show the message “Client Connected at port 3333”.

Now let’s see how our client application code looks like. Our client application will try to connect at port 3333. Following is the Client Application Code:

using System;
using System.Text;
using System.Net;
using System.Net.Sockets;

class SocketClient
{
  static void Main(string[]args)
  {
    TcpClient tcpClient;
    NetworkStream networkStream;
    StreamReader streamReader;
    StreamWriter streamWriter;
    try
    {
        tcpClient = new TcpClient("localhost", 3333);
        networkStream = tcpClient.GetStream();
        streamReader = new StreamReader(networkStream);
        streamWriter = new StreamWriter(networkStream);
        streamWriter.WriteLine("Client connected at port 3333");
        streamWriter.Flush();
    }
    catch (SocketException ex)
    {
        Console.WriteLine(ex);
    }
    Console.Read();
  }
}

When the above client application code will execute, it will look for an opened connection from a server application at port 3333. As soon as it finds the connection, it will establish the connection with it and will send a message “Client connected at port 3333”. This test message will be displayed on server application console.

A Port is an integer that identifies a process running over a network for the purpose of providing a service. A socket is the end point of a two way communication between two processes running over a network. Microsoft .NET provides excellent support for writing programs that leverage the power of Sockets to implement programs that can run over a network to communicate and share data and information. 

In this entry we tried look at how we can work with Sockets using Microsoft .NET and how we can implement a simple client-server program that can communicate over a network. 

Your comments and suggestions are welcome.

Happy reading!

Waqas Ahmed

Filed under: .Net, Code Snippets, , ,

Data Access with ADO.NET 3.5 – LINQ to SQL

Alright, going back to our series where we discussed LINQ and all related extensions. Lat time we discussed LINQ itself. Today, we are going to talk about LINQ to SQL extension.

Well LINQ to SQL is more than just a LINQ implementation for relational databases; it includes a very easy to use graphical object/relational mapping tool. This tool generates an entity class for each table you select from the SQL Server database to which the project connects. The tool also generates an association that relates to each foreign key constraint. Associations eliminate the need to specify joins in LINQ queries that include projections of fields from related tables. The ability to navigate associations is an important feature of LINQ implementations and the Entity Framework.

The advantage of moving from the relational model of tables, rows, and keys to the entity model of entity classes, entity instances, and associations is that entities usually do represent business objects, such as customers, partners, vendors, employees, products, and services. An entity model enables developers to design real world objects with custom properties, associations, and behaviors suited to the organization’s business processes.

LINQ to SQL is intended for 1:1 mapping of tables:entities, although it does have limited support for entity inheritance of the Table per Hierarchy (TPH) type based on discriminator columns. Read only entities can be based on views or stored procedures. The Entity Framework’s Entity Data Model supports joining multiple tables to create a single updatable entity, as well as several types of entity inheritance.

This entry covers a brief overview about LINQ to SQL extension or you can say a LINQ to SQL in a nutshell. I will try to give some examples about the auto generated classes for entities which entity framework create, in some later post.

Till than, Happy Reading.

Waqas Ahmed

Filed under: .Net, , ,

Pages

Categories

Archives

.Net Hive @ Twitter

Blog Directory by Blog Flux
Computers/Tech blogs