473,320 Members | 1,910 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,320 software developers and data experts.

Events and delegates...


Hello,
I have populated an a web form with dynamically created radiobuutons in
a table. I wnat some of the radio buttons to fire events when their
..CheckChanged property has changed.
I understand and was told this can be done with events and delegates. But my
code would
not compile. Here is what i did.

public delegate void specialRadioEventHandler(object sender, EventArgs e);

....

....

....

private void BuildMyDialog()

{

//Dynamically creating my table . Function called in
page_Load(IsPostBack==false)

....

....

....

RadioButton rb = new RadioButton();

if (RadioMeetMycodnition)

{

rb.AutoPostBack = true;

rb.CheckedChanged+=new MyOwnEventHandler( DoSomething );

}

public voidDoSomething (object sender, EventArgs e)

{

//Only for testing purpose.

string teststring = "oi";

Response.Write(teststring);

}
.......

.......

.....

I'am i doing something wrong?

Thanks...
Jul 21 '05 #1
14 1288
bredal Jensen <br******@jensen.dk> wrote:

<snip>
I'am i doing something wrong?


Yes:

rb.CheckedChanged+=new MyOwnEventHandler( DoSomething );

You've got to use the type of the event, namely EventHandler:

rb.CheckedChanged += new EventHandler (DoSomething);
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2
bredal Jensen <br******@jensen.dk> wrote:

<snip>
I'am i doing something wrong?


Yes:

rb.CheckedChanged+=new MyOwnEventHandler( DoSomething );

You've got to use the type of the event, namely EventHandler:

rb.CheckedChanged += new EventHandler (DoSomething);
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #3
Well does this mean, there is no need for delegates here? and the only code
i need is the following?

RadioButton rb = new RadioButton();

if (RadioMeetMycodnition)

{

rb.AutoPostBack = true;

rb.CheckedChanged+=new EventHandler ( DoSomething );

}

public voidDoSomething (object sender, EventArgs e)

{

//Only for testing purpose.

string teststring = "oi";

Response.Write(teststring);

}

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
bredal Jensen <br******@jensen.dk> wrote:

<snip>
I'am i doing something wrong?


Yes:

rb.CheckedChanged+=new MyOwnEventHandler( DoSomething );

You've got to use the type of the event, namely EventHandler:

rb.CheckedChanged += new EventHandler (DoSomething);
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #4
Well does this mean, there is no need for delegates here? and the only code
i need is the following?

RadioButton rb = new RadioButton();

if (RadioMeetMycodnition)

{

rb.AutoPostBack = true;

rb.CheckedChanged+=new EventHandler ( DoSomething );

}

public voidDoSomething (object sender, EventArgs e)

{

//Only for testing purpose.

string teststring = "oi";

Response.Write(teststring);

}

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
bredal Jensen <br******@jensen.dk> wrote:

<snip>
I'am i doing something wrong?


Yes:

rb.CheckedChanged+=new MyOwnEventHandler( DoSomething );

You've got to use the type of the event, namely EventHandler:

rb.CheckedChanged += new EventHandler (DoSomething);
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #5
bredal Jensen <br******@jensen.dk> wrote:
Well does this mean, there is no need for delegates here? and the only code
i need is the following?


That *is* using delegates - EventHandler is a delegate.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #6
bredal Jensen <br******@jensen.dk> wrote:
Well does this mean, there is no need for delegates here? and the only code
i need is the following?


That *is* using delegates - EventHandler is a delegate.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #7
That is correct and i just verified it...

When i select my button , it does not fire my custom event.
Well i'm obviously still missing something.

Anyway thank you for your assistance.


"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
bredal Jensen <br******@jensen.dk> wrote:
Well does this mean, there is no need for delegates here? and the only code i need is the following?


That *is* using delegates - EventHandler is a delegate.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #8
That is correct and i just verified it...

When i select my button , it does not fire my custom event.
Well i'm obviously still missing something.

Anyway thank you for your assistance.


"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
bredal Jensen <br******@jensen.dk> wrote:
Well does this mean, there is no need for delegates here? and the only code i need is the following?


That *is* using delegates - EventHandler is a delegate.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #9
bredal Jensen <br******@jensen.dk> wrote:
That is correct and i just verified it...

When i select my button , it does not fire my custom event.
Well i'm obviously still missing something.

Anyway thank you for your assistance.


Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #10
bredal Jensen <br******@jensen.dk> wrote:
That is correct and i just verified it...

When i select my button , it does not fire my custom event.
Well i'm obviously still missing something.

Anyway thank you for your assistance.


Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #11
Well i 'll try , i hope the listing can be read...

public class DamageEntry : System.Web.UI.Page
{

private void Page_Load(object sender, System.EventArgs e)
{
if (Page.IsPostBack)
{
//Did some initialasation non relevent for the problem
}
else
{

//Did some initialasation non relevent for the problem
BuildDialog() ;

}

}
private void BuildDialog()
{
// Turn on the Damage Entry Table, but deactivate the subparts list.
DamageEntryTable.Visible = true;
SubpartsList.Visible = false;

//if (subpart==true) damTypes =
CarHandle.GetLocalizedDamageTypesSubpart(DBrm);
// Build rest of table.
foreach (DataRow typ in damTypes.Table.Rows)
{
// Add a new row.
TableRow r = new TableRow();

// Add a cell with the damage type description.
string damTypName = (string)typ["dt_name"];

TableCell ct = new TableCell();
ct.Text = damTypName;
ct.CssClass = "DamType";
r.Cells.Add(ct);
byte damTypID = (byte)typ["dt_id"];
foreach (DataRow sev in damSeverities.Table.Rows)
{
if(int.Parse(sev["dsev_id"].ToString()) == 0) // Don't show the blank
field here!
continue;
TableCell c = new TableCell();
c.BackColor = Color.FromArgb((byte)sev["dsev_color_r"],
(byte)sev["dsev_color_g"], (byte)sev["dsev_color_b"]);
RadioButton rb = new RadioButton();

byte damSevID = (byte)sev["dsev_id"];
//rb.ID = ConjureRadioButtonID(damTypID, (byte)sev["dsev_id"]);
rb.ID = damSevID.ToString();

if (ct.Text ==string.Empty)
{
rb.AutoPostBack = true; <**********The code
ctually reach this place. I verrified with a breakpoint***********>

rb.CheckedChanged += new EventHandler(rb_CheckedChanged);
**********************************************
}

rb.GroupName = damTypID.ToString();
c.Controls.Add(rb);
r.Cells.Add(c);

}
DamageEntryTable.Rows.Add(r);
}

// iPAQ has problems with Danish chars - I will HtmlEncode some of the
texts for now...
// Show location name - and subpart name if necessary.
DamLocLabel.Text = Server.HtmlEncode((string)activeRow["dl_name"]);
if (subpart) DamLocLabel.Text += Server.HtmlEncode(" - " +
(string)activeRowSupp["dls_name"]);

// "Other" button is only available if location has subparts (and we are
not already doing subpart dialog).
ExtendedButton.Enabled = !subpart && !activeRow.IsNull("dl_supp_group");

// The back button - disabled for now.
// BackButton.Enabled = false;
}
public void rb_CheckedChanged(object sender, System.EventArgs e)
{
string teststring = "se"; <***********This is
never called*************>
Response.Write();
}


"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
bredal Jensen <br******@jensen.dk> wrote:
That is correct and i just verified it...

When i select my button , it does not fire my custom event.
Well i'm obviously still missing something.

Anyway thank you for your assistance.


Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #12
Well i 'll try , i hope the listing can be read...

public class DamageEntry : System.Web.UI.Page
{

private void Page_Load(object sender, System.EventArgs e)
{
if (Page.IsPostBack)
{
//Did some initialasation non relevent for the problem
}
else
{

//Did some initialasation non relevent for the problem
BuildDialog() ;

}

}
private void BuildDialog()
{
// Turn on the Damage Entry Table, but deactivate the subparts list.
DamageEntryTable.Visible = true;
SubpartsList.Visible = false;

//if (subpart==true) damTypes =
CarHandle.GetLocalizedDamageTypesSubpart(DBrm);
// Build rest of table.
foreach (DataRow typ in damTypes.Table.Rows)
{
// Add a new row.
TableRow r = new TableRow();

// Add a cell with the damage type description.
string damTypName = (string)typ["dt_name"];

TableCell ct = new TableCell();
ct.Text = damTypName;
ct.CssClass = "DamType";
r.Cells.Add(ct);
byte damTypID = (byte)typ["dt_id"];
foreach (DataRow sev in damSeverities.Table.Rows)
{
if(int.Parse(sev["dsev_id"].ToString()) == 0) // Don't show the blank
field here!
continue;
TableCell c = new TableCell();
c.BackColor = Color.FromArgb((byte)sev["dsev_color_r"],
(byte)sev["dsev_color_g"], (byte)sev["dsev_color_b"]);
RadioButton rb = new RadioButton();

byte damSevID = (byte)sev["dsev_id"];
//rb.ID = ConjureRadioButtonID(damTypID, (byte)sev["dsev_id"]);
rb.ID = damSevID.ToString();

if (ct.Text ==string.Empty)
{
rb.AutoPostBack = true; <**********The code
ctually reach this place. I verrified with a breakpoint***********>

rb.CheckedChanged += new EventHandler(rb_CheckedChanged);
**********************************************
}

rb.GroupName = damTypID.ToString();
c.Controls.Add(rb);
r.Cells.Add(c);

}
DamageEntryTable.Rows.Add(r);
}

// iPAQ has problems with Danish chars - I will HtmlEncode some of the
texts for now...
// Show location name - and subpart name if necessary.
DamLocLabel.Text = Server.HtmlEncode((string)activeRow["dl_name"]);
if (subpart) DamLocLabel.Text += Server.HtmlEncode(" - " +
(string)activeRowSupp["dls_name"]);

// "Other" button is only available if location has subparts (and we are
not already doing subpart dialog).
ExtendedButton.Enabled = !subpart && !activeRow.IsNull("dl_supp_group");

// The back button - disabled for now.
// BackButton.Enabled = false;
}
public void rb_CheckedChanged(object sender, System.EventArgs e)
{
string teststring = "se"; <***********This is
never called*************>
Response.Write();
}


"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
bredal Jensen <br******@jensen.dk> wrote:
That is correct and i just verified it...

When i select my button , it does not fire my custom event.
Well i'm obviously still missing something.

Anyway thank you for your assistance.


Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #13
bredal Jensen <br******@jensen.dk> wrote:
Well i 'll try , i hope the listing can be read...


Sort of - I believe it's better if you turn off "paragraph mode" or
something similar in IE.

I'm afraid I haven't really done any ASP.NET, and don't have a server
set up at the moment - hopefully someone else will test your code
though. (It would probably help if you'd post the aspx file as well.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #14
bredal Jensen <br******@jensen.dk> wrote:
Well i 'll try , i hope the listing can be read...


Sort of - I believe it's better if you turn off "paragraph mode" or
something similar in IE.

I'm afraid I haven't really done any ASP.NET, and don't have a server
set up at the moment - hopefully someone else will test your code
though. (It would probably help if you'd post the aspx file as well.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #15

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

Similar topics

4
by: Marty McDonald | last post by:
It is still unclear to me why we would use events when delegates seem to do just fine. People say that events make it so the publisher doesn't need to know about the listeners. What does that...
7
by: Rakesh Rajan | last post by:
Hi, I find that when i define a delegate, it gets derived from MulticastDelegate, which provides all funtionality that events would provide (like registering new handlers etc.). Then, apart from...
4
by: LP | last post by:
Hello! I am still transitioning from VB.NET to C#. I undertand the basic concepts of Delegates, more so of Events and somewhat understand AsyncCallback methods. But I need some clarification on...
3
by: Chris | last post by:
Hi, what is the difference between using events and delegates (apart from the syntax) ? have a look at following (working) programs please (you can just copy/paste and build it) : First...
11
by: Nicky Smith | last post by:
Hello, I'm studying a book on VB.net Win apps, and I'm reading a section on events and delegates and raising events. Is it just me, or is this not just subs dressed up as something else? I...
4
by: Tim | last post by:
There are a set of clients who need to be notified of certain events. I have used events and delegates (publisher-Subscriber model) for the notification mechanism. All the clients register with...
30
by: Burkhard | last post by:
Hi, I am new to C# (with long year experience in C++) and I am a bit confused by the language construct of events. What is it I can do with events that I cannot do with delegates? At the moment...
2
by: kristian.freed | last post by:
Hi, I currently work in a project written fully in C# where we make extensive use of delegates and events. We have a model where a "state", an object holding data but not much code but which...
5
by: raylopez99 | last post by:
I understand delegates (static and non-static) and I agree they are very useful, and that the "Forms" used in the Windows .NET API could not work without them. That said, I'm curious as to how...
7
by: Siegfried Heintze | last post by:
I'm studying the book "Microsoft Visual Basic.NET Language Reference" and I would like some clarify the difference between events and delegates. On page 156 I see a WinForms example of timer that...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.