Client-Side Script for CheckBox Web Control

Ask a Question related to ASP.NET Building Controls, Design and Development.

  1. #1

    Default Client-Side Script for CheckBox Web Control

    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);
    }
    }
    }


    Mickie Guest

  2. Similar Questions and Discussions

    1. Client side script ASP.NET
      Hi, How can i eassely add client side script in my code. I have a sort of TAB control, that works through postbacks but i want the action taken...
    2. client side checkbox event
      This is probably a simple one, but I cannot seem to find an example. I have a datagrid with each row containing Checkbox | Textbox |...
    3. Client Side Script interferes with Server Side Script?
      I added the form validation script (Java Script) to the head section of my insert records form page which works great when tested but when I submit...
    4. Can client-side script be included in a user control (ascx)?
      I am creating a simple usercontrol (.ascx). There is some client-side functionality I would like it to have. I tried adding in a Script element,...
    5. Client Side Java Script
      Hi, am not a ASP.NET programmer, and it seems that i have to learn web development, so i need help learning java script. any links to some...
  3. #2

    Default Re: Client-Side Script for CheckBox Web Control

    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:
    [url]http://obies.com/vga/blog.aspx[/url]

    To contact me remove 'NOSPAM'. Please post all questions to the newsgroup
    "Mickie" <mstamm@synergistix.net> wrote in message
    news:eQRN5eWsDHA.1996@TK2MSFTNGP09.phx.gbl...
    > 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);
    > }
    > }
    > }
    >
    >

    Victor Garcia Aprea Guest

  4. #3

    Default Re: Client-Side Script for CheckBox Web Control

    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:
    [url]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuiwebcontrolsbuttonmemberstopic.asp[/url]

    and the checkbox AddAttributesToRender method is not overridden:
    [url]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuiwebcontrolscheckboxmemberstopic.as p[/url]

    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" <vga@obies.com> wrote in message
    news:%235G$%23n9sDHA.2400@tk2msftngp13.phx.gbl...
    > 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:
    > [url]http://obies.com/vga/blog.aspx[/url]
    >
    > To contact me remove 'NOSPAM'. Please post all questions to the newsgroup
    > "Mickie" <mstamm@synergistix.net> wrote in message
    > news:eQRN5eWsDHA.1996@TK2MSFTNGP09.phx.gbl...
    > > 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);
    > > }
    > > }
    > > }
    > >
    > >
    >
    >

    Mickie Guest

  5. #4

    Default RE: Client-Side Script for CheckBox Web Control

    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)






    Jesse Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139