Client-Side Script for CheckBox Web Control

Discussion in 'ASP .Net Building Controls' started by Mickie, Nov 23, 2003.

  1. Mickie

    Mickie Guest

    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);
    }
    }
    }
     
    Mickie, Nov 23, 2003
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:...
    > 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);
    > }
    > }
    > }
    >
    >
     
    Victor Garcia Aprea, Nov 26, 2003
    #2
    1. Advertising

  3. Mickie

    Mickie Guest

    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" <> wrote in message
    news:%235G$%...
    > 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" <> wrote in message
    > news:...
    > > 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);
    > > }
    > > }
    > > }
    > >
    > >

    >
    >
     
    Mickie, Nov 26, 2003
    #3
  4. Mickie

    Jesse Guest

    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)
     
    Jesse, Dec 1, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Magnus Blomberg
    Replies:
    3
    Views:
    720
    Magnus Blomberg
    Apr 14, 2005
  2. dotnet dude
    Replies:
    0
    Views:
    458
    dotnet dude
    Sep 15, 2006
  3. Boss302
    Replies:
    0
    Views:
    1,053
    Boss302
    Nov 21, 2006
  4. Bogdan
    Replies:
    2
    Views:
    655
    Bogdan
    Jun 9, 2008
  5. Zoe Hart
    Replies:
    1
    Views:
    373
    Scott Wisniewski
    Jan 8, 2004
Loading...

Share This Page