Tags

, ,


Handle user control events in ASPX Page

Given example handling GridView’s SlelctedItemIndexChanged event and Button’s Click Event of User Control in ASPX page.

TestUserControl .ASCX File

<%@ Control Language=”C#” AutoEventWireup=”true” CodeFile=”TestUserControl.ascx.cs” Inherits=”UserControls_TestUserControl” %>
<asp:GridView
    ID=”GridView1″
    runat=”server”
    BackColor=”White”
    BorderColor=”#3366CC”
    BorderStyle=”None”
    BorderWidth=”1px”
    CellPadding=”4″
    onselectedindexchanged=”GridView1_SelectedIndexChanged”
    >
    <RowStyle
        BackColor=”White”
        ForeColor=”#003399″
        />
    <Columns>
        <asp:CommandField
             ShowSelectButton=”True”
             />
    </Columns>
    <FooterStyle
        BackColor=”#99CCCC”
        ForeColor=”#003399″
        />
    <PagerStyle
        BackColor=”#99CCCC”
        ForeColor=”#003399″
        HorizontalAlign=”Left”
        />
    <SelectedRowStyle
        BackColor=”#009999″
        Font-Bold=”True”
        ForeColor=”#CCFF99″
        />
    <HeaderStyle
        BackColor=”#003399″
        Font-Bold=”True”
        ForeColor=”#CCCCFF”
        />
</asp:GridView>
    <asp:Button
        ID=”btnSearch”
        runat=”server”
        onclick=”btnSearch_Click”
        Text=”Search”
       />
</p>

TestUserControl .ASCX.CS File

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class UserControls_TestUserControl : System.Web.UI.UserControl
{
    public event EventHandler SearchButtonClick; //For Search Button

    public event EventHandler SelectCommand; //For GridView

    //For Search Button
    protected virtual void OnSearchButtonClick(object sender, EventArgs e)
    {

        if (SearchButtonClick != null)

            SearchButtonClick(sender, e);

    }

    //For GridView
    protected virtual void OnSelectCommand(object sender, EventArgs e )

    {

        if (SelectCommand != null)

            SelectCommand(sender, e);

    }
    //For Search Button
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        OnSearchButtonClick(sender, e);
    }

    //For GridView   
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
       
        OnSelectCommand(sender,e);
    }
}

 

TestPage.ASPX File

<%@ Page
 Language=”C#”


%>
<%@ Register
 src=”TestUserControl.ascx”
 tagname=”TestUserControl”
 tagprefix=”uc1″
%>
 

        <uc1:TestUserControl
         OnSearchButtonClick=”OnSearchButtonClick_Click”
         OnSelectCommand =”TestUserControl_Select”
  ID=”TestUserControl1″
  runat=”server”
 />

</asp:Content>

TestPage.ASPX.CS File

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net;

public partial class TestPage : System.Web.UI.Page
{

    protected void TestUserControl_Select(object sender, EventArgs e)
    {
 //Code for Grid View Select Command

    }
    protected void OnSearchButtonClick_Click(object sender, EventArgs e)
    {
        //Code for Search Button Click
    }
   
  
}

Advertisements