Hi DJ,
The following code will set the selectedIndex property to -1 on the client
for a DropDownList server control named, "DropDownLi st1" so that no item is
selected upon the initial rendering of the page and across post-backs until
the user manually selects an item from the list. The code may be used as-is
on a Page or UserControl and is browser-independent according to the W3C
standards. I've tested the code on IE 6 and Mozilla FireFox 1.5.0.3.
The code requires the Microsoft .NET Framework 2.0 and a Page or UserControl
that contains a DropDownList control named, "DropDownList1" . To make this
code compatible with an earlier version of the framework hard-code the name
of the form in place of the 'Page.Form.Name ' code within the client-side
script and remove '.ClientScript' from 'Page.ClientScr ipt' (also make sure
that the appropriate parameters are passed to the RegisterStartup Script
method since I can't remember if the signatures differ between framework
versions).
If you require this functionality on multiple drop-downs per page then I
suggest taking my code and encapsulating it within a derived-DropDownList
implementation of your own.
/// <summary>
/// Indicates across post-backs whether the user has selected a value from
<see cref="DropDownL ist1" />.
/// </summary>
private bool DropDownList1Se lectedIndexChan ged
{
get
{
return ((bool?)
ViewState["DropDownList1S electedIndexCha nged"]).GetValueOrDef ault(false);
}
set
{
ViewState["DropDownList1S electedIndexCha nged"] = value;
}
}
protected void Page_Load(objec t sender, EventArgs e)
{
DropDownList1.S electedIndexCha nged += new
EventHandler(Dr opDownList1_Sel ectedIndexChang ed);
if (!Page.IsPostBa ck)
{
DataTable table = new DataTable();
table.Columns.A dd("ID", typeof(int)).Au toIncrement = true;
table.Columns.A dd("Name", typeof(string)) ;
table.Rows.Add( 1, "Dave");
table.Rows.Add( 2, "Steve");
table.Rows.Add( 3, "Phil");
DropDownList1.D ataValueField = "ID";
DropDownList1.D ataTextField = "Name";
DropDownList1.D ataSource = table;
DropDownList1.D ataBind();
}
}
private void DropDownList1_S electedIndexCha nged(object sender, EventArgs e)
{
// from now on we won't render the script that
// resets the selectedIndex to -1 on the client
// since the user selected a value from the list
DropDownList1Se lectedIndexChan ged = true;
}
protected override void OnPreRender(Eve ntArgs e)
{
Response.Write( "<div>User has selected a value: " +
DropDownList1Se lectedIndexChan ged.ToString() + "</div>");
if (!DropDownList1 SelectedIndexCh anged)
// The user has not selected a value, so register
// a script that executes before the page finishes
// loading to set selectedIndex = -1
{
string script =
"var theForm = document.forms['" + Page.Form.Name + @"'];
if (!theForm) {
theForm = document.form1;
}
theForm." + DropDownList1.C lientID + ".selectedI ndex = -1;";
Page.ClientScri pt.RegisterStar tupScript(typeo f(DropDownList) ,
"InitDropDownLi st1", script, true);
}
base.OnPreRende r(e);
}
- Dave Sexton
"DJ" <re***@cox.netw rote in message
news:OH******** ******@TK2MSFTN GP04.phx.gbl...
>I have a DropDownList in Visual Web Developer that is databound to a SQL
Database. Whenever the web page is opened the DropDownList is automatically
filled with the first item in the table.
My question is, how do I set it so that the DropDownList is empty until a
value is selected?