473,486 Members | 2,407 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

javascript in ascx file

27 New Member
Hi,
i have an ascx file(web control file ) haivng code like

<asp:LinkButton ID="col" runat="server" Text='<%# Eval("8Y") %>' OnClick="ColSelected" />

now i am using code behind implmentation for ColSelected.I want to move
this to a javascript.Can I write script tags in ascx file ?if yes how ? and how to link Onclick with the script inside the ascx file

regards
mp
Oct 18 '07 #1
7 11882
Plater
7,872 Recognized Expert Expert
I believe using onmouseclick= will tell it to point to a javascript function.
Oct 18 '07 #2
mikeyeli
63 New Member
Hi,
i have an ascx file(web control file ) haivng code like

<asp:LinkButton ID="col" runat="server" Text='<%# Eval("8Y") %>' OnClick="ColSelected" />

now i am using code behind implmentation for ColSelected.I want to move
this to a javascript.Can I write script tags in ascx file ?if yes how ? and how to link Onclick with the script inside the ascx file

regards
mp
yes you can,

try:
javascript:
Expand|Select|Wrap|Line Numbers
  1. onclick="javascript:whateverFunction();"
thus, some asp .net controls seem to have issues with events that are called the same:
what i mean is, asp .net recognizes OnClick, not onclick, and for some reason it "overwrites" it and it doesnt pay attention to the onclick on the html code, so the client side onclick does nothing(on .net controls, if you use pure html elements there is no problem).

so what i do to workaround this is, register the attribute serverside. on the load or anywhere else

C#:
Expand|Select|Wrap|Line Numbers
  1. col.Attributes.Add("onclick", "javascipt:whateverFunction()");
and it fixes this issue, done it a million times, so i know it works
Oct 18 '07 #3
mpl
27 New Member
when i put this code in the page load of the code behind
col.Attributes.Add("onclick", "javascipt:whateverFunction()");

i get error 'col is not declared '.why the ID not being in the scope?

In the ascx file I added
<asp:LinkButton ID="col" runat="server" Text='<%# Eval("1Y") %>' OnClick="javascript:whateverFunction();"/>

but an underline comes in as above .why ?

thanks
mp


yes you can,

try:
javascript:
Expand|Select|Wrap|Line Numbers
  1. onclick="javascript:whateverFunction();"
thus, some asp .net controls seem to have issues with events that are called the same:
what i mean is, asp .net recognizes OnClick, not onclick, and for some reason it "overwrites" it and it doesnt pay attention to the onclick on the html code, so the client side onclick does nothing(on .net controls, if you use pure html elements there is no problem).

so what i do to workaround this is, register the attribute serverside. on the load or anywhere else

C#:
Expand|Select|Wrap|Line Numbers
  1. col.Attributes.Add("onclick", "javascipt:whateverFunction()");
and it fixes this issue, done it a million times, so i know it works
Oct 19 '07 #4
Plater
7,872 Recognized Expert Expert
if there is an error in the aspx side (<asp:LinkButton ID="col" runat="server" Text='<%# Eval("1Y") %>' />) the object might not show up in the backend code page.
Oct 19 '07 #5
mikeyeli
63 New Member
when i put this code in the page load of the code behind
col.Attributes.Add("onclick", "javascipt:whateverFunction()");

i get error 'col is not declared '.why the ID not being in the scope?

In the ascx file I added
<asp:LinkButton ID="col" runat="server" Text='<%# Eval("1Y") %>' OnClick="javascript:whateverFunction();"/>

but an underline comes in as above .why ?

thanks
mp
when i explained the whole "issue" paragraph i forgot to mention that if you do the "workaround" thing, there is no need to add the attribute in the html, since you already are doing that server side.

one more thing, remember that since a web user control, is just a chunk of html, any javascript should be in the parent form using that user control.

so the parent aspx would look like
HTML:
Expand|Select|Wrap|Line Numbers
  1.    .
  2.    .
  3.    .
  4. <script language="javascript">
  5. <!--
  6. function whateverFunction()
  7. {
  8.     alert('Hello World!')
  9. }
  10. //-->
  11. </script>
  12.     </HEAD>
  13.     <body MS_POSITIONING="GridLayout">
  14.         <form id="Form1" method="post" runat="server">
  15.             <uc1:WebUserControl1 id="WebUserControl11" runat="server"></uc1:WebUserControl1>
  16.         </form>
  17.     </body>
  18. </HTML>
the user control aspx:
HTML:
Expand|Select|Wrap|Line Numbers
  1. <asp:LinkButton id="LinkButton1" runat="server">LinkButton</asp:LinkButton>
you can see there are no modifications on the controls html tag

now the user controls code behind
c#:
Expand|Select|Wrap|Line Numbers
  1. private void Page_Load(object sender, System.EventArgs e)
  2. {
  3.          this.LinkButton1.Attributes.Add("onclick", "javascript:whateverFunction()");
  4. }
To answer your second question, the visual studio text editor for some reason recognizes that as a link, so it underlines it, if you press ctrl+click, itll try to browse to it, nothing important really, so just ignore it.
Oct 19 '07 #6
mpl
27 New Member
so i should move the script from ascx file to the aspx file ?
reggards
melbin


when i explained the whole "issue" paragraph i forgot to mention that if you do the "workaround" thing, there is no need to add the attribute in the html, since you already are doing that server side.

one more thing, remember that since a web user control, is just a chunk of html, any javascript should be in the parent form using that user control.

so the parent aspx would look like
HTML:
Expand|Select|Wrap|Line Numbers
  1.    .
  2.    .
  3.    .
  4. <script language="javascript">
  5. <!--
  6. function whateverFunction()
  7. {
  8.     alert('Hello World!')
  9. }
  10. //-->
  11. </script>
  12.     </HEAD>
  13.     <body MS_POSITIONING="GridLayout">
  14.         <form id="Form1" method="post" runat="server">
  15.             <uc1:WebUserControl1 id="WebUserControl11" runat="server"></uc1:WebUserControl1>
  16.         </form>
  17.     </body>
  18. </HTML>
the user control aspx:
HTML:
Expand|Select|Wrap|Line Numbers
  1. <asp:LinkButton id="LinkButton1" runat="server">LinkButton</asp:LinkButton>
you can see there are no modifications on the controls html tag

now the user controls code behind
c#:
Expand|Select|Wrap|Line Numbers
  1. private void Page_Load(object sender, System.EventArgs e)
  2. {
  3.          this.LinkButton1.Attributes.Add("onclick", "javascript:whateverFunction()");
  4. }
To answer your second question, the visual studio text editor for some reason recognizes that as a link, so it underlines it, if you press ctrl+click, itll try to browse to it, nothing important really, so just ignore it.
Oct 21 '07 #7
mikeyeli
63 New Member
so i should move the script from ascx file to the aspx file ?
reggards
melbin
yes.

try the example i posted before...
Oct 22 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

1
3849
by: Matthew Nace | last post by:
Is there any limitations in using javascript in .ascx files (user controls) I got it working just fine but some of the properties for objects, ie form.item property isn't coming up. In .aspx files...
3
2815
by: Peter | last post by:
Hello, We are inserting a side menu to our application using a class that is writing HTML on all our pages. This is a part of the code as an example: writer.Write(" <table WIDTH=""100%""...
4
5206
by: E | last post by:
I am having trouble with setTimeout working on a second call to the setTimeout function from a second page which is an html page. Here is the scenario. I have a web page and onload it calls a...
0
979
by: rockdale | last post by:
I have an aspx file and a placeholder to dynamic load user defined webcontrols (ascx) in one one my user defined webcontrols, I need to call functions in my js file. I put the following line in...
0
1843
by: acadam | last post by:
Hi, I have an userControl .ascx with a gridView control. One of the columns is a TemplateField with an HyperLink control. On the Row_DataBound event, I am checking that one of the other columns...
2
9127
by: Altman | last post by:
I have created an ascx control and I am calling registerclientscriptblock. The path to the js file is relative to the aspx page and not the ascx file. The function runs fine in IE7 but in...
0
1579
by: Brett Wesoloski | last post by:
I am trying to put javascript in an .ascx file. Although it seems to work no compile errors the button does nothing. This is what I have. In my grid a template column. Then above in the .ascx...
0
1231
by: mpl | last post by:
Hi, i have an ascx file haivng code like <asp:LinkButton ID="col" runat="server" Text='<%# Eval("8Y") %>' OnClick="ColSelected" /> now i am using code behind implmentation for ColSelected.I...
2
3003
by: mpl | last post by:
Hi, i have an ascx file named mytestcontrol.ascx. it has reference to another ascx control as below. <%@ Register Src="myControl.ascx" TagName="myControl" TagPrefix="uc" %> .... .........
0
6964
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7330
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...
0
5434
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4865
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4559
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1378
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
262
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.