Question about Databound DropDownList in VWD.

D

DJ

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?
 
D

Dave Sexton

Hi DJ,

The following code will set the selectedIndex property to -1 on the client
for a DropDownList server control named, "DropDownList1" 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.ClientScript' (also make sure
that the appropriate parameters are passed to the RegisterStartupScript
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="DropDownList1" />.
/// </summary>
private bool DropDownList1SelectedIndexChanged
{
get
{
return ((bool?)
ViewState["DropDownList1SelectedIndexChanged"]).GetValueOrDefault(false);
}
set
{
ViewState["DropDownList1SelectedIndexChanged"] = value;
}
}

protected void Page_Load(object sender, EventArgs e)
{
DropDownList1.SelectedIndexChanged += new
EventHandler(DropDownList1_SelectedIndexChanged);

if (!Page.IsPostBack)
{
DataTable table = new DataTable();
table.Columns.Add("ID", typeof(int)).AutoIncrement = true;
table.Columns.Add("Name", typeof(string));

table.Rows.Add(1, "Dave");
table.Rows.Add(2, "Steve");
table.Rows.Add(3, "Phil");

DropDownList1.DataValueField = "ID";
DropDownList1.DataTextField = "Name";
DropDownList1.DataSource = table;

DropDownList1.DataBind();
}
}

private void DropDownList1_SelectedIndexChanged(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
DropDownList1SelectedIndexChanged = true;
}

protected override void OnPreRender(EventArgs e)
{
Response.Write("<div>User has selected a value: " +
DropDownList1SelectedIndexChanged.ToString() + "</div>");

if (!DropDownList1SelectedIndexChanged)
// 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.ClientID + ".selectedIndex = -1;";

Page.ClientScript.RegisterStartupScript(typeof(DropDownList),
"InitDropDownList1", script, true);
}

base.OnPreRender(e);
}

- Dave Sexton
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top