Adding Javascript to control in c# 2.0

T

tshad

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
 
C

Cowboy \(Gregory A. Beamer\)

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!
|
*************************************************
 
B

bruce barker

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)
 
T

tshad

bruce barker said:
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
 
M

Mark Rae [MVP]

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...
 
T

tshad

Mark Rae said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top