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

Question about Databound DropDownList in VWD.

P: n/a
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?
Jul 15 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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(Dro pDownList),
"InitDropDownList1", script, true);
}

base.OnPreRender(e);
}

- Dave Sexton

"DJ" <re***@cox.netwrote in message
news:OH**************@TK2MSFTNGP04.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?

Jul 19 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.