I have dropdownlist with list item of Yes and No. I want "No" will be displayed on the page but the Selected="true" on the ListItem is not working.
<asp:DropDownList ID="ddlIsDistributor" runat="server">
<asp:ListItem Value="1" Text="Yes">Yes</asp:ListItem>
<asp:ListItem Value="0" Text="No" Selected="True">No</asp:ListItem>
</asp:DropDownList>
Invoke this bit of code in your page load event so it selects the desired value from the dropdown when loading: ddlIsDistributor.SelectedIndex = 0;
Related
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.
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.
I'm wondering if it is possible, using an asp:dropdownlist if you can fire a postback on every selection, even if it is the same? For example:
<asp:DropDownList ID="DropDownList_SortCars" runat="server" OnSelectedIndexChanged="DropDownList_SortCars">
<asp:ListItem Value="0" Text="Volvo" ></asp:ListItem>
<asp:ListItem Value="1" Text="Saab"></asp:ListItem>
<asp:ListItem Value="2" Text="Audi"></asp:ListItem>
</asp:DropDownList>
So rather than having a 'OnSelectedindexChanged' event you would have a IndexSelected event. The reason? Well, if somebody selects a volvo, and this would be a list of models (or something), if they selected volvo again it would show the same list but in reverse order! At the moment I can only get the list to fire if they change an index.
So basically can you force a postback on selection, any selection?
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.
I have a web form in which there are some controls along with dropdown list as below:
<asp:DropDownList ID="ddlrange" CssClass="dropdownstate" style="position:absolute;top:224px; left:200px;" runat="server" >
<asp:ListItem >Select Range</asp:ListItem>
<asp:ListItem Value="A" >A</asp:ListItem>
<asp:ListItem Value="B" >B</asp:ListItem>
<asp:ListItem Value="C" >C</asp:ListItem>
<asp:ListItem Value="D" >D</asp:ListItem>
</asp:DropDownList>
and theres a gridview in which data is shown on btninsert click.
and I have a link button EDIT. on clicking edit the corresponding values are displayed in their respective controls.
Now, coming to the point,insert works fine, but when i click on Edit link every thing is fine except the value in dropdown list.
it should be like as shown in screenshot below:
but what I get is shown below:
how can I overcome this???
Try giving value to the Select Range list item and then chcek, may be that helps.
Maybe this is helpful for you
<asp:DropDownList ID="ddlrange" CssClass="dropdownstate" style="position:absolute;top:224px; left:200px;" runat="server" >
<asp:ListItem></asp:ListItem>
<asp:ListItem Value="" >Select Range</asp:ListItem>
<asp:ListItem Value="A" >A</asp:ListItem>
<asp:ListItem Value="B" >B</asp:ListItem>
<asp:ListItem Value="C" >C</asp:ListItem>
<asp:ListItem Value="D" >D</asp:ListItem>
</asp:DropDownList>
In edit click event add a "Select Range" value into the drop down.
ddlRange.Items.Insert (0,"Select Range");
Add <asp:ListItem Value=null>Select Range</asp:ListItem> line.
Try Below:
<asp:DropDownList ID="ddlrange" CssClass="dropdownstate" style="position:absolute;top:224px; left:200px;" runat="server" >
<asp:ListItem Value="Select Range">Select Range</asp:ListItem>
<asp:ListItem Value="A" >A</asp:ListItem>
<asp:ListItem Value="B" >B</asp:ListItem>
<asp:ListItem Value="C" >C</asp:ListItem>
<asp:ListItem Value="D" >D</asp:ListItem>
</asp:DropDownList>
I tried using ddlrange.SelectedValue instead of ddlrange.SelectedItem.Text, and issue is solved.
Thanks everyone for your efforts, I appreciate.