Dropdownlist not reset on reload page

Hey ... a very simple question

I usually write php, but maybe program using the ASP.NET 3.0 platform and cannot figure out how to fix it.

I have a DropdownList, and if I select some value ... my code behind does a bunch of things and outputs data ...

Now ... if I reload the page (i.e. press F5 or a slight reload in the browser) .... all my data is reset, as if my selectedIndex is 0 .... but the actual value display is the previous one that I selected ! ... I'm trying to reset the index in my code behind in the expression "if ispostback = false" ... but nothing works ... he likes the value being cached and can't change anything ...

... any help would be appreciated :)

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

Andrew

+5
source share
6 answers

This is normal behavior for form fields. Browsers usually try to remember the contents of text fields, the state of checkboxes and highlighted items in drop-down lists above events, such as page reload and back / forward.

script, , HTML. script DOM .

/, form.reset() script , HTML. .

+5

, f5, . , - , . , . , f5 , , if (IsPostBack == false) , .

, f5 .

, , . , / , viewstate.

+3
+1

? Control + F5 , .

0

, , . , , . , , reset, . , - , . .

enter code

`<% @Page Language = "#" 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 = "- -" > </: ListItem >           < asp: ListItem = "ALABAMA" > </: ListItem >

    < /asp:DropDownList>

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

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

System;

System.Data;

System.Configuration;

System.Collections;

System.Web;

System.Web.Security;

System.Web.UI;

System.Web.UI.WebControls;

System.Web.UI.WebControls.WebParts;

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;

}

}

0

parrkid , :

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

... , , asp.net, IMHO:-P

0

All Articles