Client-Side Script for CheckBox Web Control

M

Mickie

I'm trying to create a server Web control that renders as a checkbox that
has an OnClick event associated with it. I've created a short proof of
concept to simply attach an alert message when the checkbox is clicked. The
problem is that the override is being ignored and the OnClick event is never
rendered.

If you change 'public class ctlCheckBoxSelect : CheckBox' to
'public class ctlCheckBoxSelect : Button', then it will render successfully
as a Button with the OnClick event associated with it.

Any help with ideas for how to get the OnClick event to render for the
CheckBox in this way would be greatly appreaciated.

Thanks,
- Mickie


Here is the code:

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace ClientSideScripts
{
/// <summary>
/// Check box that pops up an alert box when clicked.
/// </summary>

[DefaultProperty("Text"),
ToolboxData("<{0}:ctlCheckBoxSelect
runat=server></{0}:ctlCheckBoxSelect>")]
public class ctlCheckBoxSelect : CheckBox
{

protected override void AddAttributesToRender(HtmlTextWriter writer)
{
string script = @"alert(""Hello"");";
writer.AddAttribute(HtmlTextWriterAttribute.Onclick, script);

base.AddAttributesToRender(writer);
//this.AddAttributesToRender(writer);
}
}
}
 
V

Victor Garcia Aprea

Hi Mickie,

Your code is ok and should work. It may be that your browser doesn't like
the rendered html, which browser are you using? Also, take a look at the
rendered html and make sure the actual html for the checkbox (an <input> and
a <label>) is there.

--
Victor Garcia Aprea
Microsoft MVP | ASP.NET
Looking for insights on ASP.NET? Read my blog:
http://obies.com/vga/blog.aspx

To contact me remove 'NOSPAM'. Please post all questions to the newsgroup
 
M

Mickie

Hi, Victor - Thanks for the reply.

I'm using IE6 on WinXP Pro.

If I set up the following lines in my test.aspx page, one for a manually
created checkbox with onclick, and one for my custom checkbox like this:

<asp:checkbox id="CheckBox1" onclick="alert('Hello');"
runat="server"></asp:checkbox>

<cc2:ctlCheckBoxSelect id="CtlCheckBoxSelect1"
runat="server"></cc2:ctlCheckBoxSelect>

I get the following html output:

<input id="CheckBox1" type="checkbox" name="CheckBox1"
onclick="alert('Hello');" />

<input id="CtlCheckBoxSelect1" type="checkbox" name="CtlCheckBoxSelect1" />

However, if you change 'public class ctlCheckBoxSelect : CheckBox' to
'public class ctlCheckBoxSelect : Button', then it will render correctly as
a button with the client-side onclick event attached. I've also tried
changing it to an imagebutton and a linkbutton, and those work fine too.

Looking into it further I see that the button AddAttributesToRender method
is overridden:
http://msdn.microsoft.com/library/d...fsystemwebuiwebcontrolsbuttonmemberstopic.asp

and the checkbox AddAttributesToRender method is not overridden:
http://msdn.microsoft.com/library/d...ystemwebuiwebcontrolscheckboxmemberstopic.asp

What I'm not sure about is where I can go from here with my custom checkbox
based on this, to get the onclick event to render - or if this is even
possible...?

Thanks -

- Mickie


Victor Garcia Aprea said:
Hi Mickie,

Your code is ok and should work. It may be that your browser doesn't like
the rendered html, which browser are you using? Also, take a look at the
rendered html and make sure the actual html for the checkbox (an <input> and
a <label>) is there.

--
Victor Garcia Aprea
Microsoft MVP | ASP.NET
Looking for insights on ASP.NET? Read my blog:
http://obies.com/vga/blog.aspx

To contact me remove 'NOSPAM'. Please post all questions to the newsgroup
Mickie said:
I'm trying to create a server Web control that renders as a checkbox that
has an OnClick event associated with it. I've created a short proof of
concept to simply attach an alert message when the checkbox is clicked. The
problem is that the override is being ignored and the OnClick event is never
rendered.

If you change 'public class ctlCheckBoxSelect : CheckBox' to
'public class ctlCheckBoxSelect : Button', then it will render successfully
as a Button with the OnClick event associated with it.

Any help with ideas for how to get the OnClick event to render for the
CheckBox in this way would be greatly appreaciated.

Thanks,
- Mickie


Here is the code:

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace ClientSideScripts
{
/// <summary>
/// Check box that pops up an alert box when clicked.
/// </summary>

[DefaultProperty("Text"),
ToolboxData("<{0}:ctlCheckBoxSelect
runat=server></{0}:ctlCheckBoxSelect>")]
public class ctlCheckBoxSelect : CheckBox
{

protected override void AddAttributesToRender(HtmlTextWriter writer)
{
string script = @"alert(""Hello"");";
writer.AddAttribute(HtmlTextWriterAttribute.Onclick, script);

base.AddAttributesToRender(writer);
//this.AddAttributesToRender(writer);
}
}
}
 
J

Jesse

I think a checkbox only has a CheckedChanged method, not an onclick method. Try overloading the CheckedChanged method and see if that works


----- Mickie wrote: ----

I'm trying to create a server Web control that renders as a checkbox tha
has an OnClick event associated with it. I've created a short proof o
concept to simply attach an alert message when the checkbox is clicked. Th
problem is that the override is being ignored and the OnClick event is neve
rendered

If you change 'public class ctlCheckBoxSelect : CheckBox' t
'public class ctlCheckBoxSelect : Button', then it will render successfull
as a Button with the OnClick event associated with it

Any help with ideas for how to get the OnClick event to render for th
CheckBox in this way would be greatly appreaciated

Thanks
- Micki


Here is the code

using System
using System.Web.UI
using System.Web.UI.WebControls
using System.ComponentModel

namespace ClientSideScript

/// <summary
/// Check box that pops up an alert box when clicked
/// </summary

[DefaultProperty("Text")
ToolboxData("<{0}:ctlCheckBoxSelec
runat=server></{0}:ctlCheckBoxSelect>")
public class ctlCheckBoxSelect : CheckBo


protected override void AddAttributesToRender(HtmlTextWriter writer

string script = @"alert(""Hello"");"
writer.AddAttribute(HtmlTextWriterAttribute.Onclick, script)

base.AddAttributesToRender(writer)
//this.AddAttributesToRender(writer)
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top