views:

1967

answers:

6

I have a menu usercontrol called LeftMenu that has a bulletedlist of linkitems. It's on the ascx page as such:

<asp:BulletedList ID="PublisherList" DisplayMode="LinkButton" OnClick="PublisherList_Click" cssClass="Menu" runat="server"></asp:BulletedList>

I databind the list in the page_load under if(!isPostBack)

I'm having an issue on a page that loads the control. When the page first loads, the event handler fires. However, when the page posts back it no longer fires and in IE8, when I'm debugging, I get "Microsoft JScript runtime error: Object expected" in Visual Studio pointing at "doPostBack('LeftMenu$PublisherList','0')." In FF I don't get the error, but nothing happens. I'm **not loading the control dynamically, it's loaded on the aspx page using:

<%@ Register TagPrefix="Standards" TagName="LeftMenu" Src="LeftMenu.ascx" %>

<Standards:LeftMenu ID="LeftMenu" runat="server"/>

Any ideas of where I'm losing the event handler?

I just realized this is happening on another user control I have as well. A text box and a button and I'm using the default button to make sure pressing the enter key uses that button. .Net converts that in the html to:

 <div id="SearchBarInclude_SearchBar" onkeypress="javascript:return WebForm_FireDefaultButton(event, 'SearchBarInclude_QuickSearchButton')">

so as soon as i enter a key in the box I get a javascript error at the line saying "object expected." It seems like the two issues are related.

Edit Again: I think I need to clarify. It's not that I'm clicking on the menu item and it can't find the selected item on postback. I have this search page with the left navigation on it and then the main content of the page is something that causes a postback. Everything is fine with this postback. Once that page has been posted back, now if I click on the bulleted list in the left navigation I get a javascript error and it fails. The page_init for the LeftMenu control is never called.

A: 

It sounds like you might be losing the click because you are not DataBinding the list on PostBack. Therefore, the post back is trying to refer to a control (a specific bulleted list item) that does not exist.

You should try binding the list again on PostBack just to see if that fixes your issue. BUT, what should REALLY happen is that the LeftMenu and the BulletedList should store their information into ViewState so that you can ensure that the data that was shown to the user on their initial page load is the same data that the PostBack is processing and working with.

Tony Heupel
What if the menu and list contain 100 or 1000 items? Should they still be stored in the view state?
Phaedrus
Ideally, ViewState dies a horrible death and never comes back so that these mysteries of PostBacks never rear their ugly heads and our pages stay light and airy ;-) That being said, this is a left-nav menu on a web site, so I doubt there is more than a few items. Obviously, if this was some kind of database call, you would want to cache it and that may help with keeping the page call light and not require ViewState, but you aren't guaranteed that the list that the PostBack works on is the same as the original one the user saw (if the Cache expires).
Tony Heupel
Removing it from if(!isPostBack){...} doesn't help. The data will rarely be changing, should I still be storing it in the viewstate?
Ashley
A: 

If you have EnableViewState=true for your UserControl and all controls within it, everything should work fine. With ViewState enabled, ASP will reinflate your controls from ViewState after Init has fired. This means that the postback event arg (which points to an index in your control list) will still find the control in that list position. Otherwise the list is empty on postback.

However, ViewState is the work of the devil and was designed simply to foster the illusion that you are working in a stateful environment. It is okay to use it for small amounts of data but typically not advisable for templated controls like repeaters and lists because you have no idea how much data is going to be created in ViewState.

If you are dealing with static, or relatively static data, store it in the application cache and rebind your lists in Page.Init every time (note that it has to be in Init because post-init is when ASP rebinds from ViewState; if you get in there first, your data will be used instead).

If you are dealing with volatile data, you have a problem because the data you rebind must be exactly the same as the original page request, otherwise the postback events will be firing against the wrong rows. In that case you need to either store your initial data in Session or you simply store the list of rows ids (in a hidden variable or Session) and you recreate the data to bind against from the ids each time.

An even better solution is to not use postback events at all. Try to turn all your events into GETs that have an ID on the query string. You can still create the list using binding the first time through the page (as you are currently doing), and you can even GET the same page with a new ID.

If you need to keep state on the same page but need to respond to the user changing a radio button selection (or something else), think about using Ajax calls to update the screen. You also do that with an ID that you pass to the Ajax call.

In general, the more you move from using stateful ASP, the lighter and more responsive your pages will become. You will also be in a better position to move to stateless MVC if necessary. You will also save lots of time lost to debugging obscure problems because ViewState is not available when you need it to be.

The best analysis of ViewState I've read is in the link below. If you fully understand how it works, you can continue to use it without necessarily incurring the costs.

http://weblogs.asp.net/infinitiesloop/archive/2006/08/03/truly-understanding-viewstate.aspx

Rob Kent
Sorry I guess I didn't explain myself well enough. It's not that I can't find the selected item on the postback, that works most of the time. The issue is when I have this control on a page that posts back to itself and then try and click on an item in the list. I get a javascript error "Object Expected" and it just doesn't work
Ashley
A: 

It's possible that this might be javascript related, and that a script that is loading earlier in the page is throwing an error and causing the page to not be loaded properly.

Are your usercontrols loading any javascript onto the page? Can you check for javascript errors on the initial load of the page?

womp
A: 

I moved the code into an existing project we have and for some strange reason, I stopped getting the javascript errors and instead got:

"Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page.

For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation."

I haven't quite figured out where I'm supposed to put the register event validation with a user control, but in the mean time I just set enableeventvalidation=false and it seems to work now.

Ashley
Are you running this on your local machine or on a dev/test cluster of machines? It could be that if you are hopping machines that the machine keys are not set up properly to match each other. If hitting this locally on your machine, then nevermind :-)
Tony Heupel
A: 

It looks like the doPostBack function is missing since its arguments are literals so they couldn't be the cause. Is that one of your own functions or did you mean to call the ASP __doPostBack function?

Have a look at the Firefox error console or allow script debugging in IE and see exactly what object can't be found. Even better, download Firebug and debug it.

Rob Kent
A: 

I had a similar issue. It turned out that Akamai was modifying the user-agent string because an setting was being applied that was not needed.

This meant that some .NET controls did not render __doPostBack code properly. This issue has been blogged here.

DotNetDevster