0

I don't know if this is possible -- Googling has turned up nothing. But I would like to have a DropDownList display the value for its selected index on selection change.

Any advice is appreciated.

Regards.

Filburt
  • 17,626
  • 12
  • 64
  • 115
Kevin
  • 1,252
  • 6
  • 32
  • 50
  • 1
    What are you using? WinForms? XAML? WebForms? MVC? – Ethan Brown Jul 13 '12 at 19:20
  • Have you tried http://stackoverflow.com/questions/5956236/dropdownlist-selectedvalue – deostroll Jul 13 '12 at 19:21
  • I am using Webforms. I would like the DDL to remain a DDL where, instead of the selected index, the selected value is displayed. The indices are text and the values are images and I would like to be able to see the image in the DDL while still being able to choose a different index. – Kevin Jul 13 '12 at 19:23

3 Answers3

1

I think you are mixing up the vocabulary a bit. A regular drop down contains "items".

In plain html, a drop down is rendered as a <select ..> element with <option ..> elements representing the items.

In Asp.Net, the corresponding control is called DropDownList and it contains ListItem child elements. ListItems has properties Text and Value, where the Text property is what is shown in the UI and the Value property is what is posted back to the server.

For the parent DropDownList, SelectedValue is the Value attribute from the selected ListItem. SelectedIndex on the othe hand is the zero based numerical index of the selected ListItem among all the ListItems in the dropdown.

Vocabulary lesson out of the way, you seem to want to have a drop down where instead of showing text, an image would be shown in the UI and the user could then select the correct image. The value property of the dropdown would contain some sort of ID for the selected image (maybe an image file name, or a key from a database table or something like that).

To get you on you way with showing images in the dropdown, please have a look at the accepted answer to this question:

Images in dropdown list

Community
  • 1
  • 1
user1429080
  • 9,086
  • 4
  • 31
  • 54
0

Value cannot be retreived, unless if you havent proved it when setting datasouce. You must proved DisplayMember (the items displayed) and ValueMember property to retreive value of selected item. If you didnt do so, you cannot get Vlaue (or selectedValue) of selected item.

Mitja Bonca
  • 4,268
  • 5
  • 24
  • 30
0

Here is the dropdown:

@Html.DropDownListFor(model => model.CountyID, new SelectList(Model.allCounties, "CountyID", "CountyName"), "Select.....", new { @class = "form-control", @id = "CountyID", @onchange = "DisplayCountyInfo();" })




<div id="CountyView">
  @Html.Partial("_CountyView", Model._countyModel)
</div>

<script>
function DisplayCountyInfo() {
    $("#CountyView").load('@(Url.Action("_CountyView", "Controller", null, Request.Url.Scheme))?id=' + $("#CountyID").val());
};
</script>

Create a partial view

@model Application.Web.ViewModels.CountyViewModel 

<h4><b>@Model.CountyName</b></h4>
<a href="@Model.URL" target="_blank">@Model.URL</a>

Controller

[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
    public ActionResult _CountyView(int id)
    {
        var county = _countyService.Get(id);
        CountyViewModel viewModel = new CountyViewModel()
        {
            CountyName = county.CountyName,
            URL = county.URL
        };
        return PartialView("_CountyView", viewModel);
    }
JoshYates1980
  • 3,476
  • 2
  • 36
  • 57