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. SelectedIndexChanged with data bound DropDownList

SelectedIndexChanged with data bound DropDownList

Scheduled Pinned Locked Moved ASP.NET
databasecsharpasp-netbusinesshelp
5 Posts 3 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.
  • A Offline
    A Offline
    Andy M
    wrote on last edited by
    #1

    Hi, I have a DropDownList that is using one of my business objects as the DataSource.

    meetingDateDdl.DataSource = room.Meetings; // == List<MeetingInfo>
    meetingDateDdl.DataBind();

    However, when I get the details in the SelectedIndexChanged event handler the meetingDataDdl.SelectedItem shows the display text for the Text and Value properties. The problem is that another user could updated the data so that the item has a different display text or that someother data could be added or deleted making the index invalid which means I cannot use SelectedIndex. What I want is to put the unique Id, which is just the primary key from the database, into the DropDownList somehow so that when I check the SelectedItem I can see from the Value what the unique id is so I can find the correct business object again. Any ideas? BTW, I'm using ASP.NET 2.0

    P M 2 Replies Last reply
    0
    • A Andy M

      Hi, I have a DropDownList that is using one of my business objects as the DataSource.

      meetingDateDdl.DataSource = room.Meetings; // == List<MeetingInfo>
      meetingDateDdl.DataBind();

      However, when I get the details in the SelectedIndexChanged event handler the meetingDataDdl.SelectedItem shows the display text for the Text and Value properties. The problem is that another user could updated the data so that the item has a different display text or that someother data could be added or deleted making the index invalid which means I cannot use SelectedIndex. What I want is to put the unique Id, which is just the primary key from the database, into the DropDownList somehow so that when I check the SelectedItem I can see from the Value what the unique id is so I can find the correct business object again. Any ideas? BTW, I'm using ASP.NET 2.0

      P Offline
      P Offline
      Pankaj Kulkarni
      wrote on last edited by
      #2

      [Message Deleted]

      A 1 Reply Last reply
      0
      • P Pankaj Kulkarni

        [Message Deleted]

        A Offline
        A Offline
        Andy M
        wrote on last edited by
        #3

        Pankaj Kulkarni wrote:

        Because u haven't stated the use of ur DropDownList.

        I don't understand what you mean by that. My use of the DropDownList is to display a list of existing meeting date/times that come from a MeetingInfo object.

        Pankaj Kulkarni wrote:

        Asssuming that using for the Storing Data to Database

        The UI knows nothing about the database. The UI is binding to the business objects, in this case MeetingInfo.

        class MeetingInfo
        {
        public int MeetingID{ get; set; } // This is what I want as the value
        public string Description { get; set; } // This is what I want displayed to the user
        }

        Pankaj Kulkarni wrote:

        dd.SelectedIndex = dd.IndexOf(dd.FindByValue(Variable))

        That won't work for me as it is assigning the selected index. The item is already selected, I want to be able to find out the exact MeetingInfo object that was used to generate the item. Therefore, I want to be able to assign to each DropDownList item the MeetingID while displaying to the user the Description. Does this make more sense that my original post? Cheers, Andy.

        1 Reply Last reply
        0
        • A Andy M

          Hi, I have a DropDownList that is using one of my business objects as the DataSource.

          meetingDateDdl.DataSource = room.Meetings; // == List<MeetingInfo>
          meetingDateDdl.DataBind();

          However, when I get the details in the SelectedIndexChanged event handler the meetingDataDdl.SelectedItem shows the display text for the Text and Value properties. The problem is that another user could updated the data so that the item has a different display text or that someother data could be added or deleted making the index invalid which means I cannot use SelectedIndex. What I want is to put the unique Id, which is just the primary key from the database, into the DropDownList somehow so that when I check the SelectedItem I can see from the Value what the unique id is so I can find the correct business object again. Any ideas? BTW, I'm using ASP.NET 2.0

          M Offline
          M Offline
          MihirV
          wrote on last edited by
          #4

          hi there you should bind DropDown control like this...

          meetingDateDdl.DataSource = room.Meetings;
          meetingDateDdl.DataTextField = "MeetingID";
          meetingDateDdl.DataValueField = "Description";
          meetingDateDdl.DataBind();
          
          //the following line gives u the exact selected MeetingID
          string selectedMeetingID = meetingDateDdl.SelectedValue;
          

          :)


          Confidence comes not from always being right, but from not fearing to be wrong. Mihir..

          A 1 Reply Last reply
          0
          • M MihirV

            hi there you should bind DropDown control like this...

            meetingDateDdl.DataSource = room.Meetings;
            meetingDateDdl.DataTextField = "MeetingID";
            meetingDateDdl.DataValueField = "Description";
            meetingDateDdl.DataBind();
            
            //the following line gives u the exact selected MeetingID
            string selectedMeetingID = meetingDateDdl.SelectedValue;
            

            :)


            Confidence comes not from always being right, but from not fearing to be wrong. Mihir..

            A Offline
            A Offline
            Andy M
            wrote on last edited by
            #5

            Yes. That is exactly what I was looking for. And I cannot believe I missed it. It was right there in the Intellisense thingy next to the other methods I was using. :doh: I'm obviously having a really stupid day. Many thanks. Cheers, Andy.

            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