mpl 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
7 11882 Plater 7,872
Recognized Expert Expert
I believe using onmouseclick= will tell it to point to a javascript function.
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 : - 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#: - col.Attributes.Add("onclick", "javascipt:whateverFunction()");
and it fixes this issue, done it a million times, so i know it works
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: - 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#: - col.Attributes.Add("onclick", "javascipt:whateverFunction()");
and it fixes this issue, done it a million times, so i know it works
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.
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: - .
-
.
-
.
-
<script language="javascript">
-
<!--
-
function whateverFunction()
-
{
-
alert('Hello World!')
-
}
-
//-->
-
</script>
-
</HEAD>
-
<body MS_POSITIONING="GridLayout">
-
<form id="Form1" method="post" runat="server">
-
<uc1:WebUserControl1 id="WebUserControl11" runat="server"></uc1:WebUserControl1>
-
</form>
-
</body>
-
</HTML>
the user control aspx:
HTML: - <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#: - private void Page_Load(object sender, System.EventArgs e)
-
{
-
this.LinkButton1.Attributes.Add("onclick", "javascript:whateverFunction()");
-
}
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.
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: - .
-
.
-
.
-
<script language="javascript">
-
<!--
-
function whateverFunction()
-
{
-
alert('Hello World!')
-
}
-
//-->
-
</script>
-
</HEAD>
-
<body MS_POSITIONING="GridLayout">
-
<form id="Form1" method="post" runat="server">
-
<uc1:WebUserControl1 id="WebUserControl11" runat="server"></uc1:WebUserControl1>
-
</form>
-
</body>
-
</HTML>
the user control aspx:
HTML: - <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#: - private void Page_Load(object sender, System.EventArgs e)
-
{
-
this.LinkButton1.Attributes.Add("onclick", "javascript:whateverFunction()");
-
}
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.
so i should move the script from ascx file to the aspx file ?
reggards
melbin
yes.
try the example i posted before...
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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%""...
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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" %>
....
.........
|
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...
|
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: 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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |