Dropdownlist and SelectedItem in ASP.Net

Ask a Question related to ASP.NET General, Design and Development.

  1. #1

    Default Re: Dropdownlist and SelectedItem in ASP.Net

    Does each item have a different Value?

    "md" <no@spam.sux> wrote in message
    news:ebXfpwxODHA.3700@tk2msftngp13.phx.gbl...
    > Hello all -
    >
    > I am having a goofy problem that I strongly suspect is a (gasp!) bug in my
    > code, but I can't for the life of me figure it out.
    >
    > I have a Dropdownlist control on a page that is bound in code to a
    DataTable
    > object. That works fine, it shows the data. I select an item in the list
    > other than the first item, but when I click on a button and read the
    > .SelectedItem.Value property it always returns the value for the first
    item
    > in the list.
    >
    > I used the command window and displayed Dropdownlist1.Items(x).Value for
    > each item in the list (there are only three at the moment), and it
    displayed
    > the correct value, but when I try to use the .SelectedItem.Value property
    I
    > always get the value for the first entry in the list, regardless of what
    was
    > chosen.
    >
    > Anybody run into this? I appreciate any help anyone can offer.
    >
    > Matt
    >
    >

    Marina Guest

  2. Similar Questions and Discussions

    1. Datagrid and loose coupling of selectedItem
      That was a very nicely explained.But I have a question.Actually I have many as i am new to Flex and action script. let me explain myself.....i...
    2. Dynamic Value and SelectedItem
      Hallo All I want to select dinamically an object (es DataGrid) from the stage.... nameDataGrid.selectedItem.DataToRead Is there a method to...
    3. XXX selectedItem
      if you have a datagride with several rows, and you wont to change the highlight row by raising an event, say clik a button . how can this happen....
    4. passing selectedItem data from a component
      I need help as I could not figure out my mistake ? I have main application page and two components. The main page calls for the first component to...
    5. Help Me~~My DropDownList Can not get the"BigClassList.SelectedItem.Text" there is an error: System.NullReferenceException
      I have many dropdownlist controls in my ascx (and use LoadControl in a aspx fiel) file,so i write a method "InitList(DropDownList list,string...
  3. #2

    Default Re: Dropdownlist and SelectedItem in ASP.Net

    "md" <no@spam.sux> wrote in message
    news:ebXfpwxODHA.3700@tk2msftngp13.phx.gbl...
    > Hello all -
    >
    > I am having a goofy problem that I strongly suspect is a (gasp!) bug in my
    > code, but I can't for the life of me figure it out.
    >
    > I have a Dropdownlist control on a page that is bound in code to a
    DataTable
    > object. That works fine, it shows the data. I select an item in the list
    > other than the first item, but when I click on a button and read the
    > .SelectedItem.Value property it always returns the value for the first
    item in the list.
    >
    > I used the command window and displayed Dropdownlist1.Items(x).Value for
    > each item in the list (there are only three at the moment), and it
    displayed
    > the correct value, but when I try to use the .SelectedItem.Value property
    I always get the value for the first entry in the list, regardless of what
    was chosen.
    >
    > Anybody run into this? I appreciate any help anyone can offer.
    >
    > Matt
    My guess is that you are databinding again on postback.
    This causes your server to lose the state of your list.

    Try changing your Page_Load event in something like this:

    Sub Page_Load(Sender As Object, E As EventArgs)

    If Not Page.IsPostBack Then
    -----Bind your list here-----
    End If

    End Sub

    --

    Jos Branders



    Jos Branders Guest

  4. #3

    Default Re: Dropdownlist and SelectedItem in ASP.Net

    Thanks for your reply. I had AutoPostback set to False on the dropdownlist.
    I recoded it with AutoPostback set to true and rebound only when IsPostBack
    was false, and now it works. Thanks for your help, but I don't really
    understand why it has to post back to the server just to change the selected
    item in the dropdown, but apparently it does. Of course I'm not an ASP.Net
    expert (obviously). Thanks again for your help.

    Matt

    "Jos Branders" <josnospambranders@fastmail.fm> wrote in message
    news:3ef9a719$0$1045$ba620e4c@reader1.news.skynet. be...
    > "md" <no@spam.sux> wrote in message
    > news:ebXfpwxODHA.3700@tk2msftngp13.phx.gbl...
    > > Hello all -
    > >
    > > I am having a goofy problem that I strongly suspect is a (gasp!) bug in
    my
    > > code, but I can't for the life of me figure it out.
    > >
    > > I have a Dropdownlist control on a page that is bound in code to a
    > DataTable
    > > object. That works fine, it shows the data. I select an item in the list
    > > other than the first item, but when I click on a button and read the
    > > .SelectedItem.Value property it always returns the value for the first
    > item in the list.
    > >
    > > I used the command window and displayed Dropdownlist1.Items(x).Value for
    > > each item in the list (there are only three at the moment), and it
    > displayed
    > > the correct value, but when I try to use the .SelectedItem.Value
    property
    > I always get the value for the first entry in the list, regardless of what
    > was chosen.
    > >
    > > Anybody run into this? I appreciate any help anyone can offer.
    > >
    > > Matt
    >
    > My guess is that you are databinding again on postback.
    > This causes your server to lose the state of your list.
    >
    > Try changing your Page_Load event in something like this:
    >
    > Sub Page_Load(Sender As Object, E As EventArgs)
    >
    > If Not Page.IsPostBack Then
    > -----Bind your list here-----
    > End If
    >
    > End Sub
    >
    > --
    >
    > Jos Branders
    >
    >
    >

    md Guest

  5. #4

    Default Re: Dropdownlist and SelectedItem in ASP.Net

    "md" <no@spam.sux> wrote in message
    news:eHpNfFyODHA.1364@TK2MSFTNGP10.phx.gbl...
    > Thanks for your reply. I had AutoPostback set to False on the
    dropdownlist.
    > I recoded it with AutoPostback set to true and rebound only when
    IsPostBack
    > was false, and now it works. Thanks for your help, but I don't really
    > understand why it has to post back to the server just to change the
    selected
    > item in the dropdown, but apparently it does.
    This is because you want to read the SelectedItem.Value when you
    click on a button.
    Obviously this has to be done on the server, and therefore you need a
    postback.
    Since the button also causes a postback, I think you don't even need
    the AutoPostback setting.

    --

    Jos Branders


    Jos Branders Guest

  6. #5

    Default Re: Dropdownlist and SelectedItem in ASP.Net

    You don't need to set the AutoPostBack to true for the DropDownList to get
    the selected item.

    If you aren't rebinding the data to the list box. On the button_click
    event on the server, the SelectedItem.Value property will hold the first
    selected item (in case it is a multiselect).

    Setting AutoPostBack to true, just adds an extra round trip to the server.

    If you want, post a little bit of code if you need more help.

    bill

    "Jos Branders" <josnospambranders@fastmail.fm> wrote in message
    news:3ef9ac94$0$1051$ba620e4c@reader1.news.skynet. be...
    > "md" <no@spam.sux> wrote in message
    > news:eHpNfFyODHA.1364@TK2MSFTNGP10.phx.gbl...
    > > Thanks for your reply. I had AutoPostback set to False on the
    > dropdownlist.
    > > I recoded it with AutoPostback set to true and rebound only when
    > IsPostBack
    > > was false, and now it works. Thanks for your help, but I don't really
    > > understand why it has to post back to the server just to change the
    > selected
    > > item in the dropdown, but apparently it does.
    >
    > This is because you want to read the SelectedItem.Value when you
    > click on a button.
    > Obviously this has to be done on the server, and therefore you need a
    > postback.
    > Since the button also causes a postback, I think you don't even need
    > the AutoPostback setting.
    >
    > --
    >
    > Jos Branders
    >
    >

    William F. Robertson, Jr. Guest

  7. #6

    Default Re: Dropdownlist and SelectedItem in ASP.Net

    Hi Bill -

    Here's the code I had before that didn't seem to work.

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    lblHeading.Text = "Edit vendor information for " &
    GetServerNameFromId(CInt(Request.Params.Item("id") ))
    LoadVendorList()
    LoadVendorTypeList()
    End Sub

    Private Sub LoadVendorList()
    Dim SQL As String = "SELECT * FROM vendor ORDER BY CompanyName"
    Dim Conn As New SqlClient.SqlDataAdapter(SQL,
    Global.sipedb_connection_string)
    Dim Tbl As New DataTable()

    Conn.Fill(Tbl)

    cmbVendorList.DataSource = Tbl
    cmbVendorList.DataBind()
    End Sub

    Private Sub LoadVendorTypeList()
    Dim SQL As String = "SELECT * FROM vendortypes"
    Dim Tbl As New DataTable()
    Dim Conn As New SqlClient.SqlDataAdapter(SQL,
    Global.sipedb_connection_string)

    Conn.Fill(Tbl)

    cmbVendorTypeList.DataSource = Tbl
    cmbVendorTypeList.DataBind()
    End Sub

    Then at this point the page is displayed. Then I would use cmbVendorList to
    let the user select a vendor and click an Add button with this code:

    Private Sub cmdAddExisting_Click(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles cmdAddExisting.Click
    Dim SQL As String = "SELECT * FROM Vendors"
    Dim Conn As New SqlClient.SqlDataAdapter(SQL,
    Global.sipedb_connection_string)
    Dim CommBuilder As New SqlClient.SqlCommandBuilder(Conn)
    Dim NewRecord As DataRow
    Dim Tbl As New DataTable()

    Conn.Fill(Tbl)

    NewRecord = Tbl.NewRow

    NewRecord.BeginEdit()
    NewRecord.Item("server_id") = CInt(Request.Params.Item("id"))
    NewRecord.Item("vendor_id") = cmbVendorList.SelectedItem.Value
    NewRecord.EndEdit()

    Tbl.Rows.Add(NewRecord)

    Conn.Update(Tbl)
    BindVendorsData()
    End Sub

    The BindVendorsData routine binds a table to a datagrid to display the
    vendor that was just added.

    The line that reads NewRecord.Item("vendor_id") =
    cmbVendorList.SelectedItem.Value always returned the value for the first
    item in the dropdown list, even if I had selected a different one. Using
    Jos's idea makes it work, I just didn't really understand why it needed to
    post back to the server.

    Thanks for taking a look.

    Matt

    "William F. Robertson, Jr." <wfrobertson@kpmg.com> wrote in message
    news:%23PaWaLzODHA.1908@TK2MSFTNGP11.phx.gbl...
    > You don't need to set the AutoPostBack to true for the DropDownList to get
    > the selected item.
    >
    > If you aren't rebinding the data to the list box. On the button_click
    > event on the server, the SelectedItem.Value property will hold the first
    > selected item (in case it is a multiselect).
    >
    > Setting AutoPostBack to true, just adds an extra round trip to the server.
    >
    > If you want, post a little bit of code if you need more help.
    >
    > bill
    >
    > "Jos Branders" <josnospambranders@fastmail.fm> wrote in message
    > news:3ef9ac94$0$1051$ba620e4c@reader1.news.skynet. be...
    > > "md" <no@spam.sux> wrote in message
    > > news:eHpNfFyODHA.1364@TK2MSFTNGP10.phx.gbl...
    > > > Thanks for your reply. I had AutoPostback set to False on the
    > > dropdownlist.
    > > > I recoded it with AutoPostback set to true and rebound only when
    > > IsPostBack
    > > > was false, and now it works. Thanks for your help, but I don't really
    > > > understand why it has to post back to the server just to change the
    > > selected
    > > > item in the dropdown, but apparently it does.
    > >
    > > This is because you want to read the SelectedItem.Value when you
    > > click on a button.
    > > Obviously this has to be done on the server, and therefore you need a
    > > postback.
    > > Since the button also causes a postback, I think you don't even need
    > > the AutoPostback setting.
    > >
    > > --
    > >
    > > Jos Branders
    > >
    > >
    >
    >

    md Guest

  8. #7

    Default Re: Dropdownlist and SelectedItem in ASP.Net

    By the way, the DataTextField and DataValueField properties are set in the
    VS.Net propertieswindow.

    "md" <no@spam.sux> wrote in message
    news:e7I%23GS0ODHA.2316@TK2MSFTNGP11.phx.gbl...
    > Hi Bill -
    >
    > Here's the code I had before that didn't seem to work.
    >
    > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles MyBase.Load
    > lblHeading.Text = "Edit vendor information for " &
    > GetServerNameFromId(CInt(Request.Params.Item("id") ))
    > LoadVendorList()
    > LoadVendorTypeList()
    > End Sub
    >
    > Private Sub LoadVendorList()
    > Dim SQL As String = "SELECT * FROM vendor ORDER BY CompanyName"
    > Dim Conn As New SqlClient.SqlDataAdapter(SQL,
    > Global.sipedb_connection_string)
    > Dim Tbl As New DataTable()
    >
    > Conn.Fill(Tbl)
    >
    > cmbVendorList.DataSource = Tbl
    > cmbVendorList.DataBind()
    > End Sub
    >
    > Private Sub LoadVendorTypeList()
    > Dim SQL As String = "SELECT * FROM vendortypes"
    > Dim Tbl As New DataTable()
    > Dim Conn As New SqlClient.SqlDataAdapter(SQL,
    > Global.sipedb_connection_string)
    >
    > Conn.Fill(Tbl)
    >
    > cmbVendorTypeList.DataSource = Tbl
    > cmbVendorTypeList.DataBind()
    > End Sub
    >
    > Then at this point the page is displayed. Then I would use cmbVendorList
    to
    > let the user select a vendor and click an Add button with this code:
    >
    > Private Sub cmdAddExisting_Click(ByVal sender As Object, ByVal e As
    > System.EventArgs) Handles cmdAddExisting.Click
    > Dim SQL As String = "SELECT * FROM Vendors"
    > Dim Conn As New SqlClient.SqlDataAdapter(SQL,
    > Global.sipedb_connection_string)
    > Dim CommBuilder As New SqlClient.SqlCommandBuilder(Conn)
    > Dim NewRecord As DataRow
    > Dim Tbl As New DataTable()
    >
    > Conn.Fill(Tbl)
    >
    > NewRecord = Tbl.NewRow
    >
    > NewRecord.BeginEdit()
    > NewRecord.Item("server_id") = CInt(Request.Params.Item("id"))
    > NewRecord.Item("vendor_id") = cmbVendorList.SelectedItem.Value
    > NewRecord.EndEdit()
    >
    > Tbl.Rows.Add(NewRecord)
    >
    > Conn.Update(Tbl)
    > BindVendorsData()
    > End Sub
    >
    > The BindVendorsData routine binds a table to a datagrid to display the
    > vendor that was just added.
    >
    > The line that reads NewRecord.Item("vendor_id") =
    > cmbVendorList.SelectedItem.Value always returned the value for the first
    > item in the dropdown list, even if I had selected a different one. Using
    > Jos's idea makes it work, I just didn't really understand why it needed to
    > post back to the server.
    >
    > Thanks for taking a look.
    >
    > Matt
    >
    > "William F. Robertson, Jr." <wfrobertson@kpmg.com> wrote in message
    > news:%23PaWaLzODHA.1908@TK2MSFTNGP11.phx.gbl...
    >
    > > You don't need to set the AutoPostBack to true for the DropDownList to
    get
    > > the selected item.
    > >
    > > If you aren't rebinding the data to the list box. On the button_click
    > > event on the server, the SelectedItem.Value property will hold the first
    > > selected item (in case it is a multiselect).
    > >
    > > Setting AutoPostBack to true, just adds an extra round trip to the
    server.
    > >
    > > If you want, post a little bit of code if you need more help.
    > >
    > > bill
    > >
    > > "Jos Branders" <josnospambranders@fastmail.fm> wrote in message
    > > news:3ef9ac94$0$1051$ba620e4c@reader1.news.skynet. be...
    > > > "md" <no@spam.sux> wrote in message
    > > > news:eHpNfFyODHA.1364@TK2MSFTNGP10.phx.gbl...
    > > > > Thanks for your reply. I had AutoPostback set to False on the
    > > > dropdownlist.
    > > > > I recoded it with AutoPostback set to true and rebound only when
    > > > IsPostBack
    > > > > was false, and now it works. Thanks for your help, but I don't
    really
    > > > > understand why it has to post back to the server just to change the
    > > > selected
    > > > > item in the dropdown, but apparently it does.
    > > >
    > > > This is because you want to read the SelectedItem.Value when you
    > > > click on a button.
    > > > Obviously this has to be done on the server, and therefore you need a
    > > > postback.
    > > > Since the button also causes a postback, I think you don't even need
    > > > the AutoPostback setting.
    > > >
    > > > --
    > > >
    > > > Jos Branders
    > > >
    > > >
    > >
    > >
    >
    >

    md Guest

  9. #8

    Default Re: Dropdownlist and SelectedItem in ASP.Net

    My comments are c style comments -> //

    Here is the new Page_Load

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load

    lblHeading.Text = "Edit vendor information for " &
    GetServerNameFromId(CInt(Request.Params.Item("id") ))

    //if the Request.Params doesn't change, then I would move it to inside
    the following if statement.

    if not Page.IsPostBack then
    LoadVendorList()
    LoadVendorTypeList()
    endif
    End Sub

    By putting the Load functions in a NOT postback if statement, the data will
    be only be rebound to your list boxes once. That way when you access the
    SelectedItem property it will have what was selected. When you rebind the
    DropDownList boxes on every page_load you are replacing the state of the
    list boxes, and thus, the control doesn't know what was selected since all
    the data is "new".

    Page_Load runs every single time anything goes to the page. If there is an
    autopostback, button click, refresh, it doesn't matter, the page_load will
    run, so if you are ever going to be binding to a control, and the DATA (not
    selection) is static (not changing) I would highly recommend protecting the
    binding code with the Page.IsPostBack.

    Oh, I am sure you have EnableViewState to true on both of the list boxes?

    If you have any other questions, I will be checking this thread for the next
    couple days.

    HTH,

    bill


    "md" <no@spam.sux> wrote in message
    news:e7I#GS0ODHA.2316@TK2MSFTNGP11.phx.gbl...
    > Hi Bill -
    >
    > Here's the code I had before that didn't seem to work.
    >
    > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles MyBase.Load
    > lblHeading.Text = "Edit vendor information for " &
    > GetServerNameFromId(CInt(Request.Params.Item("id") ))
    > LoadVendorList()
    > LoadVendorTypeList()
    > End Sub
    >
    > Private Sub LoadVendorList()
    > Dim SQL As String = "SELECT * FROM vendor ORDER BY CompanyName"
    > Dim Conn As New SqlClient.SqlDataAdapter(SQL,
    > Global.sipedb_connection_string)
    > Dim Tbl As New DataTable()
    >
    > Conn.Fill(Tbl)
    >
    > cmbVendorList.DataSource = Tbl
    > cmbVendorList.DataBind()
    > End Sub
    >
    > Private Sub LoadVendorTypeList()
    > Dim SQL As String = "SELECT * FROM vendortypes"
    > Dim Tbl As New DataTable()
    > Dim Conn As New SqlClient.SqlDataAdapter(SQL,
    > Global.sipedb_connection_string)
    >
    > Conn.Fill(Tbl)
    >
    > cmbVendorTypeList.DataSource = Tbl
    > cmbVendorTypeList.DataBind()
    > End Sub
    >
    > Then at this point the page is displayed. Then I would use cmbVendorList
    to
    > let the user select a vendor and click an Add button with this code:
    >
    > Private Sub cmdAddExisting_Click(ByVal sender As Object, ByVal e As
    > System.EventArgs) Handles cmdAddExisting.Click
    > Dim SQL As String = "SELECT * FROM Vendors"
    > Dim Conn As New SqlClient.SqlDataAdapter(SQL,
    > Global.sipedb_connection_string)
    > Dim CommBuilder As New SqlClient.SqlCommandBuilder(Conn)
    > Dim NewRecord As DataRow
    > Dim Tbl As New DataTable()
    >
    > Conn.Fill(Tbl)
    >
    > NewRecord = Tbl.NewRow
    >
    > NewRecord.BeginEdit()
    > NewRecord.Item("server_id") = CInt(Request.Params.Item("id"))
    > NewRecord.Item("vendor_id") = cmbVendorList.SelectedItem.Value
    > NewRecord.EndEdit()
    >
    > Tbl.Rows.Add(NewRecord)
    >
    > Conn.Update(Tbl)
    > BindVendorsData()
    > End Sub
    >
    > The BindVendorsData routine binds a table to a datagrid to display the
    > vendor that was just added.
    >
    > The line that reads NewRecord.Item("vendor_id") =
    > cmbVendorList.SelectedItem.Value always returned the value for the first
    > item in the dropdown list, even if I had selected a different one. Using
    > Jos's idea makes it work, I just didn't really understand why it needed to
    > post back to the server.
    >
    > Thanks for taking a look.
    >
    > Matt
    >
    > "William F. Robertson, Jr." <wfrobertson@kpmg.com> wrote in message
    > news:%23PaWaLzODHA.1908@TK2MSFTNGP11.phx.gbl...
    >
    > > You don't need to set the AutoPostBack to true for the DropDownList to
    get
    > > the selected item.
    > >
    > > If you aren't rebinding the data to the list box. On the button_click
    > > event on the server, the SelectedItem.Value property will hold the first
    > > selected item (in case it is a multiselect).
    > >
    > > Setting AutoPostBack to true, just adds an extra round trip to the
    server.
    > >
    > > If you want, post a little bit of code if you need more help.
    > >
    > > bill
    > >
    > > "Jos Branders" <josnospambranders@fastmail.fm> wrote in message
    > > news:3ef9ac94$0$1051$ba620e4c@reader1.news.skynet. be...
    > > > "md" <no@spam.sux> wrote in message
    > > > news:eHpNfFyODHA.1364@TK2MSFTNGP10.phx.gbl...
    > > > > Thanks for your reply. I had AutoPostback set to False on the
    > > > dropdownlist.
    > > > > I recoded it with AutoPostback set to true and rebound only when
    > > > IsPostBack
    > > > > was false, and now it works. Thanks for your help, but I don't
    really
    > > > > understand why it has to post back to the server just to change the
    > > > selected
    > > > > item in the dropdown, but apparently it does.
    > > >
    > > > This is because you want to read the SelectedItem.Value when you
    > > > click on a button.
    > > > Obviously this has to be done on the server, and therefore you need a
    > > > postback.
    > > > Since the button also causes a postback, I think you don't even need
    > > > the AutoPostback setting.
    > > >
    > > > --
    > > >
    > > > Jos Branders
    > > >
    > > >
    > >
    > >
    >
    >

    William F. Robertson, Jr. Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139