Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Drop-Down List

Status
Not open for further replies.

MONFU

Programmer
Oct 27, 2001
35
MT
Dear All,

I am new to ASP.NET and I am just experimenting a bit with the drop-down list.

I have the following

<asp:DropDownList CssClass="news_paragraph" DataSource="<%# dsCourses.DefaultView %>" DataTextField="courseTit" DataValueField="courseTit" ID="coursesList" runat="server"></asp:DropDownList>

Can you please tell me how can I insert a default value in the drop-down list, for example "Please Choose" which is not present in the dataset?

I can insert this text (Please Choose) as value 1 in the table, however I wish to know if there is a work around in VB.NET.

Hope I made my query understandable enough

thanks for your help and time

Johann
 
I you can insert 1 item or what I have done is selected my data in a stored proc like this...

select id,
textfield
from table

union

select -1 as id,
'<< Select >>' as textfield
 
Hello Checkai,

I am using Access as my database

Johann
 
I managed to solve it:-

DropDownList1.Items.Insert(0, "Please Choose")

Thanks for your help
 
I know this is an old post, but what the heck. I'm doing the same thing with the following code and it works fine:

If Not IsPostBack Then
DropDownList1.Items.Insert(0, "Please Choose")
End If

However,
if i select "Please Choose" in the drop-down menu after selecting another item, it gives me an error:

Index 0 is not non-negative and below total rows count.

how can i fix that?


 
I'm using the following on page load:
If Not IsPostBack Then
DropDownList1.Items.Insert(0, "Please Choose")
End If

And this is what i'm using for my Selected Index Changed:
Code:
 Private Sub ddlInsured_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddlInsured.SelectedIndexChanged

        cnNewClaim.Open()
        Dim populatequery As String = "SELECT FirstNamedInsured, InsuranceCompany, ID, [Policy Number] FROM TblPolicyTable WHERE ID LIKE '" & ddlInsured.SelectedItem.Value & "'"

        Dim objCommand As New OleDb.OleDbCommand(populatequery, cnNewClaim)
        daNewClaim.SelectCommand = objCommand

        daNewClaim.Fill(DsNewClaim1)
        txtInsCo.DataBind()
        txtPolicyID.DataBind()
        txtPolicyNumber.DataBind()

        cnNewClaim.Close()

    End Sub
 
Code:
Private Sub ddlInsured_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddlInsured.SelectedIndexChanged

 IF ddlInsured.selectedItem.Value <> "0" then
  cnNewClaim.Open()
  Dim populatequery As String = "SELECT FirstNamedInsured, InsuranceCompany, ID, [Policy Number] FROM TblPolicyTable WHERE ID LIKE '" & ddlInsured.SelectedItem.Value & "'"

  Dim objCommand As New OleDb.OleDbCommand(populatequery, cnNewClaim)
  daNewClaim.SelectCommand = objCommand

  daNewClaim.Fill(DsNewClaim1)
  txtInsCo.DataBind()
  txtPolicyID.DataBind()
  txtPolicyNumber.DataBind()

  cnNewClaim.Close()
 ELSE
   'Do nothing
 END IF

End Sub

you need something to check that the 1st item is not selected...
dlc
 
Code:
Private Sub ddlInsured_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddlInsured.SelectedIndexChanged
  response.write("<script>alert(" & ddlInsured.SelectedItem.Value & ");</script>"
  response.end

 IF ddlInsured.selectedItem.Value <> "0" then
  cnNewClaim.Open()
  Dim populatequery As String = "SELECT FirstNamedInsured, InsuranceCompany, ID, [Policy Number] FROM TblPolicyTable WHERE ID LIKE '" & ddlInsured.SelectedItem.Value & "'"

  Dim objCommand As New OleDb.OleDbCommand(populatequery, cnNewClaim)
  daNewClaim.SelectCommand = objCommand

  daNewClaim.Fill(DsNewClaim1)
  txtInsCo.DataBind()
  txtPolicyID.DataBind()
  txtPolicyNumber.DataBind()

  cnNewClaim.Close()
 ELSE
   'Do nothing
 END IF

End Sub

see what this does...
 
when i select an item in the drop down list, the datavaluefield pops up in a messagebox. when i click the "please choose" line, nothing happens..
 
try adding your 'select' item like this....

Code:
Dim item As ListItem = New ListItem("Please Choose", "-1")
Me.ddlInsured.Items.Insert(0, item)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top