asp:dropdownlist problem...

G

Guest

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
 
K

Ken Dopierala Jr.

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.
 
K

Ken Cox [Microsoft MVP]

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
 
G

Greg Burns

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 said:
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 said:
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
 
K

Ken Dopierala Jr.

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 said:
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 said:
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 said:
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
 
G

Greg Burns

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. said:
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 said:
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 said:
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

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
 
K

Ken Cox [Microsoft MVP]

You're right. It doesn't really matter.

Ken

Greg Burns said:
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
 
G

Guest

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
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top