Question About .NET Controls and Javascript

J

Jonathan Wood

I have a TextBox on a Web form that I'll be accessing from code. However,
I'd also like execute some Javascript when the control loses focus.

I'm a little confused since the IDE does not seem to present javascript
events when a control has the runat="server" specified. I know I can type it
in but that wouldn't that be specifying my server-side handler rather than
client script?

I'm pretty sure I need this to be a server-side control but would like to
connect some client-side script. Is this even possible?

Thanks.
 
D

Dina

I have a TextBox on a Web form that I'll be accessing from code. However,
I'd also like execute some Javascript when the control loses focus.

I'm a little confused since the IDE does not seem to present javascript
events when a control has the runat="server" specified. I know I can type it
in but that wouldn't that be specifying my server-side handler rather than
client script?

I'm pretty sure I need this to be a server-side control but would like to
connect some client-side script. Is this even possible?

Thanks.

You 'll have to add a Attributes.Add("onclick", "javascript:function")
on the server side to associate a client event handler to a server
control. {Just check on the syntax coz I've not worked on Web apps 4
sometime.}

Hope it helps.
Regards, Dina
 
E

Eliyahu Goldin

Any attribute that is not recognized on server side will just be emitted to
the client as it is. So

<asp:Text ... abc="xyz" /> will produce

<input type="text" abx="xyz" />
 
J

Jonathan Wood

That article looks like just the thing I needed.

However, there's one area that I didn't see it address: How to reference
controls by name. Because if I have a TextBox with ID="txtLbs" it actually
ends up with a name something like
"ctl00_ctl00_ContentPlaceHolder1_MainContent_Wizard1_txtLbs". I need to
reference two controls from the javascript. I had to hard code these names
because they could potentially change. Anyone have any ideas?

Thanks.
 
J

Jonathan Wood

My concern was that it might match one of the ASP.NET control's server-side
events/attributes. But perhaps most of those have different names.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

Eliyahu Goldin said:
Any attribute that is not recognized on server side will just be emitted
to
the client as it is. So

<asp:Text ... abc="xyz" /> will produce

<input type="text" abx="xyz" />

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin


Jonathan Wood said:
I have a TextBox on a Web form that I'll be accessing from code. However,
I'd also like execute some Javascript when the control loses focus.

I'm a little confused since the IDE does not seem to present javascript
events when a control has the runat="server" specified. I know I can type it
in but that wouldn't that be specifying my server-side handler rather
than
client script?

I'm pretty sure I need this to be a server-side control but would like to
connect some client-side script. Is this even possible?

Thanks.
 
J

Jonathan Wood

Heh, I looked at a table of javascript events had didn't relate to onblur
being when a control lost focus. That name only makes sense in an odd sort
of way. Cool.

I also found a good article based on another suggestion. But do you have any
ideas how I would reference other controls from javascript, given that the
IDs of ASP.NET controls can change substantially by the time the HTML is
rendered?

Thanks!
 
J

Jonathan Wood

Looks like I got this working. I used the ClientID property to obtain the
final control ID.

Thanks!
 

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