Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / April 2008

Tip: Looking for answers? Try searching our database.

javascript in user control.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
archana - 22 Apr 2008 07:01 GMT
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
Arnab das - 22 Apr 2008 08:10 GMT
Javascript does work in webusercontrol .Can you post your code here?

> Hi all,
>
[quoted text clipped - 18 lines]
>
> thanks in advance
Stan - 22 Apr 2008 08:20 GMT
> Hi all,
>
[quoted text clipped - 18 lines]
>
> 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.
archana - 22 Apr 2008 09:15 GMT
> > Hi all,
>
[quoted text clipped - 29 lines]
>
> - Show quoted text -

Thanks a lot for reply.

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

please help me asap.
wisccal@googlemail.com - 23 Apr 2008 06:08 GMT
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

> > > Hi all,
> >
[quoted text clipped - 35 lines]
>
> please help me asap.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.