asp 2.0 menu and javascript

K

Ken Nipper

Does anyone know how to access the menu items from javascript. I can
use the onclick to perform an action but I would like to be
enable/disable items based on page status.

Thanks
Ken
 
K

Kevin Spencer

This would best be done from the CodeBehind. While you can do it with
JavaScript on the client-side, you may get unexpected results on the
server-side.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.
 
K

Ken Nipper

Kevin said:
This would best be done from the CodeBehind. While you can do it with
JavaScript on the client-side, you may get unexpected results on the
server-side.

Would you mind explaining. My goal is to enable/disable menu items
based on page state without causing a postback to the server.
 
K

Kevin Spencer

Hi Ken,
Would you mind explaining.

ASP.Net is built to run in a stateless HTTP environment. The client is
unaware of the server, and the server is unaware of the client. That is the
nature of HTTP, which is really a messaging protocol. The client sends a
Request to a web server for a specific resource (URL). The client Request is
a text message with headers that include a bit of information about the
client (IP address, browser type, etc) and the message itself. The server
receives the Request and processes it, returning the Response, which is
typically an HTML document, or some binary file. The server is unaware of
the client, and only has the single Request message to work with in
preparing the Response.

The ASP.Net object model is designed to emulate state by a rather
sophisticated collection of work-arounds, such as the server posting
JavaScript and hidden form fields containing state information to the
client, so that the client-side, server-generated JavaScript, together with
the form's POST contents (including the hidden form fields, such as
ViewState and Event-related fields) can REMIND the server of what part of
what conversation the client may be continuing (in a PostBack).

So, using JavaScript on the client that is not generated by the server can
have unexpected results, as it may bypass or override the ASP.Net
object-model's mechanisms for passing information back and forth between the
client and server.

That said, I believe you can do what you want to do, but keep this in mind
if, in the future, you see something unexpected as a result of client-side
JavaScript form manipulation. For example, changing the value of form fields
programmatically, may or may not be recognized by the server-side Page
class, although you can usually circumvent this by using the Request.Form
Collection separtely from the built-in event-handling mechanisms.

In this case, you're not modifying anything in any form fields, but just
showing and hiding them. Keep in mind, however, that if the user changes
their mind after filling in a form field, and then hides it, this doesn't
remove the value from the POST. Just some cautionary advice. Measure twice,
cut once!

The following link is to a tutorial on this subject that may be helpful to
you:

http://www.devx.com/webdev/Article/10483/1954?pf=true

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.
 

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

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top