1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

With ViewState False DropDownList SelectedIndexChanged not working properly

Discussion in 'C#' started by webcosmo, Jul 9, 2007.

  1. #1
    I have a ASP .Net 2 web form with EnableViewState=false for the form. Inside the form I have a dropdownlist for states dynamically populated from the OnInit. ddl is populated fine and remembering states ok. But the OnSelectedIndexChanged is not working properly. This event is fired with all but the first item.
    Thanks in advance.

    on the aspx page:
    ---------------
    <asp:DropDownList ID="ddlState" OnSelectedIndexChanged="ddlState_SelectedIndexChanged" runat="server" AutoPostBack="true">
    </asp:DropDownList>
    On Code Behind:
    ---------------------
    protected override void OnInit(EventArgs e)
    {
    connectionString = "***********";
    String commandString = "Select Top 5 LocationId,Location from Locations Where Parent='0' AND Deleted='N' Order By Location ";
    DataTable dataTable = ExecuteQuery(commandString);

    ddlState.Items.Clear();
    ddlState.Items.Add(new ListItem("state/province", "0000"));
    foreach (DataRow myRow in dataTable.Rows)
    {
    ddlState.Items.Add(new ListItem(myRow["Location"].ToString(), myRow["LocationId"].ToString()));
    }

    base.OnInit(e); //to not completely override the base.OnInit method
    }

    protected void ddlState_SelectedIndexChanged(object sender, System.EventArgs e)
    {
    //FOR TEST
    Response.Write("<br>ddlState.SelectedItem.Value: " + ddlState.SelectedIndex);
    }
     
    webcosmo, Jul 9, 2007 IP
  2. TasteOfPower

    TasteOfPower Peon

    Messages:
    572
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    what do you mean "with all but the first item?"
     
    TasteOfPower, Jul 10, 2007 IP
  3. webcosmo

    webcosmo Notable Member

    Messages:
    5,840
    Likes Received:
    153
    Best Answers:
    2
    Trophy Points:
    255
    #3
    Lets say a dropdown is dynamically populated with 3 items. eg.
    <asp:dropdownlist id="ddl" OnSelectedIndexChanged="xxx" runat="server">
    <asp:listitem>a</asp:listitem>
    <asp:listitem>b</asp:listitem>
    <asp:listitem>c</asp:listitem>
    </asp:dropdownlist>

    The OnSelectedIndexChanged event will fire for b and c not a.
     
    webcosmo, Jul 10, 2007 IP
  4. MasterOfLogic

    MasterOfLogic Well-Known Member

    Messages:
    217
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    103
    #4
    the selected index on load is 0.

    I usually put something like "Make Selection" in my index 0.

    If your first state is: "AL" and it's located in index 0, it will not autopostback or selectedindexchanged becuase the index has not changed. it loads at index 0. so, by adding something like "make selection" before your first state, whenever you postback or indexchanged it will perform your action.

    of course you'll have to add to your code:

    if(DDL.SelectedIndex == 0)

    to not allow an indexchanged or postback if they happen to rechoose index 0 after they change to another index.

    put your state AL in index 1 location. PM me if ya need more info!

    Cheers!

    ps. to add another listitem to your DDL after you bind your data do this:

    DDL.items.insert(0, "Make Selection)
     
    MasterOfLogic, Jul 12, 2007 IP
  5. webcosmo

    webcosmo Notable Member

    Messages:
    5,840
    Likes Received:
    153
    Best Answers:
    2
    Trophy Points:
    255
    #5
    Yes you right. I saw that also. Thanks.
     
    webcosmo, Jul 14, 2007 IP