By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,726 Members | 2,465 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,726 IT Pros & Developers. It's quick & easy.

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

P: n/a
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_SelectedInd exChanged);
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);
}
}
}

Nov 29 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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?

Nov 29 '05 #2

P: n/a
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" <ni*************@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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?

Nov 29 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.