Add item to databound DropDownList - c#

I have dropdownlist control where item lists are coming from database
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
DataSourceID="SqlDataSource2" DataTextField="semester"
DataValueField="semester">
</asp:DropDownList>
But I want to add at the beginning 1 list item more "ALL" .. How can I add this one .
Thanks !

To add a new list item to the DropDownList, in the Properties window, click on the ellipses in the Items property.
Add a new list item with Text "ALL" & Value -1.
Or you can add the list item by adding this markup to the DropDownList:
<asp:DropDownList ID="categories" runat="server" ...>
<asp:ListItem Value="-1">
ALL
</asp:ListItem>
</asp:DropDownList>
Set the DropDownList's AppendDataBoundItems=True

With Items.Insert method you can add an item at a specific index :
DropDownList1.Items.Insert(0, new ListItem("ALL", "ALL"));

Be sure to disable the viewstate of DropDownList1 so that it doesn't retain each resultset from the db!

Related

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"

Adding an element to a drop down list which is binded to a database initially

I am coding in ASP.net using C# in Microsoft Visual Studio. I have a dropdown list that I bind using the smart tag under the "Choose Data Source" on the Default.aspx page.
I have binded it to an ACCESS database. It currently has this data: 2009, 2010, 2011, 2012, 2013. I would like to find out a way to insert a word programmatically before the data item 2009 as "Select". So the data items are as follows: Select,2009,2010,2011,2012,2013.
When ever I do this DropDownList1.Items.Add(new ListItem("Please Select", "1")); It keeps adding the "Please Select" keyword every time a user selects an item from the drop down list.
I don't want that to happen.
Thanks in advance
use !IsPostback on page_load to add element to overcome this issue as follows
if (!Page.IsPostBack)
{
DropDownList1.Items.Add(new ListItem("Please Select", "1"));
}
or
<asp:DropDownList ID="DropDownList1" AppendDataBoundItems="true" runat="server"
DataSourceID="SqlDataSource1" DataTextField="state" DataValueField="state">
<asp:ListItem Text="(Select a State)" Value="" />
</asp:DropDownList>
if (!Page.IsPostBack)
{
DropDownList1.Items.Insert(0, "Please Select");
}
and set AppendDataBoundItems="true"
<asp:DropDownList ID="DropDownList1" AppendDataBoundItems="true" runat="server"
DataSourceID="SqlDataSource1" DataTextField="state" DataValueField="state">
</asp:DropDownList>

How to make dropdownlist item's unselectable,once it has been selected

<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
I have one dropdownlist in my form in .aspx File.
Once I select "1", then next time if I redirect to same page then "1" should be
unselectable or Hidden for making sense that I have selected "1" previously because I have large items in my example.
Disabling any particular item in a dropdownlist is not possible.
Alternate:
You can use a BulletedList Server Control and use its Enable = False property to disable any particular item and all user can see that item as disabled.. here is a design time example..
<asp:BulletedList ID="BulletedList1" runat="server">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem Enabled="False">3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
</asp:BulletedList>
Took from here
You can use a hidden field to store which item was selected in code behind and disable that item using the following code.
//Code Starts
$(document).ready(function() {
$('#ddlList option:contains("HTML")').attr("disabled","disabled");
});​
//Code Ends
You can track your selection using Cookies,
on the "onChange" event of your dropdown list, create a cookie with the selected value,
then on body "onLoad" u can check and disable the items that are there in Cookie.
Or you can hold your selected values in a hidden field
To remove the last selected item in your dropdownlist (DDL), considering the following code for your DDL form, you can use the onselectedindexchanged event of your DDL (that is raised when you click on one of your DDL items):
<asp:DropDownList ID="_DDL" runat="server"
onselectedindexchanged="_DDL_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
</asp:DropDownList>
And put on the code behind the RemoveAt() method to remove the selected Item from your DDL:
protected void _DDL_SelectedIndexChanged(object sender, EventArgs e)
{
int ItemToRemove = _DDL.SelectedIndex;
_DDL.Items.RemoveAt(ItemToRemove);
}
Hope this will be helpful :)

Databound DropDownList not selecting correct item

I have a DropDownList whose SelectedValue and DataSource are both databound. The control always selects the first item in the list regardless of the SelectedValue. The correct value is passed to the database when updating the value but the first item is always selected. What am I missing here?
<asp:DropDownList ID="SendAsDdl" runat="server"
SelectedValue='<%# Bind("SendAsId") %>' EnableViewState="true"
DataSource='<%# CM.Email.Users.GetSendAsList(OfficeId) %' />
You can't put scriplets into server side controls. You have to set the SelectedValue from your code behind:
SendAsDdl.SelectedValue = this.SendAsId;

Categories

Resources