473,545 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question about Databound DropDownList in VWD.

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
1 2022
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?

Jul 19 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2326
by: Ben R. | last post by:
I've got a repeater that's bound to a datareader and I'd like to conditionally modify the text that is being outputted. Previously, I've done this by assigning a name to attribute to the surrounding tablecell in which the <%# %> tags reside and I could use the findcontrol to grab a handle to the control by name and do any necessary...
1
2172
by: Phil | last post by:
I would like to make the CreateUserWizard's security question box display as a databound (xml file) dropdownlist. I can make the dropdownlist display by using a contenettemplate, but I can't get the databinding to work. When I reference the control I receive an error saying it doens't exist. I assume the control is buried within the...
1
2858
by: IMB | last post by:
I'm using VS2005 and am working with a ASP.NET 2.0 web site and I'm coding in VB.NET. I understand that I can add an item to a databound dropdownlist and make it the default selection using the following code: MyDropDownList.Items.Insert(0, "My default item") MyDropDownList.SelectedIndex = 0 The problem that I am experiencing is...
2
1711
by: cider123 | last post by:
I have a SqlDataSource bound to a DropDownList Combo. Works just fine. I noticed 2 properties you can set in the DropDownList, 1 for the value to be displayed in the "Text" column and 1 for the value to be used in the "Value" property. I'm at a catch 22 right now though.. The SqlDataSource gets 2 field values.. "Name" and "Description".
5
25302
by: maurban | last post by:
Hi there experts, I have a gridview with a couple textboxes and a dropdownlist. I'm trying to insert a default value into my database driven dropdownlist. I'm doing this in the rowdatabound event. My problem is that my code only works for the very first row in the gridview. For the first row, when I press "edit", my gridview goes to edit...
0
1662
by: Mark Micallef | last post by:
Hi, I'm having a problem using a databound dropdownlist inside a reorderlist ajax control. Here's a snippet of the code I'm using: <InsertItemTemplate> <div class="insertArea"> <asp:DropDownList ID="lstUser" runat="server" DataSourceID="UserDataSource" DataTextField="UserName" DataValueField="UserId" SelectedValue='<%# Bind("UserId") %>'...
3
3160
by: dizzy77 | last post by:
I have a dropdownlist databound to table column with string values "n" i "y". <asp:DropDownList ID="ddlPrivate" runat="server" SelectedValue='<%# Bind("cPrivate") %>'> <asp:ListItem Value="n">No</asp:ListItem> <asp:ListItem Value="y">Yes</asp:ListItem> </asp:DropDownList> How to handle situation when value is...
2
1669
by: Cirene | last post by:
In my databound datagrid i have a databound dropdownlist, like this: <asp:DropDownList ID="ddlAvail0" runat="server" SelectedValue='<%# Bind("AvailMon") %>'> <asp:ListItem></asp:ListItem> <asp:ListItem Value="1">Yes</asp:ListItem> <asp:ListItem Value="0">No</asp:ListItem> </asp:DropDownList> I would really prefer to use a checkbox, but...
5
1872
by: Cirene | last post by:
I seem to remember seeing a solution where a databound dropdownlist (used as a FILTER for a gridview) had the 1st item as "ALL ITEMS". It used a query with a UNION. But I can't find the example. It used no code I believe. Anyone have this solution handy? Thanks!
0
7478
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7668
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7923
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7437
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5984
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3466
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1901
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1025
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.