Aspx dropdownlist not displaying top value - c#

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:
<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>

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));

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>

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.

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.

Related

Is there a way to make an item in a DropDownList not selectable?

I have an ASP.net dropdownlist with four items,
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Selected="True" Value="0">Select Payment Method</asp:ListItem>
<asp:ListItem>Credit Card</asp:ListItem>
<asp:ListItem>PayPal</asp:ListItem>
<asp:ListItem>WeKea Store Gift Card</asp:ListItem>
</asp:DropDownList>
Is there a way to make "Select Payment Method" not selectable? I read through all the properties of an ASP.net DDL and I dont see anything of the sort.
You can use the disabled attribute (see here) and apply it to the ListItem:
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Selected="True" Value="0" disabled="disabled">Select Payment Method</asp:ListItem>
<asp:ListItem>Credit Card</asp:ListItem>
<asp:ListItem>PayPal</asp:ListItem>
<asp:ListItem>WeKea Store Gift Card</asp:ListItem>
</asp:DropDownList>
This will render the option as greyed out and it will not be selectable. However, the selected=True will override that and it will be displayed as selected when the page loads, which I think is what you want.

stop autopost back on default value of dropdownlist asp.net

I have a dropdownlist(autopostback enabled) in my project in binded through sqldatasource, i have added an initial value to it using list item. The problem is the page is being postbacked even when selecting the initial value which i dont want. Is there a way to achieve this?
<asp:DropDownList ID="DropDownListTheme" runat="server"
DataSourceID="SqlDataSourceTheme" DataTextField="h_theme"
DataValueField="h_theme" Height="30px" Width="30%"
AppendDataBoundItems="true" AutoPostBack="True">
<asp:ListItem Text="--Select One--" Value="-1" />
</asp:DropDownList>
Set the initial item as selected initially
<asp:ListItem Text="--Select One--" Selected="True" Value="-1" />
Add
onchange="if(this.selectedIndex == 0)return false;"
event to drop down list.

asp dropdown reload page when setting dropdown.selectedvalue

I have the following asp dropdown:
<asp:DropDownList ID="ArticleCategories" runat="server" AppendDataBoundItems="true" CssClass="ArticleCategoriesDropDown" onselectedindexchanged="mCategoryItemSelected" AutoPostBack="True">
<asp:ListItem Text="Select Category" Value="" />
</asp:DropDownList>
Once a selection has been made, and the data loaded, I want to set the value in the dropdown to display the value chosen by the user.
I am doing this by:
ArticleCategories.SelectedValue = CategoryID.ToString();
This works fine the first time, but from then the page is stuck on that selection, as the selected value gets loaded before the new value can load.
I have tried disabling the itemchangelistener by:
ArticleCategories.SelectedIndexChanged += new EventHandler(doNothing);
But that does not work.
I have also tried to disable postback which also does not work.
Any ideas?
You should only set the selection when loading the page the first time. In ASP.NET, you can achieve this by checking the IsPostBack property:
if(!IsPostBack)
{
ArticleCategories.SelectedValue = CategoryID.ToString();
}
You can do Autopostback= false
<asp:DropDownList ID="ArticleCategories" runat="server" AppendDataBoundItems="true" CssClass="ArticleCategoriesDropDown" onselectedindexchanged="mCategoryItemSelected" AutoPostBack="False">
<asp:ListItem Text="Select Category" Value="" />
</asp:DropDownList>
Hey before selecting another item from Dropdown you should clear previous selection then selected another one.
ArticleCategories.ClearSelection();
Hope it helps you.

How to add the checkbox item at the new line?

I have a checkbox list like below:
<asp:CheckBoxList id="chx" runat="server" repeatdirection="Horizontal"> <asp:ListItem value="OPTION1">Option 1</asp:ListItem>
<asp:ListItem value="OPTION2">Option 2</asp:ListItem>
<asp:ListItem value="OPTION3">Option 3</asp:ListItem><
asp:ListItem value="OPTION4">Option 4</asp:ListItem>
</asp:CheckBoxList>
And I am dynamically adding the list items to this checkbox using the following C# code:
chx.Items.Clear();
while(dr.Read()){
if(dr.GetOracleString(0).ToString()!="Null"){
chx.Items.Add(dr.GetOracleString(0).ToString());
}
}
My requirement is whenever the new item is added in the checkboxlist, it should check whether the new item is going beyond the border(fixed width) of the page and if so the new item should go the next line of checkboxlist without overlapping below control.
Please help me to fix this.
You can use the property RepeatColumns
RepeatColumns="3"

affecting existing RadioButtonList to set a default value

I have an existing RadioButtonList on a page and need to set the second button to be checked as default instead of the first.
I probably need to do it with javascript on the page as I cannot edit the original control.
<list:RadioButtonList runat="server" Class="class" Text="text"
AlternativeText="alternative text" />
Any idea how i can detect the control and set its default value?
If you use ASP.NET you can set the following:
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
<asp:ListItem Selected="True" ></asp:ListItem>
<asp:ListItem></asp:ListItem>
<asp:ListItem></asp:ListItem>
<asp:ListItem></asp:ListItem>
<asp:ListItem></asp:ListItem>
</asp:RadioButtonList>
I've added the attribute Selected="True", so you always have a default value selected.
You can also do this in code:
if (RadioButtonList1.SelectedIndex == -1) //-1 is the indication of none selected
{
RadioButtonList1.SelectedIndex = 2; //select index 2 (can also be value or text)
}

Categories

Resources