Ask a Question related to ASP.NET Building Controls, Design and Development.
-
Mickie #1
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
-
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... -
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 |... -
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... -
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,... -
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... -
Victor Garcia Aprea #2
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...The> 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.never> problem is that the override is being ignored and the OnClick event issuccessfully> rendered.
>
> If you change 'public class ctlCheckBoxSelect : CheckBox' to
> 'public class ctlCheckBoxSelect : Button', then it will render> 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
-
Mickie #3
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...and> 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>that> 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> The> > 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.> never> > problem is that the override is being ignored and the OnClick event is> successfully> > rendered.
> >
> > If you change 'public class ctlCheckBoxSelect : CheckBox' to
> > 'public class ctlCheckBoxSelect : Button', then it will render>> > 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
-
Jesse #4
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



Reply With Quote

