Dropdownlist add default blank while bind to sql server

S

syncwa

Hi,

My dropdownlist is binded to a sql datasource.
I would like to have it list a blank entry on top and do not show real
data before user select it.
How can I do this.? Thanks
 
S

syncwa

Sorry, I'm new to this. I put it in test.aspx.cs but it doesn't work.
Could you take a look at the following code to see where I should put
this? Regards


test.aspx.cs::

public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DropDownList1.Items.Insert(0, "");
}
}


test.aspx:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="testSqlDataSource1"
DataTextField="Company_Name" DataValueField="Company_ID">
</asp:DropDownList><asp:SqlDataSource ID="testSqlDataSource1"
runat="server" ConnectionString="<%$
ConnectionStrings:kConnectionString %>"
SelectCommand="SELECT [Company_Name], [Company_ID] FROM
[Companies]"></asp:SqlDataSource>

</div>
</form>
</body>
</html>
 
A

Alex D.

I would suggest to databind programatically instead of using the IDE.
otherwise I jus dont know how to do it ;-)


syncwa said:
Sorry, I'm new to this. I put it in test.aspx.cs but it doesn't work.
Could you take a look at the following code to see where I should put
this? Regards


test.aspx.cs::

public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DropDownList1.Items.Insert(0, "");
}
}


test.aspx:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="testSqlDataSource1"
DataTextField="Company_Name" DataValueField="Company_ID">
</asp:DropDownList><asp:SqlDataSource ID="testSqlDataSource1"
runat="server" ConnectionString="<%$
ConnectionStrings:kConnectionString %>"
SelectCommand="SELECT [Company_Name], [Company_ID] FROM
[Companies]"></asp:SqlDataSource>

</div>
</form>
</body>
</html>



after you databind do this:
this.DropDownList1.Items.Insert(0,"");
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top