T
tshad
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="WebUserControl1.ascx.cs"
Inherits="UserControlSQL.WebUserControl1" %>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="Data Source=PTERADON\AW3000_INSTANCE;Initial
Catalog=CSpearAndAssociates;Integrated Security=True"
OnSelecting="SqlDataSource1_Selecting"
SelectCommand="SELECT FirstName,Lastname FROM Persons Join Address on
(Persons.AddressID = Addresses.AddressID) WHERE State = @State" "
SelectCommandType="Text">
<SelectParameters>
<asparameter Name="State" Size="2" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
***********************************
WebUserControl1.ascx.cs
***************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace UserControlSQL
{
public partial class WebUserControl1 : System.Web.UI.UserControl
{
public event SelectingEventHandler Selecting;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void SqlDataSource1_Selecting(object sender,
SqlDataSourceSelectingEventArgs e)
{
SelectingEventArgs args = new SelectingEventArgs();
// 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.Parameters["@State"].Value = args.State;
}
}
}
***************************************
Default.aspx
**********************************************
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
Inherits="UserControlSQL._Default" %>
<%@ Register Src="WebUserControl1.ascx" TagName="UserControl" TagPrefix="uc"
%>
<!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>
<uc:UserControl ID="UserControl1" runat="server"
OnSelecting="UserControl1_Selecting" />
</div>
</form>
</body>
</html>
**********************************************
Default.aspx.cs
************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace UserControlSQL
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
else
{
}
}
protected void Page_Init(object sender, EventArgs e)
{
UserControl1.Selecting += new
SelectingEventHandler(UserControl1_Selecting);
if (!IsPostBack)
{
}
else
{
}
}
protected void UserControl1_Selecting(object sender,
SelectingEventArgs e)
{
// Do something here to determine what state to specify.
}
}
}
*************************************************
SelectingEventArgs.cs
**********************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace UserControlSQL
{
public delegate void SelectingEventHandler(object sender,
SelectingEventArgs e);
public class SelectingEventArgs : 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.Selecting += new
SelectingEventHandler(UserControl1_Selecting);
and
<uc:UserControl ID="UserControl1" runat="server"
OnSelecting="UserControl1_Selecting" />
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="WebUserControl1.ascx.cs"
Inherits="UserControlSQL.WebUserControl1" %>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="Data Source=PTERADON\AW3000_INSTANCE;Initial
Catalog=CSpearAndAssociates;Integrated Security=True"
OnSelecting="SqlDataSource1_Selecting"
SelectCommand="SELECT FirstName,Lastname FROM Persons Join Address on
(Persons.AddressID = Addresses.AddressID) WHERE State = @State" "
SelectCommandType="Text">
<SelectParameters>
<asparameter Name="State" Size="2" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
***********************************
WebUserControl1.ascx.cs
***************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace UserControlSQL
{
public partial class WebUserControl1 : System.Web.UI.UserControl
{
public event SelectingEventHandler Selecting;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void SqlDataSource1_Selecting(object sender,
SqlDataSourceSelectingEventArgs e)
{
SelectingEventArgs args = new SelectingEventArgs();
// 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.Parameters["@State"].Value = args.State;
}
}
}
***************************************
Default.aspx
**********************************************
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
Inherits="UserControlSQL._Default" %>
<%@ Register Src="WebUserControl1.ascx" TagName="UserControl" TagPrefix="uc"
%>
<!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>
<uc:UserControl ID="UserControl1" runat="server"
OnSelecting="UserControl1_Selecting" />
</div>
</form>
</body>
</html>
**********************************************
Default.aspx.cs
************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace UserControlSQL
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
else
{
}
}
protected void Page_Init(object sender, EventArgs e)
{
UserControl1.Selecting += new
SelectingEventHandler(UserControl1_Selecting);
if (!IsPostBack)
{
}
else
{
}
}
protected void UserControl1_Selecting(object sender,
SelectingEventArgs e)
{
// Do something here to determine what state to specify.
}
}
}
*************************************************
SelectingEventArgs.cs
**********************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace UserControlSQL
{
public delegate void SelectingEventHandler(object sender,
SelectingEventArgs e);
public class SelectingEventArgs : 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.Selecting += new
SelectingEventHandler(UserControl1_Selecting);
and
<uc:UserControl ID="UserControl1" runat="server"
OnSelecting="UserControl1_Selecting" />
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