473,400 Members | 2,145 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,400 software developers and data experts.

Using a custom event.

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


Oct 20 '08 #1
6 1861
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...15/events.html

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://feeds.feedburner.com/GregoryBeamer#

or just read it:
http://feeds.feedburner.com/GregoryBeamer

********************************************
| Think outside the box! |
********************************************
"tshad" <tf*@dslextreme.comwrote in message
news:%2***************@TK2MSFTNGP02.phx.gbl...
>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


Oct 20 '08 #2
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)

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=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

Oct 21 '08 #3

"bruce barker" <no****@nospam.comwrote in message
news:Op**************@TK2MSFTNGP06.phx.gbl...
>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
Oct 21 '08 #4

"Gregory A. Beamer (Cowboy) - MVP" <No************@comcast.netNoSpamMwrote
in message news:eW**************@TK2MSFTNGP03.phx.gbl...
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);
Try this:
http://www.ondotnet.com/pub/a/dotnet...15/events.html
Good article.

Helped a lot.

Thanks,

Tom
>
--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://feeds.feedburner.com/GregoryBeamer#

or just read it:
http://feeds.feedburner.com/GregoryBeamer

********************************************
| Think outside the box! |
********************************************
"tshad" <tf*@dslextreme.comwrote in message
news:%2***************@TK2MSFTNGP02.phx.gbl...
>>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



Oct 21 '08 #5
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)
tshad wrote:
"bruce barker" <no****@nospam.comwrote in message
news:Op**************@TK2MSFTNGP06.phx.gbl...
>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
Oct 22 '08 #6

"bruce barker" <no****@nospam.comwrote in message
news:eK**************@TK2MSFTNGP03.phx.gbl...
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)
tshad wrote:
>"bruce barker" <no****@nospam.comwrote in message
news:Op**************@TK2MSFTNGP06.phx.gbl...
>>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

Oct 22 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Tim Reynolds | last post by:
I am using EIF to write event log entries to my application. By first creating my Event Source and then raising my event via the Event Source, I do see my entries in the Application Event Log. I...
4
by: Steve Amey | last post by:
Hi all I am creating a basic control to perform some tasks, and I want to declare some events to be raised so they can be handled from the form that the control is on. I can create my own Event...
9
by: MacDermott | last post by:
I have an Access MDB which instantiates a class in a custom DLL, manipulates it for a while, then sets it equal nothing. The MDB does other things,too, and generally behaves itself as desired....
3
by: Todd Schinell | last post by:
Back in July, Jeffery Tan posted this: http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=OWOTdf0VDHA.2296%40cpmsftngxa06.phx.gbl In response as to how to get click events from a...
3
by: jlea | last post by:
I've created a custom control based on TreeView and it handles several events, such as Mousedown. I added this custom control to the toolbox in another project, dragged the custom control to the...
3
by: cjk | last post by:
Issue Our web application requires access to write to a custom event log, yet access is denied. This access is denied because we are using impersonation, and our end-users do not (should not) have...
1
by: jazzart | last post by:
Hi there, I'm fairly new to programming with Asp.Net 2.0 so I'm finding myself regularly fumbling around in the dark a bit, so to speak. I'm currently using Visual Web Developer and have been...
2
by: Michal Valent | last post by:
I would like to fire some custom server control event before Page_Load event like this (from the trace of an aspx page) : Trace Information Category Message From First(s) From Last(s) aspx.page...
4
by: Jimmy | last post by:
hi, all I'm having a problem with creating custom events in wxpython. I have a class A handling some data processing work and another class B of GUI matter. I need GUI to display information...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.