Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. First item in a DropDownList

First item in a DropDownList

Scheduled Pinned Locked Moved ASP.NET
13 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G gharry

    The problem is that a user after having eneterded a name and hitting the backspace gest a dropdowlist with choices. if his coice is the first name on the list than that choice should be honored. The page you refered to doenst say me anything becourse i am a simpley guy just building a simple page and not a java programmer. Yet another piece of magic to learn :-)

    A Offline
    A Offline
    Agha net
    wrote on last edited by
    #4

    Dear that is javascript not java :) anyway .. can you show me that page so that i can know what exactly you want .

    G 1 Reply Last reply
    0
    • A Agha net

      Dear that is javascript not java :) anyway .. can you show me that page so that i can know what exactly you want .

      G Offline
      G Offline
      gharry
      wrote on last edited by
      #5

      Sorry but cant show the page becourse it is behind a secure company website running ISS. The outside websites running Apache so not asp.net able. But i am trying a other aproach adding a extra row on the dataset.table. This code: Else 'Found many Rows and place them in a DropDownList Dim dataRow As DataRow dataRow = dataSet.Tables("Horses").NewRow() dataRow.Item("Horse_name") = "--Select a Horse--" dataRow.Item("Horse_NF") = "--Select a Horse--" dataRow.Item("FEI_Pass_No") = "--Select a Horse--" dataRow.Item("Sex") = "--Select a Horse--" dataRow.Item("Horse_DOB") = "--Select a Horse--" dataSet.Tables(0).Rows.Add(dataRow) dataListHorses.DataSource = dataSet.Tables("Horses").DefaultView dataListHorses.DataBind() HorseName.Visible = "false" dataListHorses.Visible = "true" End If But something goes wrong. It wil insert a new row but it contains no data. Dont know why

      R 1 Reply Last reply
      0
      • G gharry

        Sorry but cant show the page becourse it is behind a secure company website running ISS. The outside websites running Apache so not asp.net able. But i am trying a other aproach adding a extra row on the dataset.table. This code: Else 'Found many Rows and place them in a DropDownList Dim dataRow As DataRow dataRow = dataSet.Tables("Horses").NewRow() dataRow.Item("Horse_name") = "--Select a Horse--" dataRow.Item("Horse_NF") = "--Select a Horse--" dataRow.Item("FEI_Pass_No") = "--Select a Horse--" dataRow.Item("Sex") = "--Select a Horse--" dataRow.Item("Horse_DOB") = "--Select a Horse--" dataSet.Tables(0).Rows.Add(dataRow) dataListHorses.DataSource = dataSet.Tables("Horses").DefaultView dataListHorses.DataBind() HorseName.Visible = "false" dataListHorses.Visible = "true" End If But something goes wrong. It wil insert a new row but it contains no data. Dont know why

        R Offline
        R Offline
        RSArockiam
        wrote on last edited by
        #6

        Don't create new record in dataset. Just create a new listitem in your dropdownlist after binding the data. DropDownList1.DataSource=.... DropDownList1.DataBind() Dim LItem as New ListItem("---Select a Horse---"); DropDownList1.Items.Insert(0,LItem); Regards R.Arockiapathinathan

        G 1 Reply Last reply
        0
        • R RSArockiam

          Don't create new record in dataset. Just create a new listitem in your dropdownlist after binding the data. DropDownList1.DataSource=.... DropDownList1.DataBind() Dim LItem as New ListItem("---Select a Horse---"); DropDownList1.Items.Insert(0,LItem); Regards R.Arockiapathinathan

          G Offline
          G Offline
          gharry
          wrote on last edited by
          #7

          Sorry but that dit not work, this is the code is use: dataListRiders.DataSource = dataSet.Tables("Riders").DefaultView dataListRiders.DataBind() Dim LItem As New ListItem("---Select a Horse---") dataListRiders.Items.Insert(0, LItem)

          R 1 Reply Last reply
          0
          • G gharry

            Sorry but that dit not work, this is the code is use: dataListRiders.DataSource = dataSet.Tables("Riders").DefaultView dataListRiders.DataBind() Dim LItem As New ListItem("---Select a Horse---") dataListRiders.Items.Insert(0, LItem)

            R Offline
            R Offline
            RSArockiam
            wrote on last edited by
            #8

            i couldn't identify ur exact problem. Normally from the dropdownlist box, first item is selected in default. after that if u changing the item(selected item) to different item then the selecteditemchanged event will fire. otherwise it will not fire. from the above solution , in default the empty ("---select a horse---") string is the first item and at the time the event will not execute. I think you r using the dropdown list as a help window (for searching the text, that is in default it is invisible, and the user editing the text from the textbox, it should show - is it correct?), in this put empty string " " instead of "--select a horse--" and it should work. otherwise post ur code to identify the problem. Regards R.Arockiapathinathan

            G 1 Reply Last reply
            0
            • R RSArockiam

              i couldn't identify ur exact problem. Normally from the dropdownlist box, first item is selected in default. after that if u changing the item(selected item) to different item then the selecteditemchanged event will fire. otherwise it will not fire. from the above solution , in default the empty ("---select a horse---") string is the first item and at the time the event will not execute. I think you r using the dropdown list as a help window (for searching the text, that is in default it is invisible, and the user editing the text from the textbox, it should show - is it correct?), in this put empty string " " instead of "--select a horse--" and it should work. otherwise post ur code to identify the problem. Regards R.Arockiapathinathan

              G Offline
              G Offline
              gharry
              wrote on last edited by
              #9

              Please find the problem subroutine down here: Sub BindDataRiders(ByVal strSwitch As String, ByVal strValue As String) 'Object vars Dim sqlConnection As SqlConnection Dim sqlDataAdaptor As SqlDataAdapter Dim sqlCommand As SqlCommand Dim dataSet As DataSet Try 'First construct connectionstring and open connection sqlConnection = New SqlConnection("server=localhost;uid=sa;pwd=secret;database=EventMngt") sqlConnection.Open() 'Then construct SELECT string including a extra Column named Combi for the DropDownList DataTextField ──┐ Dim sqlString As String = "SELECT Rider_Family_Name, Rider_Given_Name, Rider_NF, FEI_Rider_ID_No, Gender, Rider_DOB, Rider_Family_Name + ', ' + Rider_Given_Name + '-'+ Rider_NF As Combi FROM C_Registered_riders WHERE " & strSwitch sqlCommand = New SqlCommand(sqlString, sqlConnection) Response.Write(sqlString & "
              ") 'Put the result in a DataSet named Riders sqlDataAdaptor = New SqlDataAdapter(sqlCommand) dataSet = New DataSet() sqlDataAdaptor.Fill(dataSet, "Riders") dataSet.Tables(0).DefaultView.Sort = "rider_family_name ASC" 'Check for none, one or many row If (dataSet.Tables(0).Rows.Count) = 1 Then 'Found only one Row Rider_Family_Name.Text = dataSet.Tables("Riders").Rows(0)("Rider_Family_Name").ToString() Rider_Given_Name.Text = dataSet.Tables("Riders").Rows(0)("Rider_Given_Name").ToString() Rider_NF.Text = dataSet.Tables("Riders").Rows(0)("Rider_NF").ToString() Rider_NFFull.Text = getNF(Rider_NF.Text) Rider_FEIID.Text = dataSet.Tables("Riders").Rows(0)("FEI_Rider_ID_no").ToString() Rider_Gender.Text = dataSet.Tables("Riders").Rows(0)("Gender").ToString() Rider_DOB.Text = dataSet.Tables("Riders").Rows(0)("Rider_DOB").ToString() Response.Write("
              Date:" & Replace(Rider_DOB.Text, ".", "-")) Rider_Age.Text = Age(Rider_DOB.Text) dataListRiders.Visible = "false" ElseIf (dataSet.Tables(0).Rows.Count) > 1 Then 'Found many Rows and place them in a DropDownList dataListRiders.DataSource = dataSet.Tables("Riders").DefaultView dataListRiders.DataBind() 'Add extra listItem at the start of the list dataListRiders.Items.Insert(1, New ListItem("")) RiderFamilyName.Visible = "false" dataListRiders.Visible = "true" Else 'Found no records lbl

              R 1 Reply Last reply
              0
              • G gharry

                When i create a DropDownList normaly the selected item event fires when a new item is selected. But when i choose the first item it doenst fire so i never can choose the first item in a DDL. Anny solution.

                A Offline
                A Offline
                andyk1967
                wrote on last edited by
                #10

                lstland.SelectedIndex = -1 after the databind should do the trick... or otherwise you could add an extra field on top before the drodown is generated f.e. "select an item"

                1 Reply Last reply
                0
                • G gharry

                  Please find the problem subroutine down here: Sub BindDataRiders(ByVal strSwitch As String, ByVal strValue As String) 'Object vars Dim sqlConnection As SqlConnection Dim sqlDataAdaptor As SqlDataAdapter Dim sqlCommand As SqlCommand Dim dataSet As DataSet Try 'First construct connectionstring and open connection sqlConnection = New SqlConnection("server=localhost;uid=sa;pwd=secret;database=EventMngt") sqlConnection.Open() 'Then construct SELECT string including a extra Column named Combi for the DropDownList DataTextField ──┐ Dim sqlString As String = "SELECT Rider_Family_Name, Rider_Given_Name, Rider_NF, FEI_Rider_ID_No, Gender, Rider_DOB, Rider_Family_Name + ', ' + Rider_Given_Name + '-'+ Rider_NF As Combi FROM C_Registered_riders WHERE " & strSwitch sqlCommand = New SqlCommand(sqlString, sqlConnection) Response.Write(sqlString & "
                  ") 'Put the result in a DataSet named Riders sqlDataAdaptor = New SqlDataAdapter(sqlCommand) dataSet = New DataSet() sqlDataAdaptor.Fill(dataSet, "Riders") dataSet.Tables(0).DefaultView.Sort = "rider_family_name ASC" 'Check for none, one or many row If (dataSet.Tables(0).Rows.Count) = 1 Then 'Found only one Row Rider_Family_Name.Text = dataSet.Tables("Riders").Rows(0)("Rider_Family_Name").ToString() Rider_Given_Name.Text = dataSet.Tables("Riders").Rows(0)("Rider_Given_Name").ToString() Rider_NF.Text = dataSet.Tables("Riders").Rows(0)("Rider_NF").ToString() Rider_NFFull.Text = getNF(Rider_NF.Text) Rider_FEIID.Text = dataSet.Tables("Riders").Rows(0)("FEI_Rider_ID_no").ToString() Rider_Gender.Text = dataSet.Tables("Riders").Rows(0)("Gender").ToString() Rider_DOB.Text = dataSet.Tables("Riders").Rows(0)("Rider_DOB").ToString() Response.Write("
                  Date:" & Replace(Rider_DOB.Text, ".", "-")) Rider_Age.Text = Age(Rider_DOB.Text) dataListRiders.Visible = "false" ElseIf (dataSet.Tables(0).Rows.Count) > 1 Then 'Found many Rows and place them in a DropDownList dataListRiders.DataSource = dataSet.Tables("Riders").DefaultView dataListRiders.DataBind() 'Add extra listItem at the start of the list dataListRiders.Items.Insert(1, New ListItem("")) RiderFamilyName.Visible = "false" dataListRiders.Visible = "true" Else 'Found no records lbl

                  R Offline
                  R Offline
                  RSArockiam
                  wrote on last edited by
                  #11

                  where this BindDataRiders sub called? I think, this is from the textbox changed event, is it? if this is, what is the need of extra item in ddl? still, i couldn't understand ur exact problem. Regards R.Arockiapathinathan

                  G 1 Reply Last reply
                  0
                  • R RSArockiam

                    where this BindDataRiders sub called? I think, this is from the textbox changed event, is it? if this is, what is the need of extra item in ddl? still, i couldn't understand ur exact problem. Regards R.Arockiapathinathan

                    G Offline
                    G Offline
                    gharry
                    wrote on last edited by
                    #12

                    The first time it is called from the textbox changed event, it then does a search in the database on names starting with the textbox content. It then produces either no record, a single record or a list of records. If it has a list of records the user should be able to pick any name in that dropdownlist, either the first or any other. And the problem is the first record, so there must be a extra first record.

                    R 1 Reply Last reply
                    0
                    • G gharry

                      The first time it is called from the textbox changed event, it then does a search in the database on names starting with the textbox content. It then produces either no record, a single record or a list of records. If it has a list of records the user should be able to pick any name in that dropdownlist, either the first or any other. And the problem is the first record, so there must be a extra first record.

                      R Offline
                      R Offline
                      RSArockiam
                      wrote on last edited by
                      #13

                      so this (dataListRiders.Items.Insert(0, New ListItem(""))) will solve the problem. Regards R.Arockiapathinathan

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups