I was looking at a page that showed how to set up a custom event and it
seems to work ok. But I am not sure how I would use it. How would I
subscribe to it. There is actual action (such as pressing a button or
changing text in a textbox).
It gets set up and on the user control on my web page I can see the event
from intellisense. So it seems to be set up, but I am trying to get an easy
example of how I would now use this event.
The conrol is just an SqlDataSource that will look up persons from the
Persons table based on State.
But how do I get the event to work?
I have 3 files: the control, the default page that uses the control and an
"EventArgs" class to handle the parameters - in this case just State.
WebUserControl1 .ascx
*************** *************** *****
<%@ Control Language="C#" AutoEventWireup ="true"
CodeBehind="Web UserControl1.as cx.cs"
Inherits="UserC ontrolSQL.WebUs erControl1" %>
<asp:SqlDataSou rce ID="SqlDataSour ce1" runat="server"
ConnectionStrin g="Data Source=PTERADON \AW3000_INSTANC E;Initial
Catalog=CSpearA ndAssociates;In tegrated Security=True"
OnSelecting="Sq lDataSource1_Se lecting"
SelectCommand=" SELECT FirstName,Lastn ame FROM Persons Join Address on
(Persons.Addres sID = Addresses.Addre ssID) WHERE State = @State" "
SelectCommandTy pe="Text">
<SelectParamete rs>
<asp:Paramete r Name="State" Size="2" Type="String" />
</SelectParameter s>
</asp:SqlDataSour ce>
*************** *************** *****
WebUserControl1 .ascx.cs
*************** *************** *********
using System;
using System.Collecti ons.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.W ebControls;
namespace UserControlSQL
{
public partial class WebUserControl1 : System.Web.UI.U serControl
{
public event SelectingEventH andler Selecting;
protected void Page_Load(objec t sender, EventArgs e)
{
}
protected void SqlDataSource1_ Selecting(objec t sender,
SqlDataSourceSe lectingEventArg s e)
{
SelectingEventA rgs args = new SelectingEventA rgs();
// Retrieve the parameter values from the consumer
if (Selecting != null)
{
Selecting(this, args);
}
// Use the values set by the consumer to set the values of the
parameters
e.Command.Param eters["@State"].Value = args.State;
}
}
}
*************** *************** *********
Default.aspx
*************** *************** *************** *
<%@ Page Language="C#" AutoEventWireup ="true" CodeBehind="Def ault.aspx.cs"
Inherits="UserC ontrolSQL._Defa ult" %>
<%@ Register Src="WebUserCon trol1.ascx" TagName="UserCo ntrol" TagPrefix="uc"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<uc:UserContr ol ID="UserControl 1" runat="server"
OnSelecting="Us erControl1_Sele cting" />
</div>
</form>
</body>
</html>
*************** *************** *************** *
Default.aspx.cs
*************** *************** *************** ***
using System;
using System.Collecti ons.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.W ebControls;
namespace UserControlSQL
{
public partial class _Default : System.Web.UI.P age
{
protected void Page_Load(objec t sender, EventArgs e)
{
if (!IsPostBack)
{
}
else
{
}
}
protected void Page_Init(objec t sender, EventArgs e)
{
UserControl1.Se lecting += new
SelectingEventH andler(UserCont rol1_Selecting) ;
if (!IsPostBack)
{
}
else
{
}
}
protected void UserControl1_Se lecting(object sender,
SelectingEventA rgs e)
{
// Do something here to determine what state to specify.
}
}
}
*************** *************** *************** ****
SelectingEventA rgs.cs
*************** *************** ****
using System;
using System.Collecti ons.Generic;
using System.Linq;
using System.Web;
namespace UserControlSQL
{
public delegate void SelectingEventH andler(object sender,
SelectingEventA rgs e);
public class SelectingEventA rgs : EventArgs
{
string _State;
public string State
{
get { return _State; }
set { _State = value; }
}
}
}
*************** *************** ****
In my example, I am subscribing to the event in 2 ways:
UserControl1.Se lecting += new
SelectingEventH andler(UserCont rol1_Selecting) ;
and
<uc:UserContr ol ID="UserControl 1" runat="server"
OnSelecting="Us erControl1_Sele cting" />
And as I mentioned, in the UserControl, I am using OnSelecting which shows
in intellisense.
But what would I do to actually use the event in my default page?
Thanks,
Tom
seems to work ok. But I am not sure how I would use it. How would I
subscribe to it. There is actual action (such as pressing a button or
changing text in a textbox).
It gets set up and on the user control on my web page I can see the event
from intellisense. So it seems to be set up, but I am trying to get an easy
example of how I would now use this event.
The conrol is just an SqlDataSource that will look up persons from the
Persons table based on State.
But how do I get the event to work?
I have 3 files: the control, the default page that uses the control and an
"EventArgs" class to handle the parameters - in this case just State.
WebUserControl1 .ascx
*************** *************** *****
<%@ Control Language="C#" AutoEventWireup ="true"
CodeBehind="Web UserControl1.as cx.cs"
Inherits="UserC ontrolSQL.WebUs erControl1" %>
<asp:SqlDataSou rce ID="SqlDataSour ce1" runat="server"
ConnectionStrin g="Data Source=PTERADON \AW3000_INSTANC E;Initial
Catalog=CSpearA ndAssociates;In tegrated Security=True"
OnSelecting="Sq lDataSource1_Se lecting"
SelectCommand=" SELECT FirstName,Lastn ame FROM Persons Join Address on
(Persons.Addres sID = Addresses.Addre ssID) WHERE State = @State" "
SelectCommandTy pe="Text">
<SelectParamete rs>
<asp:Paramete r Name="State" Size="2" Type="String" />
</SelectParameter s>
</asp:SqlDataSour ce>
*************** *************** *****
WebUserControl1 .ascx.cs
*************** *************** *********
using System;
using System.Collecti ons.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.W ebControls;
namespace UserControlSQL
{
public partial class WebUserControl1 : System.Web.UI.U serControl
{
public event SelectingEventH andler Selecting;
protected void Page_Load(objec t sender, EventArgs e)
{
}
protected void SqlDataSource1_ Selecting(objec t sender,
SqlDataSourceSe lectingEventArg s e)
{
SelectingEventA rgs args = new SelectingEventA rgs();
// Retrieve the parameter values from the consumer
if (Selecting != null)
{
Selecting(this, args);
}
// Use the values set by the consumer to set the values of the
parameters
e.Command.Param eters["@State"].Value = args.State;
}
}
}
*************** *************** *********
Default.aspx
*************** *************** *************** *
<%@ Page Language="C#" AutoEventWireup ="true" CodeBehind="Def ault.aspx.cs"
Inherits="UserC ontrolSQL._Defa ult" %>
<%@ Register Src="WebUserCon trol1.ascx" TagName="UserCo ntrol" TagPrefix="uc"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<uc:UserContr ol ID="UserControl 1" runat="server"
OnSelecting="Us erControl1_Sele cting" />
</div>
</form>
</body>
</html>
*************** *************** *************** *
Default.aspx.cs
*************** *************** *************** ***
using System;
using System.Collecti ons.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.W ebControls;
namespace UserControlSQL
{
public partial class _Default : System.Web.UI.P age
{
protected void Page_Load(objec t sender, EventArgs e)
{
if (!IsPostBack)
{
}
else
{
}
}
protected void Page_Init(objec t sender, EventArgs e)
{
UserControl1.Se lecting += new
SelectingEventH andler(UserCont rol1_Selecting) ;
if (!IsPostBack)
{
}
else
{
}
}
protected void UserControl1_Se lecting(object sender,
SelectingEventA rgs e)
{
// Do something here to determine what state to specify.
}
}
}
*************** *************** *************** ****
SelectingEventA rgs.cs
*************** *************** ****
using System;
using System.Collecti ons.Generic;
using System.Linq;
using System.Web;
namespace UserControlSQL
{
public delegate void SelectingEventH andler(object sender,
SelectingEventA rgs e);
public class SelectingEventA rgs : EventArgs
{
string _State;
public string State
{
get { return _State; }
set { _State = value; }
}
}
}
*************** *************** ****
In my example, I am subscribing to the event in 2 ways:
UserControl1.Se lecting += new
SelectingEventH andler(UserCont rol1_Selecting) ;
and
<uc:UserContr ol ID="UserControl 1" runat="server"
OnSelecting="Us erControl1_Sele cting" />
And as I mentioned, in the UserControl, I am using OnSelecting which shows
in intellisense.
But what would I do to actually use the event in my default page?
Thanks,
Tom
Comment