Databinding a drop down list, hooking into the SelectedIndexChanged and avoiding the viewstate

Discussion in 'ASP .Net' started by Dave A, Nov 29, 2005.

  1. Dave A

    Dave A Guest

    I am stuggling with databinding a drop down list, hooking into the SelectedIndexChanged and attempting to avoid using the viewstate.

    The drop down list is quite large so I would prefer to avoid using the view state so I set EnableViewState on the page to false. To enable the drop down list to bind to a datasource I bind it during the OnInit.

    I do a response.Write during the SelectedIndexChanged event to verify that the event fires.

    It all works EXCEPT when the first item of the drop down list is reselected after another item is selected. The event just does not fire.

    Does this mean that if you want to hook into SelectedIndexChanged reliably you must use the ViewState?

    Regards
    Dave A


    <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication1.WebForm1" enableViewState="False"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>WebForm1</title>
    <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" Content="C#">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body MS_POSITIONING="GridLayout">
    <form id="Form1" method="post" runat="server">
    <asp:DropDownList id="DropDownList1" style="Z-INDEX: 101; LEFT: 96px; POSITION: absolute; TOP: 72px"
    runat="server" Width="280px" AutoPostBack="True"></asp:DropDownList>
    </form>
    </body>
    </HTML>

    ----

    using System;
    using System.Collections;
    using System.Collections.Specialized;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;

    namespace WebApplication1
    {
    /// <summary>
    /// Summary description for WebForm1.
    /// </summary>
    public class WebForm1 : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.DropDownList DropDownList1;
    NameValueCollection list = new NameValueCollection();

    protected WebForm1()
    {
    list.Add("Do not filter","");
    list.Add("abc","1");
    list.Add("def","2");
    list.Add("ghi","3");
    list.Add("jkl","4");
    }


    private void Page_Load(object sender, System.EventArgs e)
    {

    }

    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    DropDownList1.DataSource = list;
    DropDownList1.DataBind();

    //
    // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    this.DropDownList1.SelectedIndexChanged += new System.EventHandler(this.DropDownList1_SelectedIndexChanged);
    this.Load += new System.EventHandler(this.Page_Load);

    }
    #endregion

    private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
    {
    Response.Write("Drop down list fired:" + DropDownList1.SelectedValue);
    }
    }
    }
     
    Dave A, Nov 29, 2005
    #1
    1. Advertising

  2. Dave A

    Nosaj Guest

    You maybe should define DropDownList1.Datamember.

    I am also struggling with databinding and dropdownlist: It works in a
    aspx page but not in an ascx component, do you know why?
     
    Nosaj, Nov 29, 2005
    #2
    1. Advertising

  3. Dave A

    Dave A Guest

    If you could provide a cut down example I would love to take a look. This
    stuff is all 'too easy' when you read it in the help or in a news group or
    in a book but when you are faced with real-world problems the wheels seem to
    fall off.

    "Nosaj" <> wrote in message
    news:...
    > You maybe should define DropDownList1.Datamember.
    >
    > I am also struggling with databinding and dropdownlist: It works in a
    > aspx page but not in an ascx component, do you know why?
    >
     
    Dave A, Nov 29, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. weiwei
    Replies:
    0
    Views:
    1,047
    weiwei
    Jan 5, 2007
  2. Replies:
    0
    Views:
    846
  3. jbrune
    Replies:
    0
    Views:
    444
    jbrune
    Jul 1, 2008
  4. akhzar
    Replies:
    1
    Views:
    747
    akhzar
    Mar 6, 2009
  5. Replies:
    5
    Views:
    295
Loading...

Share This Page