Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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.

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

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.

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;

}

}

'

This is actually from parrkid on a different stackoverflow question, but add this at the end of your page:

<script language="javascript">
  var B01 = document.getElementById('<%=YourDropDownList.ClientID%>');
  B01.selectedIndex = 0;
</script>

Yep...total hack for what's probably an asp.net but IMHO :-P

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top