Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to add items to Dropdownlist Permanently,

In design mod i have add 3 items (one, two, three)

but now at run time i want to add (four) to dropdown list permanently

item must be there after reloading page

Note:
Without using any database

This Is my code
C#
List<ListItem> items = new List<ListItem>();
       items.Add(new ListItem("" + TextBox2.Text + "", "Value 2"));

       items.Sort(delegate(ListItem item1, ListItem item2) { return item1.Text.CompareTo(item2.Text); });
       DropDownList3.Items.AddRange(items.ToArray());


guys help
Posted
Updated 1-Aug-13 21:38pm
v3
Comments
Dholakiya Ankit 2-Aug-13 3:24am    
what's the problem?
jackspero18 2-Aug-13 3:28am    
in design mode i have added three names to dropdownlist,
and in runtime dynamilcally i have added other name to that list was not added permanent
when i refresh page.

C#
ComboboxItem item = new ComboboxItem();
item.Text = "four"
   item.Value = "four"

   comboBox1.Items.Add(item);


do this in page load of c#
 
Share this answer
 
v2
Comments
jackspero18 2-Aug-13 3:57am    
i want this dynamically, if i want to add sis on button click then i will write six in textbox,
then its not possible on load event
Member 9762654 2-Aug-13 4:23am    
ok then store the values in session array upto which you want to add then in page load add items
Before adding, put this in Data table & put this in Session or View state, later bind this with drop down list .
 
Share this answer
 
Try this,

button click event

C#
protected void btnAddItem_Click(object sender, EventArgs e)
{
session["cbItem"]= textBox1.Text;
Additem();
}


On page load

C#
protected void Page_Load(object sender, EventArgs e)
{
     Additem();
}



C#
public void AddItem()
{
ComboboxItem item = new ComboboxItem();
item.Text = session["cbItem"].Tostring();
item.Value = session["cbItem"].Tostring();
comboBox1.Items.Add(item);
}
 
Share this answer
 
v2
hi !

in pageload you insert code after you bind data to dropdown :
ex : insert into first position of dropdown

DropDownList3.Items.Insert(0, new ListItem("item A", "value A"));

0 : is index of array.


by Wengdeli
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900