javascript in user control.

Hi all,

i am having one user control. what i want is to add javascript which
will gets called on button click of user control.

but user control is not working if i add javascript in user control..
but if i add javascript in page in which i am adding user control then
that javascript is executed properly.

i tested by displaying alert message in javascript. can anyone tell
me how will i add javascript in user control.

and one more thing controls present in user control, can't be access
directly in javascript. i checked viewsource of page what i found is
for controls of user control 'ctl' get prefixed. can nyone tell me
reason behind this?

any how will i get this id of controls of user control?

please help me asap.

thanks in advance
archana [ Di, 22 April 2008 08:01 ] [ ID #1947359 ]

Re: javascript in user control.

Javascript does work in webusercontrol .Can you post your code here?

archana wrote:
> Hi all,
>
> i am having one user control. what i want is to add javascript which
> will gets called on button click of user control.
>
> but user control is not working if i add javascript in user control..
> but if i add javascript in page in which i am adding user control then
> that javascript is executed properly.
>
> i tested by displaying alert message in javascript. can anyone tell
> me how will i add javascript in user control.
>
> and one more thing controls present in user control, can't be access
> directly in javascript. i checked viewsource of page what i found is
> for controls of user control 'ctl' get prefixed. can nyone tell me
> reason behind this?
>
> any how will i get this id of controls of user control?
>
> please help me asap.
>
> thanks in advance
arnabit [ Di, 22 April 2008 09:10 ] [ ID #1947361 ]

Re: javascript in user control.

On 22 Apr, 07:01, archana <trialproduct2... [at] yahoo.com> wrote:
> Hi all,
>
> i am having one user control. =A0what i want is to add javascript which
> will gets called on button click of user control.
>
> but user control is not working if i add javascript in user control..
> but if i add javascript in page in which i am adding user control then
> that javascript is executed properly.
>
> i tested by displaying alert message in javascript. =A0can anyone tell
> me how will i add javascript in user control.
>
> and one more thing controls present in user control, can't be access
> directly in javascript. =A0i checked viewsource of page what i found is
> for controls of user control 'ctl' get prefixed. =A0can nyone tell me
> reason behind this?
>
> any how will i get this id of controls of user control?
>
> please help me asap.
>
> thanks in advance

Try inserting the script using the Page.ClientScript property to
create a ClientScriptManager class to insert the script.

The ClientID property of web server controls is constructed to
guarantee a unique id within the context of the rendered page. The
control's ID property is not used as it is because scoping rules allow
identifiers to be re-used within code and within templates inside
databound controls.
Stan [ Di, 22 April 2008 09:20 ] [ ID #1947362 ]

Re: javascript in user control.

On Apr 22, 12:20=A0pm, Stan <goo... [at] philphall.me.uk> wrote:
> On 22 Apr, 07:01, archana <trialproduct2... [at] yahoo.com> wrote:
>
>
>
>
>
> > Hi all,
>
> > i am having one user control. =A0what i want is to add javascript which
> > will gets called on button click of user control.
>
> > but user control is not working if i add javascript in user control..
> > but if i add javascript in page in which i am adding user control then
> > that javascript is executed properly.
>
> > i tested by displaying alert message in javascript. =A0can anyone tell
> > me how will i add javascript in user control.
>
> > and one more thing controls present in user control, can't be access
> > directly in javascript. =A0i checked viewsource of page what i found is
> > for controls of user control 'ctl' get prefixed. =A0can nyone tell me
> > reason behind this?
>
> > any how will i get this id of controls of user control?
>
> > please help me asap.
>
> > thanks in advance
>
> Try inserting the script using the Page.ClientScript property to
> create a ClientScriptManager class to insert the script.
>
> The ClientID property of web server controls is constructed to
> guarantee a unique id within the context of the rendered page. The
> control's ID property is not used as it is because scoping rules allow
> identifiers to be re-used within code and within templates inside
> databound controls.- Hide quoted text -
>
> - Show quoted text -

Thanks a lot for reply.

how will i access controls of user cotrol in parent page?

please help me asap.
archana [ Di, 22 April 2008 10:15 ] [ ID #1947364 ]

Re: javascript in user control.

Hi Archana,

I'm not sure if you would like to access the controls in your user
control in codebehind or in Javascript. The below sample does both. In
both cases, the trick is to use FindControl on your user control.
Please post back if this doesn't answer your question.

Default.aspx:

<% [at] Page Language=3D"C#" AutoEventWireup=3D"true"
CodeBehind=3D"Default.aspx.cs" Inherits=3D"JSInUserControlTest._Default"
%>
<% [at] Register Src=3D"~/WebUserControl1.ascx" TagName=3D"UserCtrl"
TagPrefix=3D"my" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns=3D"http://www.w3.org/1999/xhtml" >
<head runat=3D"server">
<title>Untitled Page</title>
<script type=3D"text/javascript">
function ChangeText() {
var theLabel =3D document.getElementById('<%=3D
myUserCtrl.FindControl("theLabel").ClientID %>');
theLabel.innerText =3D "Changed By JS";
}
</script>
</head>
<body>
<form id=3D"form1" runat=3D"server">
<div>
<my:UserCtrl ID=3D"myUserCtrl" runat=3D"server" />
<asp:Button ID=3D"theJsButton" Text=3D"Js Button"
OnClientClick=3D"ChangeText();return false" runat=3D"server" />
<asp:Button ID=3D"theAspButton" Text=3D"ASP Button"
OnClick=3D"theAspButton_Click" runat=3D"server" />
</div>
</form>
</body>
</html>

Default.aspx.cs:

using System;
using System.Web.UI.WebControls;

namespace JSInUserControlTest
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void theAspButton_Click(object sender, EventArgs e)
{
Label lbl =3D (Label)myUserCtrl.FindControl("theLabel");
lbl.Text =3D "Changed in codebehind";
}

}
}

WebUserControl1.ascs:

<% [at] Control Language=3D"C#" AutoEventWireup=3D"true"
CodeBehind=3D"WebUserControl1.ascx.cs"
Inherits=3D"JSInUserControlTest.WebUserControl1" %>
<asp:Label ID=3D"theLabel" Text=3D"Some text here" runat=3D"server" />

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
Regards,
Steve
www.stkomp.com


archana wrote:
> On Apr 22, 12:20=EF=BF=BDpm, Stan <goo... [at] philphall.me.uk> wrote:
> > On 22 Apr, 07:01, archana <trialproduct2... [at] yahoo.com> wrote:
> >
> >
> >
> >
> >
> > > Hi all,
> >
> > > i am having one user control. =EF=BF=BDwhat i want is to add javascrip=
t which
> > > will gets called on button click of user control.
> >
> > > but user control is not working if i add javascript in user control..
> > > but if i add javascript in page in which i am adding user control then=

> > > that javascript is executed properly.
> >
> > > i tested by displaying alert message in javascript. =EF=BF=BDcan anyon=
e tell
> > > me how will i add javascript in user control.
> >
> > > and one more thing controls present in user control, can't be access
> > > directly in javascript. =EF=BF=BDi checked viewsource of page what i f=
ound is
> > > for controls of user control 'ctl' get prefixed. =EF=BF=BDcan nyone te=
ll me
> > > reason behind this?
> >
> > > any how will i get this id of controls of user control?
> >
> > > please help me asap.
> >
> > > thanks in advance
> >
> > Try inserting the script using the Page.ClientScript property to
> > create a ClientScriptManager class to insert the script.
> >
> > The ClientID property of web server controls is constructed to
> > guarantee a unique id within the context of the rendered page. The
> > control's ID property is not used as it is because scoping rules allow
> > identifiers to be re-used within code and within templates inside
> > databound controls.- Hide quoted text -
> >
> > - Show quoted text -
>
> Thanks a lot for reply.
>
> how will i access controls of user cotrol in parent page?
>
> please help me asap.
wisccal [ Mi, 23 April 2008 07:08 ] [ ID #1948313 ]
Microsoft » microsoft.public.dotnet.framework.aspnet » javascript in user control.

Vorheriges Thema: enable execute permission in web.config
Nächstes Thema: Web Page & WebService Threading Example