Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Building Controls > Client-Side Script for CheckBox Web Control

Reply
Thread Tools

Client-Side Script for CheckBox Web Control

 
 
Mickie
Guest
Posts: n/a
 
      11-23-2003
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.Onclic k, script);

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


 
Reply With Quote
 
 
 
 
Victor Garcia Aprea
Guest
Posts: n/a
 
      11-26-2003
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.Onclic k, script);
>
> base.AddAttributesToRender(writer);
> //this.AddAttributesToRender(writer);
> }
> }
> }
>
>



 
Reply With Quote
 
 
 
 
Mickie
Guest
Posts: n/a
 
      11-26-2003
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/de...mberstopic.asp

and the checkbox AddAttributesToRender method is not overridden:
http://msdn.microsoft.com/library/de...mberstopic.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" <(E-Mail Removed)> wrote in message
news:%235G$%(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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.Onclic k, script);
> >
> > base.AddAttributesToRender(writer);
> > //this.AddAttributesToRender(writer);
> > }
> > }
> > }
> >
> >

>
>



 
Reply With Quote
 
Jesse
Guest
Posts: n/a
 
      12-01-2003
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.Onclic k, script)

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






 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DataGrid and embeded Checkbox..How to find if checkbox clicked =?Utf-8?B?RG90TmV0RGV2?= ASP .Net 1 10-06-2006 04:11 PM
Web user control - custom web control - java script. Leyla ASP .Net Web Controls 0 05-01-2006 10:05 PM
disable checkbox list checkbox Vikram ASP .Net 1 01-25-2006 02:59 PM
checkbox value attribute in checkbox control ? Imran Aziz ASP .Net Web Controls 2 08-08-2005 11:12 AM
Text on Checkbox below the checkbox tshad ASP .Net 0 04-14-2005 11:26 PM



Advertisments