473,396 Members | 1,945 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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
Jun 27 '08 #1
4 4218
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
Jun 27 '08 #2
On 22 Apr, 07:01, archana <trialproduct2...@yahoo.comwrote:
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
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.
Jun 27 '08 #3
On Apr 22, 12:20*pm, Stan <goo...@philphall.me.ukwrote:
On 22 Apr, 07:01, archana <trialproduct2...@yahoo.comwrote:


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

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.
Jun 27 '08 #4
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:

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

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

}
}

WebUserControl1.ascs:

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

==============
Regards,
Steve
www.stkomp.com
archana wrote:
On Apr 22, 12:20�pm, Stan <goo...@philphall.me.ukwrote:
On 22 Apr, 07:01, archana <trialproduct2...@yahoo.comwrote:


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
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.
Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: New User | last post by:
I have a System.Web.UI.UserControl as custom control and I have a javascript block for user control. The problem is I want to bring src attribute from outside as property or other method. e.g...
8
by: rn5a | last post by:
I have gone through a no. of posts in this NewsGroup regarding my problem but alas, couldn't come across one which would have helped me in resolving the issue. My problem is this: An ASPX Form...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.