google analytics

Wednesday, February 25, 2009

Custom Event in .NET

Hello All,

Today I will try to deliberate my experience on custom event creation in .NET , Actually I was trying to find something like hands on for event creation in .NET application, suddenly I got a quick help from one of my brother Sadique, who helped me out to resolve the thirst.

We are going to create a custom control where we will define our event class and event handler.To do that first take a  user control named InputControl.ascx  . The ascx page is as bellow.

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="InputControl.ascx.cs" Inherits="InputControl" %>
<asp:Label ID="lblLabel" runat="server" Text=""></asp:Label>
    <br />
<asp:TextBox ID="txtTextBox" runat="server"></asp:TextBox>
    <br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" 
    onclick="btnSubmit_Click" />

In that page I put a textbox control to take input , a button control to fire our custom event handler and a label to show our input result through our custom event.

The InputControl.ascx.cs code behind file is as bellow :
using System;

public partial class InputControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public string EventInputBox
    {
        get {
                return this.txtTextBox.Text;
            }
        set {
                this.txtTextBox.Text = value;
            }
    }

    public string EventLabel {
        get {
                 return this.lblLabel.Text;
            }
        set {
                 this.lblLabel.Text = value;
            }
    }

    public event EventHandler<CustomEventArgs> CustomEventSubmit;
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (this.CustomEventSubmit != null)
            this.CustomEventSubmit(this, new CustomEventArgs("your text is:" + EventInputBox));
    }

}

public class CustomEventArgs : EventArgs
{
    public string EventText { set; get; }

    public CustomEventArgs(string eventTextData)
    {
        EventText = eventTextData;

       
        /*
         Perform other event tasks.
         
         */
    }
}

Now I register the control in my Default.aspx page as bellow .

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register src="InputControl.ascx" tagname="InputControl" tagprefix="uc1" %>

<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <uc1:InputControl ID="icEventBox" runat="server" OnCustomEventSubmit="icEventBox_CustomEventSubmit"  />
    </div>
    </form>
</body>
</html>

In the file above we have to focus on OnCustomEventSubmit="icEventBox_CustomEventSubmit" which is pointing our custom event.

The code behind file Default.aspx.cs is as bellow.

using System;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void icEventBox_CustomEventSubmit(object sender, CustomEventArgs e)
    {
        icEventBox.EventLabel = e.EventText;
    }
}

In the above article I didn't go for much brief discussion , I think the code is sufficient to describe a custom event itself.

That's all for the day.
BYE

User ScrumPad for your Agile based projects.

Monday, December 29, 2008

Namespace in ASP.NET

Commonly Used Types and Namespaces in ASP.NET

System.Web
HttpApplication
HttpCookie
HttpRequest
HttpResponse
HttpRuntime
HttpServerUtility

System.Web.ApplicationServices *NEW
AuthenticationService NEW
ProfileService NEW
RoleService NEW

System.Web.Caching
Cache

System.Web.ClientServices *NEW
ClientFormsIdentity NEW
ClientRolePrincipal NEW
ConnectivityStatus NEW

System.Web.ClientServices.Providers *NEW
ClientFormsAuthenticationMembershipProvider NEW
ClientRoleProvider NEW

System.Web.Compilation
BuildProvider

System.Web.Configuration
WebConfigurationManager *NEW

System.Web.Hosting
ApplicationManager

System.Web.Management
WebBasedEvent

System.Web.Security
FormsAuthentication
FormsIdentity
Membership
Roles

System.Web.SessionState
HttpSessionState

System.Web.UI
Control
MasterPage
Page
ScriptManager NEW
System.Web.UI
UpdatePanel NEW
UpdateProgress NEW
UserControl

System.Web.UI.HtmlControls
HtmlButton
HtmlControl
HtmlForm
HtmlInputControl

System.Web.UI.WebControls
Content
DetailsView
FormView
GridView
LinqDataSource
ListView
LogIn
Menu
ObjectDataSources
TreeView
Wizard

System.Web.UI.WebControls.WebParts
WebPart

*New - New namespaces in .NET framework 3.5

[Source]

That's all for the day.
BYE

User ScrumPad for your Agile based projects.

Friday, December 5, 2008

MySQL multiple table update & Regular Expression

Hello all

Though its a very simple process but we usually don't do this stuff too much unless we are bound to do that.In this article I will show how to update a table data with another table depending on another table & another will be a simple regular expression in MySQL query.

In the example I have 'new_users' table to update with the data of 'listings' table depending on the condition or 'users' table. and the table structure is as bellow.

'new_users' :
userID,phone,user_state
'listings' :
listID,userID,phone
'users':
countID,userID,category

Now the scenario is I have to update the phone of new_users from the listings tables phone of same userID but only who are only 'Admin' which we may found in users table's  category field. So the query will be as following

-- Update from listings for 'Admin'
update listings, new_users, users
set
new_users.phone = listings.phone 
where
users.category = 'Admin' &&
listings.userID = users.userID &&
users.userID = new_users.userID
Now come to the second part, we can apply regular expression in our MySQL query as follow:
-- Update all user_state to null which contains invalid character
update new_users
set
new_users.user_state = null
where
new_users.user_state REGEXP '[1234567890~!@#$%^&*()_+|}{":?><,./;]'

The query will set null to user_state field if user_state field contains none but a-z & A-Z previously.

I have also some collection or regular expression for MySQL which i have collected from different site.

A very simple example illustrating this is to select all the records from MyTable for which MyField starts with "A"

SELECT * FROM  MyTable WHERE MyField REGEXP '^a';
Please take a look to the list below in order to find more information for the available options MySQL Regular Expressions

Matches zero or more instances of the string preceding it

Matches one or more instances of the string preceding it

Matches zero or one instances of the string preceding it

Matches any single character
[xyz] 
Matches any of x, y, or z (the characters within the brackets)
[A-Z] 
Matches any uppercase letter
[a-z] 
Matches any lowercase letter
[0-9] 
Matches any digit 

Anchors the match from the beginning 

Anchors the match to the end

Separates strings in the regular expression
{n,m} 
String must occur at least n times, but no more than n  
{n} 
String must occur exactly n times
{n,| 
String must occur at least n times

[Source]

 

That's all for the day.
BYE
User ScrumPad for your Agile based projects.