Setting Item Value property and Control Text

3 posts, 0 answers
  1. M Patrick
    M Patrick avatar
    6 posts
    Member since:
    May 2012

    Posted 18 Jun 2012 Link to this post

    I am manually loading a combobox during a postback with:
    Do While CoReader.Read
      If CoReader.IsDBNull(0) Then StateNameString = "" Else StateNameString = CoReader.Item("StateName").ToString.Trim
      Dim item As New RadComboBoxItem(StateNameString)
      Me.RadComboBoxState.Items.Add(item)
    Loop

    As I add an item, how do I set/add its value? No sooner that I posted this, than I think I found the answer. See below. If it is not correct please let me know.
    Do While CoReader.Read
      If CoReader.IsDBNull(0) Then StateNameString = "" Else StateNameString = CoReader.Item("StateName").ToString.Trim
      Dim item As New RadComboBoxItem(StateNameString)
      If CoReader.IsDBNull(1) Then item.Value = "" Else item.Value = CoReader.Item("State").ToString.Trim.ToUpper
      Me.RadComboBoxState.Items.Add(item)
    Loop


    Once the entire contol is populated by the above code, I would like to set the Text property of the control to the user's setting. Let's say the user has a Text value of "Maine" and a Value of "ME".. My code is not setting the TEXT property during postback.
    If CoReader.IsDBNull(0) Then Me.RadComboBoxState.Text = "" Else RadComboBoxState.Text = CoReader.Item("StateName").ToString.Trim

    What is the correct way to set the TEXT property during a Postback?

    Finally and this is a programing best practices question, do you think I should set the SelectedItem and/or SelectedValue property of the control when it is known, as in the above example? Or is just populating the TEXT property field sufficient?

    Thanks,
    pat

  2. Kalina
    Admin
    Kalina avatar
    918 posts

    Posted 22 Jun 2012 Link to this post

    Hello M Patrick,

    Please take a look at the "Working with Items in Server Side Code" help article.

    Regards,
    Kalina
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  3. M Patrick
    M Patrick avatar
    6 posts
    Member since:
    May 2012

    Posted 22 Jun 2012 Link to this post

    Thank you for the link. It describes exactly what I need. I have a slight modification which I have shown below.
    For an example: A state list with the state name as the Text value and the 2 letter State abbreviation as the Value. I store the 2 letter abbreviation in the database but want the user to select the full state text name.
    To set the SelectedIndex property which will set the Text property of the control to the selected index's text value if the item  "value" are unique and known, try:
    Dim item As RadComboBoxItem = Nothing
    Dim StateAbbrevString as string = string.empty
                   Dim index As Integer = -1
                   'Get a reference to a RadComboBoxItem with Text property = "Pennsylvania".  
                   item = RadComboBox1.FindItemByText("Pennsylvania")
                   StateAbbrevString = Item.Value.tostring.trim

    My country list is similar except the item Value is not unique. e.g. The Country abbreviation, US, has several common country name. The include, United States, Virgin islands and several others. When I want to display the common name which was recalled from the database, I use the code below.
    Dim item As RadComboBoxItem = Nothing
    Dim index As Integer = -1
    'Get a reference to a RadComboBoxItem with Text property = "United States".  
     item = RadComboBox1.FindItemByText("United States")
    RadCombobox1.SelectedIndex = Item.Index

    This was not obvious to me, as the intelisense for Selectedindex states that this property can be used for Getting the value. It does not state that it can be used to Set the value. But it definately can be used for setting the property.

    Thanks again,
    pat

Back to Top