[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.aspnet.webcontrols

Problem with ASP Control & Javascript

Patrick

1/13/2003 5:17:00 PM

I have a problem with using Javascript in a Control. I have an aspx Page
containing a form

<Form name="myform"> .... </Form>

Within these tags i load my own control, that contains

<script>
function goclick()
{
document.myform.checkbox1.checked=true;
document.myform.checkbox2.checked=true;
}
</script>
<asp:checkbox runat=server id=checkbox1 onclick="javascript:goclick()"/>
<asp:checkbox runat=server id=checkbox2 onclick="javascript:goclick()"/>

So, it should, when clicking one checkbox also select the other one. The
problem is, that when rendering the control the name Tag changes to
_ctl0:checkbox1 and so it can't be found anymore. When i add it twice, it
changes to _ctl1:checkbox1.

Is there a way to add dynamic javascript that also knows the ctlx name? or
is the only solution to use the OnCheckedChanged event? Just problem is,
that this event reloads the page.

Any ideas?
Thanks


4 Answers

Kim Bach Petersen

1/13/2003 6:30:00 PM

0

> So, it should, when clicking one checkbox also select the other one.
> The problem is, that when rendering the control the name Tag changes
> to _ctl0:checkbox1 and so it can't be found anymore. When i add it
> twice, it changes to _ctl1:checkbox1.

Use the ClientId-property of the CheckBox to reference it clientside:

"document.getElementById('" & checkbox1.ClientId & "').checked=true;"

If you only need to adress one control, you don't need a specific reference:
you can send a reference to an object with a javascript function:

<asp:checkbox runat=server id=checkbox2 onclick="javascript:goclick(this)"/>

function goclick(mycheckbox) {mycheckbox.checked=true;}

Kim :o)


Patrick

1/14/2003 3:13:00 PM

0

Thanks, that was exactly what I was looking for. Just saw, that this Method
won't work on Netscape, because the onclick Event will be inserted into a
SPAN-Tag .. but no problem with that.

Thanks

"Kim Bach Petersen" <msnews@kensho.dk> schrieb im Newsbeitrag
news:eewC4lyuCHA.2372@TK2MSFTNGP12...
> > So, it should, when clicking one checkbox also select the other one.
> > The problem is, that when rendering the control the name Tag changes
> > to _ctl0:checkbox1 and so it can't be found anymore. When i add it
> > twice, it changes to _ctl1:checkbox1.
>
> Use the ClientId-property of the CheckBox to reference it clientside:
>
> "document.getElementById('" & checkbox1.ClientId & "').checked=true;"
>
> If you only need to adress one control, you don't need a specific
reference:
> you can send a reference to an object with a javascript function:
>
> <asp:checkbox runat=server id=checkbox2
onclick="javascript:goclick(this)"/>
>
> function goclick(mycheckbox) {mycheckbox.checked=true;}
>
> Kim :o)
>
>


Kim Bach Petersen

1/14/2003 5:40:00 PM

0

> Thanks, that was exactly what I was looking for. Just saw, that this
> Method won't work on Netscape, because the onclick Event will be
> inserted into a SPAN-Tag .. but no problem with that.

Hooking events on the 'onclick'-event can be a problem in asp.net because
many types of controls can use 'onclick' for embedded behaviors. Somtimes
its helpfull to add the javascript programmically:

MyControl.Attributes.add("onclick", "myfunction()")

In other cases one might need to cheat by using 'onmousedown' instead - the
user won't see the difference, but it's trigged before 'onclick'.

Kim :o)


Patrick

1/15/2003 10:46:00 AM

0


> MyControl.Attributes.add("onclick", "myfunction()")
>

this works only on IE. because it renders then an SPAN Tag arround the
asp-CheckboxControl and Netscape 4.7 for Example has no onclick Event in the
SPAN-Tag. But anyway, this solutions has not to run on Netscape, so it's ok.