I have a web page with a drop-down menu. One item of the menu is "Products", which drops-down with 2 sub-items (Suppliers and Parameters), each of these drop-down to show further menu items (i.e. a list of suppliers and a list of parameters respectively). When I select an item from that 3rd level I get redirected to catalogue.aspx, with the supplier or parameter passed as a query string. The menu is written in JQuery, and the links as standard html hyperlinks which just point to catalogue.aspx.
In catalogue.aspx, a list of products are retrieved for the selected supplier or parameter. At the top of the Page_Load method I've put a break point (on the first line of Page_Load). When I first load the catalogue, my breakpoint is hit, and the list of products are retrieved and displayed. However, if I just hit refresh, or try to then view a different supplier, the breakpoint doesn't get hit, and the same products are displayed again. If I look at the URL, the newly selected supplier is shown in the querystring. Select another supplier, still no breakpoint hit, and still the original products are shown. After a while, and several changes of supplier and parameter, the breakpoint finally gets hit and the correct products are returned, but only once and then stops again.
As far as code is concerned, as detailed above all it is is a standard html hyperlink that links to catalogue.aspx (no postbacks), and a breakpoint at the top of the page_load method.
Can anyone advise what might cause this random idiotness of my page?
EDIT: As requested, some code:
The link:
<a href='http://localhost:45745/Website/catalogue/catalogue.aspx?searchby=supplier&searchterm=PMA Service&pid=25&title=Catalogue - PMA Service'>PMA Service</a>
The code-behind:
protected void Page_Load(object sender, EventArgs e)
{
var i = 0;
...
}
The breakpoint is on the line with var i = 0, and I've also put breakpoints in page_load in the master page and the master base page. However neither is being hit so its almost as if the page just isn't debugging, however this issue also occurs on the test site I've put on my server.
I seem to have fixed it now, and it does appear that it was down to caching.
I put the following in the page_load of catalogue.aspx
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Now, every time I choose another supplier or parameter it hits the breakpoints and loads the correct products.
Its a bit odd though because the same code is actually used for several websites, with the domain determining which layout/colour scheme/content is displayed. I haven't had the issue with the 3 other websites that already use this code, it's just when upgrading this particular website to the new code that its broken.
Thanks for all the responses.
Related
We're having a bizarre issue with our checkout process. Lets say that: -
User adds a few products to their basket
Clicks to view basket and then removes these products Then uses the browsers <- back button to
navigate to a previous product page
Then they add another product to
the basket, this causes the layout of the entire site to crumble,
it's as if none of the code to load the menus or product listings is
being fired off e.g.
I think it may be related to us using AJAX on the basket so i tried the suggestion posted here adding a hidden form element to the page which actually resolved the issue in chrome but in firefox the page gets stuck in an infinite loop,
Has anyone encountered similar issues with user navigating back to a previous state? I'm completely lost as to what to try next
Thanks for any help
Well i managed to fix this. The issue was that when the user navigated back via history to a previous product page, it was being treated as a postback and so none of the code to populate the controls on the page was being fired as it was inside a !Page.IsPostBack block...
Since I couldn't find anything better to use to determine when the page was broken I simply read in the navigation tabs and check if any of them are empty which is an indication of the page not loading right and then we refresh e.g.
<script type="text/javascript">
$(function() {
$("#tabs-nav ul li").each(function() {
if (!$(this).children().text() || /^\s*$/.test($(this).children().text())) {
location.reload();
}
});
});
</script>
It's not pretty but it does the job
Thanks
I have page A and page B. You can do the following things in page A:
Do stuff on page A (e.g., choose an item in a list box), which causes an UpdatePanel in page A to be redrawn with additional information.
Move on to page B. This is done with a Button and Response.Redirect.
Now the problem is as follows:
The user does stuff on page A. Page A is now different from its initial state.
The user moves to page B.
The user hits the back button of the browser.
What happens: Page A in its initial state is shown.
What I would like to happen: Page A in its final state is shown (i.e., with the "correct" item selected in the list box).
I know about ScriptManager.AddHistoryPoint! As far as I can see, it does not solve my problem:
I could call AddHistoryPoint every time something is done on page A. This is bad, because it litters the browser history with lots of entries. (A new entry every time a different list box item is selected.) But that's exactly what I want to avoid by using an UpdatePanel! Of course, if there were a ReplaceLastHistoryPoint method, that would be perfect, but I did not find one...
I tried to call AddHistoryPoint right before Response.Redirect, to save only the last state of page A, but, alas, that doesn't work (no history point is saved). This is not surprising, considering how Response.Redirect works.
Is there some solution I have missed? I'm using .NET 3.5SP1, in case it matters.
This is a bit of an old question, but I'll go ahead and provide the mechanism I use for this. The basic idea is that instead of allowing AddHistoryPoint to manage your name-value pairs, just allow it to manage a key to your NameValueCollection that you keep somewhere else, like in your Session cache. Then as subsequent Ajax requests come in, you never make another call to AddHistoryPoint; instead, you just replace your NameValueCollection with the state of the current request.
The only other bit is to keep track of whether you're on your first Ajax call and need to make that first call to AddHistoryPoint or not.
My code looks something like this:
protected void Page_LoadComplete(object sender, EventArgs e) {
if (!ScriptManager.GetCurrent(this).IsNavigating && (IsCallback || IsInAsyncPostback())) {
var state=new NameValueCollection();
//OnCallbackHistory(state); // this gets state for all interested parties
if (state.Count != 0) {
string key=ViewState["HistoryStateKey"] as string; // empty on first AJAX call
if (string.IsNullOrEmpty(key) || ScriptManager.GetCurrent(this).EnableHistory) {
key=CallbackHistoryKeyRoot+Interlocked.Increment(ref callbackHashKey).ToString();
ViewState["HistoryStateKey"]=key;
ScriptManager.GetCurrent(this).AddHistoryPoint("", key);
}
Session[key]=state;
}
}
}
Instead of calling AddHistoryPoint on the server you could call addHistoryPoint on the client using the Sys.Application class, http://msdn.microsoft.com/en-us/library/vstudio/cc488025(v=vs.90).aspx.
So you'd add a client side click listener to the button which would addHistoryPoint on the client before the button does the post back and redirect.
I am trying to Session over my Label.Text in the first .aspx page to another Label in the second .aspx page. I retrieve my value from the database and place them in the first Label.Text and want to Session this Label (Which i got the text from the database) to another page linked to the first page. I use this method is because i have a detailed products page (My first .aspx page) which consists of many products and when user click a particular product, it's product name have to be displayed in the second page of the .aspx . My problem here is that when i Session over the Label's text on the first page, the value does not pass over to another page.
My first page's .cs code (code behind):
Session["productName"] = productName.Text;
Response.Redirect("products2.aspx");
My second page's .cs code (code behind - I place this code in page load)
if (Session["productName"] != null)
productName.Text = Session["productName"].ToString();
Have i gone wrong anywhere?
Inorder to make the session not to be expired for a long time, you should follow two steps.
Keep a continous eye on the Session Timeout.
Redirect the session when its about to expire.
The Base Page for Detecting Sessions will explain you every thing you need to do, please refer it once.
your code is totally right..
but my question is are you using ispostback in your page load??
if not then use it like this..because of postback sometimes you can't get values.
then put your code if is not postback
page_load()
{
if(ispostback)
{
}
else
{
//put your code here
}
}
I encountered some weird behaviour today and I was hoping someone could shed some light on it for me because I'm perplexed.
I have a couple of methods I use to interact with the ui for the sole purpose of displaying error/success/warning messages to the user.
Here is one of them
public static void Confirm(string text)
{
var page = (Page)HttpContext.Current.Handler;
var uiConfirm = new HtmlGenericControl("div")
{
ID = "uiNotify",
InnerHtml = text
};
uiConfirm.Attributes.Add("class", "ui-confirm");
page.Master.FindControl("form1").Controls.AddAt(2, uiConfirm);
}
This works perfectly fine except for one nuance I encountered this morning and I was hoping someone could shed some light on it for me.
I am working on your run of the mill profile editing page. In this page, I am binding a couple of dropdownlists (country, province/state) on page load. I have a submit at the bottom and a click event that fires to update the information, then call the method above to notify the user that their information was successfully updated. This works the first time you click the submit button; the page posts back, the information gets updated in the database, the dynamically added div gets popped in, confirm message is displayed and all is good. However, if you then click the submit button again, it fails stating SelectedItem on the dropdowns I'm binding in the page load is null (Object reference not set to an instance of an object). The dropdown is actually wiped for some reason on the second postback, but not the first.
In sheer desperation after trying everything else, I decided to take out the call to the confirm method... and strangely enough the error disappears and I can update the information on the page as many times as I like.
If I add a generic control statically to the page I'm working on, and change my method slightly so that instead of adding a generic control to the form dynamically it just finds the generic control on the page, that does no produce the same error.
The problem also goes away if I remove the two dropdowns from the page or just stop interacting with them.
Why on earth would adding a dynamic control to the form wipe my dropdowns on postback?
I think you should consider using the PlaceHolder class in your MasterPage, the AddAt(2, uiConfirm) is going to bite you and probably is:
Markup:
.......
<asp:PlaceHolder id="PlaceHolder1"
runat="server"/>
......
Code-behind:
public static void Confirm(string text)
{
var page = (Page)HttpContext.Current.Handler;
var uiConfirm = new HtmlGenericControl("div")
{
ID = "uiNotify",
InnerHtml = text
};
uiConfirm.Attributes.Add("class", "ui-confirm");
//may need to change depending on where you put your placeholder
Control placeHolder = page.Master.FindControl("PlaceHolder1");
placeHolder.Controls.Clear();
placeHolder.Controls.Add(uiConfirm);
}
So I am experiencing an issue with an .aspx page and some server side code, where I am getting unexpected results.
The goal of this page is simple, there are 5 radio buttons and a button with a server side onclick function. The idea is the user picks 1 of the 5 radio buttons, and then clicks the button. Upon clicking the button I verify (not using form validation, because I wanted a different feel) that a button is checked, and then store the selected option in a database.
Due to the fact that the number of radio buttons may change in the future I decided to try and abstract the number of radio buttons to make it easier on my self to change in the future.
So at the top of my server side code I created a list of possible options.
I then have a registerVote function that takes in a RadioButton object, and a number to grab a setting from the config file. I throw those 2 values into a wrapper class, and then add them to the list of possible options.
Finally when the submit button is pressed, I iterate through all possible options to see which one is checked, and grab its associated value.
public partial class VotePanel : System.Web.UI.Page
{
List<VoteOption> voteOptions = new List<VoteOption>();
public string registerVote(RadioButton newRadioButton, int voteOption)
{
voteOptions.Add(new VoteOption(newRadioButton, voteOption));
return ConfigurationManager.AppSettings["vote_option_" + voteOption];
}
protected void Submit_Click(object sender, EventArgs e)
{
//Check vote
string vote_value = "";
bool someButtonChecked = false;
foreach (VoteOption vo in voteOptions)
{
if (!someButtonChecked && vo.button.Checked)
{
vote_value = vo.movie;
someButtonChecked = true;
}
}
//....
}
}
class VoteOption
{
public RadioButton button;
public int vote_value;
public VoteOption(RadioButton r, int v)
{
button = r;
vote_value= v;
}
}
The code I use in page to add a radio button looks like this
<asp:RadioButton ID="RadioButton1" runat="server" GroupName="Vote" style="position: relative; top: 3px;" /><%=registerMovie(RadioButton1,1)%>
Now for the problem I am experiencing. Whenever the submit button is clicked, the list has a count of zero, and looks like it has been reinitialized. I validated that values are being added, by returning the list count in the registerVote method, and objects are indeed being added, but for some reason are not available to the Submit function.
Now variables on a page like this shouldn't reinitialize right? I also tested a string, and it did not reset and was available to the Submit button. What I did was define a class variable string time = DateTime.Now.Ticks.toString(); and displayed that after the submit button was clicked, and the time was always the same reguardless of how many times I clicked it.
So why would my List reinitialize, but not a string? Any ideas?
Keep in mind that your page class will be constructed and destructed for every request - no state will be maintained between each page load, it is up to you to properly recreate state as needed. In this case it appears that your list voteOptions is not being recreated before Submit_Click is called.
You'll have to register all your voting options regardless of whether the page is in a postback or not inside the Page_Load or OnInit handlers of the page. This will reconstruct voteOptions, which will then be accessed when Submit_Click is called.
Take a look at the ASP.NET Page Life Cycle.
The problem seems to be that you are constructing the List<VoteOption> voteOptions at page render then expecting it to still be there on postback. The Page object does not exist past the point that the page is delivered to the browser, so your list of vote options gets disposed of as well when the browser has received the page.
You'll either need to reconstruct the voteOption list before or during Submit_Click on postback, or give yourself enough information in the value of the radio button that you don't need it.
I don't see in your code any place where the list that you are building is placed in memory. I believe you are rebuilding it on each page reload. P.s. might be my reading but you created a function called registerVote and you are calling a method called registerMovie so that might be your problem.
You could place the list in the session and get it back from session.
Personnally I would change the code to
1) Check if the list is in memory and get it. If not in memory call a method to generate it once and then place it in memory.
2) Use a RadioButtonList on your page that you can then bind to your list as a data source.
asp.net is stateless, so every postback (such as clicking Submit) recreates the server-side class. If you want your list to persist between calls, you should save it in ViewState or a Hidden field. Not sure about the string though; what you're describing doesn't fit the asp.net lifecycle.