Silvrdragon
Silvrdragon

Reputation: 43

Aspx dropdownlist not displaying top value

I have an aspx page (C# code page). I have some hard coded dropdownlists and for some reason they are not displaying the top list item (value). I added an extra top list item (value) and now the correct values display for the values already in it, but that extra one does not display.

The only functionality I do with my dropdownlists in my C# code is to hide or show them. And then do validation or not based on the selected value.

My aspx code:

<asp:DropDownList ID="ddlAction" runat="server" Visible="True" 
    AppendDataBoundItems="true" Height="25px" Width="149px">
    <asp:ListItem Value="Select">Please Select</asp:ListItem>
    <asp:ListItem>Yes</asp:ListItem>
    <asp:ListItem>No</asp:ListItem>
</asp:DropDownList>

C# Code:

ddlAction.Visible = false;
ddlAction.Visible = true;

I use dropdownlist's regularly and have never had this problem before. Does anyone have any ideas what the issue could be?

UPDATE TO THIS ISSUE:

I added my items in my C# code as per Rahul. Did a quick test and it worked. Now this morning, I am once again getting blanks for the first item ("Please Select").

Aspx code:

<asp:DropDownList ID="ddlAction" runat="server" 
 AppendDataBoundItems="True" Height="27px" Width="159px">
 </asp:DropDownList>

C# code:

ddlAction.Visible = true;
ddlAction.AppendDataBoundItems = true;
ddlAction.Items.Insert(0, new ListItem("Please Select","Select"));
ddlAction.Items.Insert(1, new ListItem("Yes", "Yes"));
ddlAction.Items.Insert(2, new ListItem("No", "No"));
ddlAction.DataBind();

Rendered source code:

  &nbsp;<select name="ctl00$ContentPlaceHolder1$ddlAction" id="ContentPlaceHolder1_ddlAction" style="height:27px;width:159px;">
<option selected="selected" value="Select"></option>
<option value="Yes">Yes</option>
<option value="No">No</option>

Upvotes: 4

Views: 4154

Answers (4)

aledpardo
aledpardo

Reputation: 761

I think that you don't have to use nor the DataBind() method neither set the AppendDataBoundItems, because, you already inserted the ListItems and you aren't loading options from a database!

I think you need to tell what ListItemIndex is the selected one by seting a value to the DropDownList.SelectedIndex property.

EDIT

Also, try to read to MSDN documentation about the AppendDataBoundItems property and the enter link description here method.

Upvotes: 1

aledpardo
aledpardo

Reputation: 761

I suggest you declare your DropDownList ListItems using its internal properties and defining what ListItem must be the selected one:

    <asp:DropDownList ID="ddlAction" runat="server" Visible="True" AppendDataBoundItems="true" Height="25px" Width="149px">
        <asp:ListItem Text="Please Select" Value="Select" Selected="True"></asp:ListItem>
        <asp:ListItem Text="Yes" Value="Yes"></asp:ListItem>
        <asp:ListItem Text="No" Value="No"</asp:ListItem>
     </asp:DropDownList>

It's the way ASP.NET uses to work and will return you the right selected value on the server side on postbacks.

Upvotes: 1

Rahul
Rahul

Reputation: 5636

Try to use AppendDataBoundItems = true property of DropSownList into your .aspx page.

you may also assign value from code behind as well like

ddlAction.Items.Insert(0, new ListItem(String.Empty, String.Empty));

Upvotes: 1

शेखर
शेखर

Reputation: 17614

use AppendDataBound = true in your aspx coe.

<asp:DropDownList ID="ddlAction" AppendDataBound = true runat="server" Visible="True" Height="25px" 
                            Width="149px">
                            <asp:ListItem Value="Select">Please Select</asp:ListItem>
                            <asp:ListItem>Yes</asp:ListItem>
                            <asp:ListItem>No</asp:ListItem>
                        </asp:DropDownList>

Edit 1

More detail about List Item

 <asp:ListItem Value="-2" Text="Please Select"></asp:ListItem>
 <asp:ListItem Value="0" Text="Yes"></asp:ListItem>
 <asp:ListItem Value="-1" Text="No"></asp:ListItem>

Upvotes: 1

Related Questions