Saturday 24 March 2012

use of event in asp.net and user control

Add a user control in Ur project.
add a button in ur user control.
design the user control page:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
<center>
<asp:Button ID="btn" runat="server" Text="Save" Width="80" onclick="btn_Click" />
</center>
C# code of the designer page.

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

public partial class WebUserControl : System.Web.UI.UserControl
{
    public event EventHandler x;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btn_Click(object sender, EventArgs e)
    {
        if(x!=null)
            x(this,EventArgs.Empty);
    }
}
add the a asp page.
design the page.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register TagPrefix="a" TagName="b" Src="~/WebUserControl.ascx" %>
<!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">
    <asp:ScriptManager ID="sm" runat="server"></asp:ScriptManager>
    <div>
    <asp:UpdatePanel ID="up" runat="server">
    <ContentTemplate>
    <table align="center">
    <tr><td>Enter Ur Name</td><td><asp:TextBox ID="tb" runat="server"></asp:TextBox></td></tr>
    <tr><td>&nbsp;</td><td><a:b ID="uc" runat="server" /></td></tr>
    <tr><td>&nbsp;</td><td><asp:Label ID="lb" runat="server"></asp:Label></td></tr>
    </table>
    </ContentTemplate>
    </asp:UpdatePanel>
    </div>
    </form>
</body>
</html>
C# code of the page :

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register TagPrefix="a" TagName="b" Src="~/WebUserControl.ascx" %>
<!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">
    <asp:ScriptManager ID="sm" runat="server"></asp:ScriptManager>
    <div>
    <asp:UpdatePanel ID="up" runat="server">
    <ContentTemplate>
    <table align="center">
    <tr><td>Enter Ur Name</td><td><asp:TextBox ID="tb" runat="server"></asp:TextBox></td></tr>
    <tr><td>&nbsp;</td><td><a:b ID="uc" runat="server" /></td></tr>
    <tr><td>&nbsp;</td><td><asp:Label ID="lb" runat="server"></asp:Label></td></tr>
    </table>
    </ContentTemplate>
    </asp:UpdatePanel>
    </div>
    </form>
</body>
</html>



No comments:

Post a Comment