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

asp:dropdownlist problem...

P: n/a
Hi all,

I’ve got a ‘dropdownlist’ web control and I can add ‘listitem’ no problem. I
can also bind data from an SQL database fine. My problem is that I want to
do both at the same time to allow me to have the first option in the list a
‘listitem’ saying something like ‘please pick an option’, and then the rest
of options coming from the database.

<asp:dropdownlist id="fm_Category" runat="server" DataTextField="CatName"
DataValueField="CatID">
<asp:ListItem Selected="true">please pick an option</asp:ListItem>
</asp:dropdownlist>

The options from the database seem to take priority and the ‘listitem’ is
not shown at all. Doing this in classic asp was easy as you could obviously
just write two ‘options’ and then loop the second one with data from the
database.

This is what I’m looking for:

<select name="fm_Category" id="fm_Category">
<option value="">please pick an option</option>
<option value="1">Data from db 1</option>
<option value="2">Data from db 2</option>
<option value="3">Data from db 3</option>
</select>

I’ve only just started to learn asp.NET having come from classic asp so I
hope I’m not just being stupid with this. Any help would be great?

Thanks

Steve

Nov 18 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Hi Steve,

After you bind your data do this:

Dim liItemLoc As New ListItem()
liItemLoc.Text = "please pick an option"
liItemLoc.Value = "0"
fm_Category.Items.Insert(0, liItemLoc)

Good luck! Ken.

--
Ken Dopierala Jr.
For great ASP.Net web hosting try:
http://www.webhost4life.com/default.asp?refid=Spinlight
If you sign up under me and need help, email me.

"Lastie" <La****@discussions.microsoft.com> wrote in message
news:F3**********************************@microsof t.com...
Hi all,

I've got a 'dropdownlist' web control and I can add 'listitem' no problem. I can also bind data from an SQL database fine. My problem is that I want to do both at the same time to allow me to have the first option in the list a 'listitem' saying something like 'please pick an option', and then the rest of options coming from the database.

<asp:dropdownlist id="fm_Category" runat="server" DataTextField="CatName"
DataValueField="CatID">
<asp:ListItem Selected="true">please pick an option</asp:ListItem>
</asp:dropdownlist>

The options from the database seem to take priority and the 'listitem' is
not shown at all. Doing this in classic asp was easy as you could obviously just write two 'options' and then loop the second one with data from the
database.

This is what I'm looking for:

<select name="fm_Category" id="fm_Category">
<option value="">please pick an option</option>
<option value="1">Data from db 1</option>
<option value="2">Data from db 2</option>
<option value="3">Data from db 3</option>
</select>

I've only just started to learn asp.NET having come from classic asp so I
hope I'm not just being stupid with this. Any help would be great?

Thanks

Steve

Nov 18 '05 #2

P: n/a
Hi Steve,

After you fill the DDL from the database, user the Items.Insert method to
put in a new instruction item. The Insert method takes the Index value which
is the position in the list where the new item should go.

Some sample code below. Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]

<asp:dropdownlist id="fm_Category" runat="server" >
</asp:dropdownlist>

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
If Not IsPostBack Then
fm_Category.DataSource = CreateDataSource()
fm_Category.DataTextField = "CatName"
fm_Category.DataValueField = "CatID"
fm_Category.DataBind()

Dim lsItem As New ListItem
lsItem.Text = "Please pick an option"
lsItem.Value = "0"
fm_Category.Items.Insert(0, lsItem)
End If
End Sub
Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("CatID", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("CatName", GetType(String)))
dt.Columns.Add(New DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource

"Lastie" <La****@discussions.microsoft.com> wrote in message
news:F3**********************************@microsof t.com...
Hi all,

I’ve got a ‘dropdownlist’ web control and I can add ‘listitem’ no problem.
I
can also bind data from an SQL database fine. My problem is that I want
to
do both at the same time to allow me to have the first option in the list
a
‘listitem’ saying something like ‘please pick an option’, and then the
rest
of options coming from the database.

<asp:dropdownlist id="fm_Category" runat="server" DataTextField="CatName"
DataValueField="CatID">
<asp:ListItem Selected="true">please pick an option</asp:ListItem>
</asp:dropdownlist>

The options from the database seem to take priority and the ‘listitem’ is
not shown at all. Doing this in classic asp was easy as you could
obviously
just write two ‘options’ and then loop the second one with data from the
database.

This is what I’m looking for:

<select name="fm_Category" id="fm_Category">
<option value="">please pick an option</option>
<option value="1">Data from db 1</option>
<option value="2">Data from db 2</option>
<option value="3">Data from db 3</option>
</select>

I’ve only just started to learn asp.NET having come from classic asp so I
hope I’m not just being stupid with this. Any help would be great?

Thanks

Steve


Nov 18 '05 #3

P: n/a
>lsItem.Value = "0"

Why default value to "0" and not ""?

Wouldn't "" be easier to tack a required field validator to (that is
probably his next move). I know you could've set the InitialValue of your
RequiredFieldValidator="0", but seems like an extra step...

Noticed both responses to the OP did it the "0" way, and I thought that odd.
Maybe I am missing something.

I suppose it really boils down to one's own preference.

Greg
"Ken Cox [Microsoft MVP]" <BA************@sympatico.ca> wrote in message
news:%2******************@TK2MSFTNGP14.phx.gbl...
Hi Steve,

After you fill the DDL from the database, user the Items.Insert method to
put in a new instruction item. The Insert method takes the Index value
which is the position in the list where the new item should go.

Some sample code below. Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]

<asp:dropdownlist id="fm_Category" runat="server" >
</asp:dropdownlist>

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
If Not IsPostBack Then
fm_Category.DataSource = CreateDataSource()
fm_Category.DataTextField = "CatName"
fm_Category.DataValueField = "CatID"
fm_Category.DataBind()

Dim lsItem As New ListItem
lsItem.Text = "Please pick an option"
lsItem.Value = "0"
fm_Category.Items.Insert(0, lsItem)
End If
End Sub
Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("CatID", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("CatName", GetType(String)))
dt.Columns.Add(New DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource

"Lastie" <La****@discussions.microsoft.com> wrote in message
news:F3**********************************@microsof t.com...
Hi all,

I've got a 'dropdownlist' web control and I can add 'listitem' no
problem. I
can also bind data from an SQL database fine. My problem is that I want
to
do both at the same time to allow me to have the first option in the list
a
'listitem' saying something like 'please pick an option', and then the
rest
of options coming from the database.

<asp:dropdownlist id="fm_Category" runat="server" DataTextField="CatName"
DataValueField="CatID">
<asp:ListItem Selected="true">please pick an option</asp:ListItem>
</asp:dropdownlist>

The options from the database seem to take priority and the 'listitem' is
not shown at all. Doing this in classic asp was easy as you could
obviously
just write two 'options' and then loop the second one with data from the
database.

This is what I'm looking for:

<select name="fm_Category" id="fm_Category">
<option value="">please pick an option</option>
<option value="1">Data from db 1</option>
<option value="2">Data from db 2</option>
<option value="3">Data from db 3</option>
</select>

I've only just started to learn asp.NET having come from classic asp so I
hope I'm not just being stupid with this. Any help would be great?

Thanks

Steve

Nov 18 '05 #4

P: n/a
Hi Greg,

I always use 0 because of how I code. My Identity fields for my tables
always start at 1. I'm also obsessive about Option Strict and explicitly
converting to the correct type before I use it and not allowing VB.Net to do
the conversion for me. Because my Value field almost always contains an ID
number which is an integer I always process it with
CInt(ddl.SelectedItem.Value), that is why I always use 0 instead of an empty
string. Other people might do it for different reasons though. Also,
handling conversions this way, should almost make it a certainty that my
code will work no matter what changes VB goes through in the future. I love
how powerful VB is becoming and currently when left to do it's own thing on
conversions it almost always gets it right. However I feel that the more
powerful VB becomes in the future the less it can be relied upon to shortcut
things for the programmer. The way I look at, is that in theory, for VB to
continue to grow in power at the rate it is things like Option Strict will
need to no longer even be an option but simply the rule. Ken.

--
Ken Dopierala Jr.
For great ASP.Net web hosting try:
http://www.webhost4life.com/default.asp?refid=Spinlight
If you sign up under me and need help, email me.

"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
lsItem.Value = "0"
Why default value to "0" and not ""?

Wouldn't "" be easier to tack a required field validator to (that is
probably his next move). I know you could've set the InitialValue of your
RequiredFieldValidator="0", but seems like an extra step...

Noticed both responses to the OP did it the "0" way, and I thought that

odd. Maybe I am missing something.

I suppose it really boils down to one's own preference.

Greg
"Ken Cox [Microsoft MVP]" <BA************@sympatico.ca> wrote in message
news:%2******************@TK2MSFTNGP14.phx.gbl...
Hi Steve,

After you fill the DDL from the database, user the Items.Insert method to put in a new instruction item. The Insert method takes the Index value
which is the position in the list where the new item should go.

Some sample code below. Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]

<asp:dropdownlist id="fm_Category" runat="server" >
</asp:dropdownlist>

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
If Not IsPostBack Then
fm_Category.DataSource = CreateDataSource()
fm_Category.DataTextField = "CatName"
fm_Category.DataValueField = "CatID"
fm_Category.DataBind()

Dim lsItem As New ListItem
lsItem.Text = "Please pick an option"
lsItem.Value = "0"
fm_Category.Items.Insert(0, lsItem)
End If
End Sub
Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("CatID", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("CatName", GetType(String)))
dt.Columns.Add(New DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource

"Lastie" <La****@discussions.microsoft.com> wrote in message
news:F3**********************************@microsof t.com...
Hi all,

I've got a 'dropdownlist' web control and I can add 'listitem' no
problem. I
can also bind data from an SQL database fine. My problem is that I want to
do both at the same time to allow me to have the first option in the list a
'listitem' saying something like 'please pick an option', and then the
rest
of options coming from the database.

<asp:dropdownlist id="fm_Category" runat="server" DataTextField="CatName" DataValueField="CatID">
<asp:ListItem Selected="true">please pick an option</asp:ListItem>
</asp:dropdownlist>

The options from the database seem to take priority and the 'listitem' is not shown at all. Doing this in classic asp was easy as you could
obviously
just write two 'options' and then loop the second one with data from the database.

This is what I'm looking for:

<select name="fm_Category" id="fm_Category">
<option value="">please pick an option</option>
<option value="1">Data from db 1</option>
<option value="2">Data from db 2</option>
<option value="3">Data from db 3</option>
</select>

I've only just started to learn asp.NET having come from classic asp so I hope I'm not just being stupid with this. Any help would be great?

Thanks

Steve


Nov 18 '05 #5

P: n/a
I agree 100% with Option Strict. If I see a post in VB that could have
obviously been avoided with Option Strict On I always metion that first. I
think having it as the default would cut down on the majority of (newbie)
posts to these groups.

Is Option Strict On the default yet in VB.NET 2.0? Let us hope. :^)

As far as processing CInt(ddl.SelectedItem.Value); I guess I never worry
about it cause the validator is gonna keep "" from ever making it as a
Value.

Greg

"Ken Dopierala Jr." <kd*********@wi.rr.com> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...
Hi Greg,

I always use 0 because of how I code. My Identity fields for my tables
always start at 1. I'm also obsessive about Option Strict and explicitly
converting to the correct type before I use it and not allowing VB.Net to
do
the conversion for me. Because my Value field almost always contains an
ID
number which is an integer I always process it with
CInt(ddl.SelectedItem.Value), that is why I always use 0 instead of an
empty
string. Other people might do it for different reasons though. Also,
handling conversions this way, should almost make it a certainty that my
code will work no matter what changes VB goes through in the future. I
love
how powerful VB is becoming and currently when left to do it's own thing
on
conversions it almost always gets it right. However I feel that the more
powerful VB becomes in the future the less it can be relied upon to
shortcut
things for the programmer. The way I look at, is that in theory, for VB
to
continue to grow in power at the rate it is things like Option Strict will
need to no longer even be an option but simply the rule. Ken.

--
Ken Dopierala Jr.
For great ASP.Net web hosting try:
http://www.webhost4life.com/default.asp?refid=Spinlight
If you sign up under me and need help, email me.

"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
>lsItem.Value = "0"


Why default value to "0" and not ""?

Wouldn't "" be easier to tack a required field validator to (that is
probably his next move). I know you could've set the InitialValue of your
RequiredFieldValidator="0", but seems like an extra step...

Noticed both responses to the OP did it the "0" way, and I thought that

odd.
Maybe I am missing something.

I suppose it really boils down to one's own preference.

Greg
"Ken Cox [Microsoft MVP]" <BA************@sympatico.ca> wrote in message
news:%2******************@TK2MSFTNGP14.phx.gbl...
> Hi Steve,
>
> After you fill the DDL from the database, user the Items.Insert method to > put in a new instruction item. The Insert method takes the Index value
> which is the position in the list where the new item should go.
>
> Some sample code below. Let us know if this helps?
>
> Ken
> Microsoft MVP [ASP.NET]
>
> <asp:dropdownlist id="fm_Category" runat="server" >
> </asp:dropdownlist>
>
> Private Sub Page_Load _
> (ByVal sender As System.Object, _
> ByVal e As System.EventArgs) _
> Handles MyBase.Load
> If Not IsPostBack Then
> fm_Category.DataSource = CreateDataSource()
> fm_Category.DataTextField = "CatName"
> fm_Category.DataValueField = "CatID"
> fm_Category.DataBind()
>
> Dim lsItem As New ListItem
> lsItem.Text = "Please pick an option"
> lsItem.Value = "0"
> fm_Category.Items.Insert(0, lsItem)
> End If
> End Sub
> Function CreateDataSource() As DataTable
> Dim dt As New DataTable
> Dim dr As DataRow
> dt.Columns.Add(New DataColumn _
> ("CatID", GetType(Int32)))
> dt.Columns.Add(New DataColumn _
> ("CatName", GetType(String)))
> dt.Columns.Add(New DataColumn _
> ("CurrencyValue", GetType(Double)))
> dt.Columns.Add(New DataColumn _
> ("Boolean", GetType(Boolean)))
> Dim i As Integer
> For i = 0 To 8
> dr = dt.NewRow()
> dr(0) = i
> dr(1) = "Item " + i.ToString()
> dr(2) = 1.23 * (i + 1)
> dr(3) = (i = 4)
> dt.Rows.Add(dr)
> Next i
> Return dt
> End Function 'CreateDataSource
>
> "Lastie" <La****@discussions.microsoft.com> wrote in message
> news:F3**********************************@microsof t.com...
>> Hi all,
>>
>> I've got a 'dropdownlist' web control and I can add 'listitem' no
>> problem. I
>> can also bind data from an SQL database fine. My problem is that I want >> to
>> do both at the same time to allow me to have the first option in the list >> a
>> 'listitem' saying something like 'please pick an option', and then the
>> rest
>> of options coming from the database.
>>
>> <asp:dropdownlist id="fm_Category" runat="server" DataTextField="CatName" >> DataValueField="CatID">
>> <asp:ListItem Selected="true">please pick an option</asp:ListItem>
>> </asp:dropdownlist>
>>
>> The options from the database seem to take priority and the 'listitem' is >> not shown at all. Doing this in classic asp was easy as you could
>> obviously
>> just write two 'options' and then loop the second one with data from the >> database.
>>
>> This is what I'm looking for:
>>
>> <select name="fm_Category" id="fm_Category">
>> <option value="">please pick an option</option>
>> <option value="1">Data from db 1</option>
>> <option value="2">Data from db 2</option>
>> <option value="3">Data from db 3</option>
>> </select>
>>
>> I've only just started to learn asp.NET having come from classic asp
>> so I >> hope I'm not just being stupid with this. Any help would be great?
>>
>> Thanks
>>
>> Steve
>>
>



Nov 18 '05 #6

P: n/a
You're right. It doesn't really matter.

Ken

"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
lsItem.Value = "0"


Why default value to "0" and not ""?

Wouldn't "" be easier to tack a required field validator to (that is
probably his next move). I know you could've set the InitialValue of your
RequiredFieldValidator="0", but seems like an extra step...

Noticed both responses to the OP did it the "0" way, and I thought that
odd. Maybe I am missing something.

I suppose it really boils down to one's own preference.

Greg


Nov 18 '05 #7

P: n/a
Hi everyone,

Thanks for all the replies, it’s quite interesting to see how people do
things in different ways, and it’s all helping me with the transfer from asp
to .net (moving to object orientated that I’ve not used before). My code now
looks like:

fm_Category.DataSource = ds
fm_Category.DataTextField = "CatName"
fm_Category.DataValueField = "CatID"
fm_Category.DataBind()

Dim FirstOption As New ListItem()
FirstOption.Text = "Please select"
FirstOption.Value = ""
fm_Category.Items.Insert(0, FirstOption)

<asp:dropdownlist id="fm_Cate-ory" runat="server"></asp:dropdownlist>

I think I’ve got to try and always think about where my code goes, as you
saw my first instinct was to try and add the first option for the list where
the dropdown will appear in the page rather than within the Sub where I’m
binding the data. It’s the separation of scripting code from the display
code that I’ve read so much about.

Thanks again to all. :-)

Steve



"Ken Dopierala Jr." wrote:
Hi Steve,

After you bind your data do this:

Dim liItemLoc As New ListItem()
liItemLoc.Text = "please pick an option"
liItemLoc.Value = "0"
fm_Category.Items.Insert(0, liItemLoc)

Good luck! Ken.

--
Ken Dopierala Jr.
For great ASP.Net web hosting try:
http://www.webhost4life.com/default.asp?refid=Spinlight
If you sign up under me and need help, email me.

"Lastie" <La****@discussions.microsoft.com> wrote in message
news:F3**********************************@microsof t.com...
Hi all,

I've got a 'dropdownlist' web control and I can add 'listitem' no problem.

I
can also bind data from an SQL database fine. My problem is that I want

to
do both at the same time to allow me to have the first option in the list

a
'listitem' saying something like 'please pick an option', and then the

rest
of options coming from the database.

<asp:dropdownlist id="fm_Category" runat="server" DataTextField="CatName"
DataValueField="CatID">
<asp:ListItem Selected="true">please pick an option</asp:ListItem>
</asp:dropdownlist>

The options from the database seem to take priority and the 'listitem' is
not shown at all. Doing this in classic asp was easy as you could

obviously
just write two 'options' and then loop the second one with data from the
database.

This is what I'm looking for:

<select name="fm_Category" id="fm_Category">
<option value="">please pick an option</option>
<option value="1">Data from db 1</option>
<option value="2">Data from db 2</option>
<option value="3">Data from db 3</option>
</select>

I've only just started to learn asp.NET having come from classic asp so I
hope I'm not just being stupid with this. Any help would be great?

Thanks

Steve


Nov 18 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.