views:

3326

answers:

5

Hey,...very simple question

I usually write php but happen to be programming with ASP.NET 3.0 framework now and can't figure out how to fix this.

I have a DropdownList and if I selected a some value,...my code behind does a bunch of stuff and outputs the data......

Now,...if I reload the page (ie. press F5 or the little reload thing on the browser)....all my data resets as if my selectedIndex is 0....but the actual showing Value is the previous one I selected!...i try to reset the index in my code behind in the "if ispostback = false" statement...but nothing works......it's like the Value is cached and nothing I can do changes it...

...any help would be appreciated :)

(further explanation: ....if print to screen a mydropdownlist.selectedIndex from my Page load Sub...It will return a 0....however the selected index on the screen is clearly not the 0 one....I understand WHY this is happening...i just need to stop it....or at least have a way of determining the index in the view state...)

Andrew

A: 

Did you try clearing the cache in your browser? Most browsers support Control+F5 as a hard-refresh that will clear the cache for the current page and reload the entire page from the server.

Andrew Hare
+3  A: 

This is normal behaviour for form fields. Browsers generally try to remember the contents of text fields, the states of checkboxes and the selected items in dropdowns over events like page reload and back/forwards.

For this reason you should not assume during script initialisation that the values of your form fields will match the content you served up in the HTML. Have the script sniff their current values and set its variables and DOM state up accordingly when the page loads.

If you really want to throw away all user form field changes on reload/navigation, call form.reset() in the script initialisation to return it to the HTML state. But this can be quite user-unfriendly in normal circumstances.

bobince
The problem is when I try to retried dropdownlist.SelectedIndex from my dropdownlist in my page load it THINKS it's 0....not what is it actually showing. ...do you mean call form.reset() from javasript or code behind? ...(do I have to use a form?)
Andrew
retried = retrieve
Andrew
From JavaScript. If you access SelectedIndex from codebehind, you are changing the HTML before it gets as far as the browser; you have no input into what the browser does after getting the HTML.
bobince
oh, thanks, good to know...if you dont' mind me asking how would i do this: "Have the script sniff their current values and set its variables and DOM state up accordingly when the page loads."
Andrew
Well, just that if you've got any client-side JavaScript stuff that has state, it shouldn't assume that the initial state is what you served to it via HTML. If you're not using client-side script you don't have to worry about it.
bobince
my contractor is using a masterpage...and thus i needed to use a document.<%=form1.CLientID %>.reset() ....but taht worked perfectly,..thanks
Andrew
A: 

Try disabling veiwstate on the checkbox if you don't need it.

OJ
tried...the entire contents of my listbox dissapears lol
Andrew
Yes, that's expected. You have to repopulate your list each time.
OJ
ya,...but the involves another database query in my particular case,..but it's a good option to know, thanks.
Andrew
A: 

It sounds like when you hit f5 you are NOT doing a new page reload. You say that when you select something from your dropdownlist that you do a bunch code behind stuff. Well that action IS a postback. So hitting f5 AFTER that action would not be considered a new call to the page so the the if (IsPostBack == false) would fail because it is a postback.

Your first action caused it to be a postback so hitting f5 after that, will be a postback.

It sounds like you may have some viewstate issues as well since your other controls are resetting. Although if you are doing the hide / shows with client side scripting, they will not be persisted through the viewstate anyways.

Kelsey
A: 

Greetings, I too am trying to clear a drop-down list once a value from the other drop-down is selected. When the user goes to submit the form, there should be only one drop-down which has a value. I have started the code as shown below but the drop-down will not reset itself when a value from the other drop-down is selected. I'm sure I'm missing something very simple or my naming is off. Thanks.

enter code

`<%@ Page Language="C#" AutoEventWireup="true" CodeFile="homepagestate.aspx.cs" inherits="_homepagestate"%>

< form id="form1" runat="server">

< asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="sel"> < asp:ListItem Value="-Select one or more states-">< /asp:ListItem> < asp:ListItem Value="ALABAMA">< /asp:ListItem>

    < /asp:DropDownList>

< asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="true"> < asp:ListItem Value="-Select One-">< /asp:ListItem> < /asp:DropDownList>

      < asp:Button ID="Button1" runat="server" Text="Submit" /></div>
</form>

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

public partial class _homepagestate : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

    if ( !IsPostBack )

    {

       // sel();

        DropDownList2.SelectedIndex = 0;

    }

}

protected void sel(object sender, EventArgs e)

{

    Response.Write(DropDownList2.SelectedIndex);

    //DropDownList2.selecteditem = 0;

}

}

'