Using a custom event.

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>
<asp:parameter 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
 
G

Gregory A. Beamer \(Cowboy\) - MVP

Wire the custom event to a routine that handles it. The signature will look
like this:

control.Event += new EventHandler(methodName);

Try this:
http://www.ondotnet.com/pub/a/dotnet/2002/04/15/events.html


tshad said:
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>
<asp:parameter 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
 
B

bruce barker

i not sure what your issue is.

you created a user control that defines an event. that control hosts a
control (SqlDataSource) that raises an event. in the subscribed event
handler, the control raises the Selecting event.

default.aspx subscribed twice to the event, once in the aspx via the
OnSelecting property and one in the code behind adding a delegate to the
actual event property. the OnSelecting is how events are exposed in aspx
syntax. when compiled they generate the similar code as your codebehind
version.

as you have no code to actually fire the SqlDataSource's event, not much
happens. if you call the Select method of the SqlDataSource the event
should fire.

-- bruce (sqlwork.com)
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>
<asp:parameter 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
 
T

tshad

bruce barker said:
i not sure what your issue is.

you created a user control that defines an event. that control hosts a
control (SqlDataSource) that raises an event. in the subscribed event
handler, the control raises the Selecting event.

default.aspx subscribed twice to the event, once in the aspx via the
OnSelecting property and one in the code behind adding a delegate to the
actual event property. the OnSelecting is how events are exposed in aspx
syntax. when compiled they generate the similar code as your codebehind
version.

Would that mean that I would get my same function called twice?
as you have no code to actually fire the SqlDataSource's event, not much
happens. if you call the Select method of the SqlDataSource the event
should fire.
So in my control I would call the SqlDataSource1_Selecting function
somewhere?

I am a little confused as to how this would work.

Apparently, the Web page is supposed to pass the parameter (State) to the
web control before it does the actual Select statement.

This is a little confusing.

I assume that the SqlDataSource1_Selecting would get called from the web
control and that would raise the Selecting event.

Then the control would run the method specified by the web page
(UserControl1_Selecting) which would then set the SelectingEventArgs to a
selected State (e.State = "CA").

Then back at the control, it would get the State from the SelectingEventArgs
that was passed from the Web Page (e.Command.Parameters["@State"].Value =
args.State;).

Then the SqlDataSource1_Selecting method would execute the Select statement.

But what would start this running?

I assume that nothing can happen until the Web Page has the State to give
the control. So what would tell the control to Raise the event?

Thanks,

Tom
-- bruce (sqlwork.com)
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=CSA;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>
<asp:parameter 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
 
T

tshad

Gregory A. Beamer (Cowboy) - MVP said:
Wire the custom event to a routine that handles it. The signature will
look like this:

control.Event += new EventHandler(methodName);
Wasn't that what I did (twice) in the Web Page (default):

UserControl1.Selecting += new
SelectingEventHandler(UserControl1_Selecting);

Good article.

Helped a lot.

Thanks,

Tom
tshad said:
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=CSA;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>
<asp:parameter 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
 
B

bruce barker

you are confused.

a sqldatasource was meant as declarative sql data access object.
normally you'd attach to a datasource aware control that would call its
select, insert, update or delete methods when it needed to. say a
ListBox would call it when its DataBind method was called in the code
behind or at PreRender by the control itself. you can directly call its
select method, SqlDataSource1(params), but don't know why you woudl
unless you were writing a data aware control. (of course this is all
obsolete with linq).


when the select method is called, before the query is passed to the
database, the Selecting event is fired. this allows the modification of
the parameter list.

its still not at all clear what you are trying to do with events, or
what you are using a SqlDataSource for.

you should read the docs on ado.net and learn a little more about data
access before you tackle events.

-- bruce (sqlwork.com)

bruce barker said:
i not sure what your issue is.

you created a user control that defines an event. that control hosts a
control (SqlDataSource) that raises an event. in the subscribed event
handler, the control raises the Selecting event.

default.aspx subscribed twice to the event, once in the aspx via the
OnSelecting property and one in the code behind adding a delegate to the
actual event property. the OnSelecting is how events are exposed in aspx
syntax. when compiled they generate the similar code as your codebehind
version.

Would that mean that I would get my same function called twice?
as you have no code to actually fire the SqlDataSource's event, not much
happens. if you call the Select method of the SqlDataSource the event
should fire.
So in my control I would call the SqlDataSource1_Selecting function
somewhere?

I am a little confused as to how this would work.

Apparently, the Web page is supposed to pass the parameter (State) to the
web control before it does the actual Select statement.

This is a little confusing.

I assume that the SqlDataSource1_Selecting would get called from the web
control and that would raise the Selecting event.

Then the control would run the method specified by the web page
(UserControl1_Selecting) which would then set the SelectingEventArgs to a
selected State (e.State = "CA").

Then back at the control, it would get the State from the SelectingEventArgs
that was passed from the Web Page (e.Command.Parameters["@State"].Value =
args.State;).

Then the SqlDataSource1_Selecting method would execute the Select statement.

But what would start this running?

I assume that nothing can happen until the Web Page has the State to give
the control. So what would tell the control to Raise the event?

Thanks,

Tom
-- bruce (sqlwork.com)
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=CSA;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>
<asp:parameter 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
 
T

tshad

bruce barker said:
you are confused.

a sqldatasource was meant as declarative sql data access object. normally
you'd attach to a datasource aware control that would call its select,
insert, update or delete methods when it needed to. say a ListBox would
call it when its DataBind method was called in the code behind or at
PreRender by the control itself. you can directly call its select method,
SqlDataSource1(params), but don't know why you woudl unless you were
writing a data aware control. (of course this is all obsolete with linq).


when the select method is called, before the query is passed to the
database, the Selecting event is fired. this allows the modification of
the parameter list.

its still not at all clear what you are trying to do with events, or what
you are using a SqlDataSource for.

you should read the docs on ado.net and learn a little more about data
access before you tackle events.

This is just an example program (I didn't write it) on event publishing.

http://www.dotnetadvisor.com/Blog/CustomEvent.aspx

I was trying to figure out how to use this as an exercise. But I am trying
to see how I would actually use this. The article doesn't go into it. It
doesn't show how the event would be called.

I was using the SqlDataSource because that was what was in the article.

Tom
-- bruce (sqlwork.com)

bruce barker said:
i not sure what your issue is.

you created a user control that defines an event. that control hosts a
control (SqlDataSource) that raises an event. in the subscribed event
handler, the control raises the Selecting event.

default.aspx subscribed twice to the event, once in the aspx via the
OnSelecting property and one in the code behind adding a delegate to the
actual event property. the OnSelecting is how events are exposed in aspx
syntax. when compiled they generate the similar code as your codebehind
version.

Would that mean that I would get my same function called twice?
as you have no code to actually fire the SqlDataSource's event, not much
happens. if you call the Select method of the SqlDataSource the event
should fire.
So in my control I would call the SqlDataSource1_Selecting function
somewhere?

I am a little confused as to how this would work.

Apparently, the Web page is supposed to pass the parameter (State) to the
web control before it does the actual Select statement.

This is a little confusing.

I assume that the SqlDataSource1_Selecting would get called from the web
control and that would raise the Selecting event.

Then the control would run the method specified by the web page
(UserControl1_Selecting) which would then set the SelectingEventArgs to a
selected State (e.State = "CA").

Then back at the control, it would get the State from the
SelectingEventArgs that was passed from the Web Page
(e.Command.Parameters["@State"].Value = args.State;).

Then the SqlDataSource1_Selecting method would execute the Select
statement.

But what would start this running?

I assume that nothing can happen until the Web Page has the State to give
the control. So what would tell the control to Raise the event?

Thanks,

Tom
-- bruce (sqlwork.com)

tshad wrote:
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=CSA;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>
<asp:parameter 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top