PC Review


Reply
Thread Tools Rate Thread

Adding Javascript to control in c# 2.0

 
 
tshad
Guest
Posts: n/a
 
      4th Mar 2008
I used to do all my coding in single page DreamWeaver and am now using VS
2005.

I used to add onclick events to linkbuttons like so:

lnkFullName.Attributes.Add("onclick", "findit");

<asp:LinkButton ID="lnkFullName" runat="server"
Text='<%# Bind("DisplayedName") %>'
CommandArgument='<%# Bind("DisplayedName") %>'
CommandName="view" CssClass="name" />

This is also in a Repeater.

The error I get is:

The name 'lnkFullName' does not exist in the current context

How do attach the Javascript function to these linkbuttons?

Thanks,

Tom


 
Reply With Quote
 
 
 
 
Cowboy \(Gregory A. Beamer\)
Guest
Posts: n/a
 
      4th Mar 2008
Most likely missing a code declartion in the code behind. Did you place this
on the page in VS, which should solve that for you, or is this some of your
old code you are replacing?

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

*************************************************
| Think outside the box!
|
*************************************************
"tshad" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
>I used to do all my coding in single page DreamWeaver and am now using VS
>2005.
>
> I used to add onclick events to linkbuttons like so:
>
> lnkFullName.Attributes.Add("onclick", "findit");
>
> <asp:LinkButton ID="lnkFullName" runat="server"
> Text='<%# Bind("DisplayedName") %>'
> CommandArgument='<%# Bind("DisplayedName") %>'
> CommandName="view" CssClass="name" />
>
> This is also in a Repeater.
>
> The error I get is:
>
> The name 'lnkFullName' does not exist in the current context
>
> How do attach the Javascript function to these linkbuttons?
>
> Thanks,
>
> Tom
>



 
Reply With Quote
 
bruce barker
Guest
Posts: n/a
 
      4th Mar 2008
when you define a control like:

<asp:linkbutton id="link1" runat="server">

in the aspx class, as class variable named linlk1 is defined, and in the
init code somethink like:

link1 = new LinkButton();
link1.ID = "link1";
...

is generated. when the control is in a repeater, it can not create a
vraiable for the control, because there are 0..n controls. also the control
are created at databind, not during init. the control are children of the
repeator, not the page.

the easiest way to access the controls in a repeater is to use the
OnDataBound event, and use FindControl to access the control. see any
databind examples.


-- bruce (sqlwork.com)


"tshad" wrote:

> I used to do all my coding in single page DreamWeaver and am now using VS
> 2005.
>
> I used to add onclick events to linkbuttons like so:
>
> lnkFullName.Attributes.Add("onclick", "findit");
>
> <asp:LinkButton ID="lnkFullName" runat="server"
> Text='<%# Bind("DisplayedName") %>'
> CommandArgument='<%# Bind("DisplayedName") %>'
> CommandName="view" CssClass="name" />
>
> This is also in a Repeater.
>
> The error I get is:
>
> The name 'lnkFullName' does not exist in the current context
>
> How do attach the Javascript function to these linkbuttons?
>
> Thanks,
>
> Tom
>
>
>

 
Reply With Quote
 
tshad
Guest
Posts: n/a
 
      4th Mar 2008

"bruce barker" <(E-Mail Removed)> wrote in message
news:CF68F1A6-A18D-434D-88AB-(E-Mail Removed)...
> when you define a control like:
>
> <asp:linkbutton id="link1" runat="server">
>
> in the aspx class, as class variable named linlk1 is defined, and in the
> init code somethink like:
>
> link1 = new LinkButton();
> link1.ID = "link1";
> ...
>
> is generated. when the control is in a repeater, it can not create a
> vraiable for the control, because there are 0..n controls. also the
> control
> are created at databind, not during init. the control are children of the
> repeator, not the page.
>
> the easiest way to access the controls in a repeater is to use the
> OnDataBound event, and use FindControl to access the control. see any
> databind examples.
>

That was exactly what I had to do. I needed to do a FindConrol then do an
".OnClientClick".

I guess you don't nee the Attributes.Add anymore.

Thanks,

Tom
>
> -- bruce (sqlwork.com)
>
>
> "tshad" wrote:
>
>> I used to do all my coding in single page DreamWeaver and am now using VS
>> 2005.
>>
>> I used to add onclick events to linkbuttons like so:
>>
>> lnkFullName.Attributes.Add("onclick", "findit");
>>
>> <asp:LinkButton ID="lnkFullName" runat="server"
>> Text='<%# Bind("DisplayedName") %>'
>> CommandArgument='<%# Bind("DisplayedName") %>'
>> CommandName="view" CssClass="name" />
>>
>> This is also in a Repeater.
>>
>> The error I get is:
>>
>> The name 'lnkFullName' does not exist in the current context
>>
>> How do attach the Javascript function to these linkbuttons?
>>
>> Thanks,
>>
>> Tom
>>
>>
>>



 
Reply With Quote
 
Mark Rae [MVP]
Guest
Posts: n/a
 
      4th Mar 2008
"tshad" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> That was exactly what I had to do. I needed to do a FindConrol then do an
> ".OnClientClick".
>
> I guess you don't need the Attributes.Add any more.



You do for all webcontrols which don't have an OnClientClick property, which
is really only available to Button controls, and to controls which inherit
from Button controls e.g. ImageButton, LinkButton etc. It doesn't exist for
HyperLink controls, etc...


--
Mark Rae
ASP.NET MVP
http://www.markrae.net

 
Reply With Quote
 
tshad
Guest
Posts: n/a
 
      4th Mar 2008

"Mark Rae [MVP]" <(E-Mail Removed)> wrote in message
news:uONrB$(E-Mail Removed)...
> "tshad" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>> That was exactly what I had to do. I needed to do a FindConrol then do
>> an ".OnClientClick".
>>
>> I guess you don't need the Attributes.Add any more.

>
>
> You do for all webcontrols which don't have an OnClientClick property,
> which is really only available to Button controls, and to controls which
> inherit from Button controls e.g. ImageButton, LinkButton etc. It doesn't
> exist for HyperLink controls, etc...
>

I see.

Thanks,

Tom
>
> --
> Mark Rae
> ASP.NET MVP
> http://www.markrae.net



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Issue with Control.Attributes Property when adding Javascript ChrisMiddle10@gmail.com Microsoft ASP .NET 6 12th Oct 2006 12:26 AM
Issue with Control.Attributes Property when adding Javascript ChrisMiddle10@gmail.com Microsoft ASP .NET 0 12th Oct 2006 12:02 AM
Adding Some Javascript to an ASP:IMAGE control xanthviper@xanthviper.com Microsoft ASP .NET 1 26th Sep 2005 06:34 PM
adding javascript to html control button Matt Tapia Microsoft ASP .NET 2 15th Oct 2004 08:51 AM
Adding javascript event to datagrid control Sjaakie Helderhorst Microsoft ASP .NET 19 26th Jul 2004 05:10 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:40 AM.