I have a drop down list populated in code and the list displays everything as it should BUT if Pink Water Buffalo is selected from the drop down, the text displayed is Yellow Snake Did I set something up incorrectly? This is full syntax (and I have no selected index changed event for the drop down list that could cause err)
protected void Page_Load(object sender, EventArgs e)
{
IP = HttpContext.Current.Request.Params["HTTP_CLIENT_IP"] ?? HttpContext.Current.Request.UserHostAddress;
if (!IsPostBack)
{
message.Visible = false;
populateDDL();
}
}
protected void populateDDL()
{
var item = new List<ListItem>
{
new ListItem("", ""),
new ListItem("Yellow Snake", "9"),,
new ListItem("This item works", "12"),
new ListItem("Pink Water Buffalo", "9"),
};
this.dropdownlistone.DataTextField = "Text";
this.dropdownlistone.DataValueField = "Value";
this.dropdownlistone.DataSource = item;
this.dropdownlistone.DataBind();
}
EDIT
This is my asp that is used to create the drop down list
<asp:DropDownList ID="dropdownlistone" runat="server" Height="20px" Width="278px"
AutoPostBack="true"> </asp:DropDownList>
EDIT #2
It's ugly and a lot of syntax if you have multiple drop down lists, but for me it wasn't a big deal. What I did was added a .0, .1 etc and incremented up for each item in the drop down list. Then used the .Split method to strip out only the relevant piece like so:
protected void populateDDL()
{
var item = new List<ListItem>
{
new ListItem("", ""),
new ListItem("Yellow Snake", "9.0"),,
new ListItem("This item works", "12.1"),
new ListItem("Pink Water Buffalo", "9.2"),
};
this.dropdownlistone.DataTextField = "Text";
this.dropdownlistone.DataValueField = "Value";
this.dropdownlistone.DataSource = item;
this.dropdownlistone.DataBind();
}
String[] stringsplit = dropdownlistone.SelectedValue.ToString().Split('.');
String itemprice = stringsplit[0].Trim();
In your example, the value for "yellow Snake" and "Pink Water Buffalo" are both set to 9. "Yellow Snake" comes first in the list, so that is the one you are getting.
You'll have to figure out a way to make the value unique for each item. Otherwise you will get unexpected results, possibly across different browsers. A method I've used is combining the text and value with a pipe (|):
var item = new List<ListItem>
{
new ListItem("", ""),
new ListItem("Yellow Snake", "9|Yellow Snake"),
new ListItem("This item works", "12|This item works"),
new ListItem("Pink Water Buffalo", "9|Pink Water Buffalo"),
};
Then you can easily get the value by splitting it:
string value = dropdownlistone.SelectedValue.Split('|')[0];
I prefer the pipe as it's hardly ever used as a display value. Using a dot you might run into some issues if the text contains a dot, such as "Yellow Snakes are great, but green ones are the the best."
Related
I have an array with country names in a listBox. When I enter the textBox, I want any country that starts with what's in the textBox to display.
So if I enter : B => Brazil
Not like this: A => Argentina, England
Only if it starts with what's in the textBox. Full words would also work.
The arraylist contains more than just names, but the code below extracts just the names. List2 is the arraylist I want to use for the search.
private void textBox7_TextChanged(object sender, EventArgs e)
{
listBox1.ClearSelected();
listBox1.DataSource = null;
foreach (Country name2 in Mytree.List)
{
List2.Add(name2.name);
Console.WriteLine(List2);
}
}
If your objective is to avoid typing the full country name then there is no need to reinvent a new kind of user interface. The TextBox has already all the plumbing available to do what you are trying to reproduce with your code. All you need is a source of your data and the settings a pair of properties
// Create the list to use as the custom source.
var source = new AutoCompleteStringCollection();
source.AddRange(new string[]
{
"Argentina",
"England",
"Brazil",
"Italy",
"..."
});
textBox1.AutoCompleteCustomSource = source;
textBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
Not quite sure but you will want to look at setting the datasource of the listbox to something like
listBox1.DataSource = Mytree.List.Where(a=>a.name.StartsWith(textBox7.Text)
Your example of using a foreach loop to add each item is a little redundant.
private void textBox7_TextChanged(object sender, EventArgs e)
{
listBox1.ClearSelected();
listBox1.DataSource = null;
var matchingCountries = Mytree.List.Where(l=>l.name.StartsWith(textBox7.Text));
foreach (Country name2 in matchingCountries)
{
listBox1.Items.Add(name2.name);
}
}
New to c# and .net and I'm trying to throw together a listview that has three columns, Quantity, Item description and Price. I know how to fill the rows with data and I can even add the subitem below but what I can't figure out is how I can group them together as a primary item with "children" items tied to it...
Sorry for my most likely incorrect verbiage, but this is what I am shooting for:
Qty Description Price
1 Widget 2.95
Widget add on .25
Widget insurance 1.25
1 Sprocket 4.95
Sprocket add on .50
I am trying to figure out how to group the subitems below the primary item to make it where when I select, for instance, "Widget insurance", it highlights "Widget add on" and "Wiget" as one entity. So for example if I need to go back and remove "Widget insurance" from the purchase of the "Widget" I can click on any item connected to "Widget", ex. "Widget", "Widget add on" or "Widget insurance" and it will pull up another form that allows me to deselect "Widget insurance", hit OK and it would update the list accordingly...
I've (poorly) thrown together code that visually gives me what I am looking for, but I think I am completely confusing the use of subitem and it's purpose:
string[] newItem = new string[3];
ListViewItem itm;
newItem[0] = "1";
newItem[1] = "Widget";
newItem[2] = "2.95";
itm = new ListViewItem(newItem);
listView1.Items.Add(itm);
string[] newSubItem = new string[3];
ListViewItem sub;
newSubItem[0] = "";
newSubItem[1] = "Widget add on";
newSubItem[2] = ".25";
sub = new ListViewItem(newSubItem);
listView1.Items.Add(sub);
Any help would be greatly appreciated.
Here you are. "Qty" will be ListViewItem's text. "Description" and "Price" will be ListViewItem's SubItems.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
listView1.View = View.Details;
listView1.Columns.Add("Qty", 100, HorizontalAlignment.Left);
listView1.Columns.Add("Description", 100, HorizontalAlignment.Left);
listView1.Columns.Add("Prie", 100, HorizontalAlignment.Left);
var details = new[] {"Widget", "2.95"};
var item = new ListViewItem("1");
item.SubItems.AddRange(details);
listView1.Items.Add(item);
details = new[]{ "Widget add on",".25"};
item = new ListViewItem("");
item.SubItems.AddRange(details);
listView1.Items.Add(item);
}
}
Hope this help.
I have three dependant checkboxlists.
1. Countries
2. States
3. Cities
I want to list all the States if the particular Country is selected in the Countries checkboxlist. And similarly if i select any State then the respective Cities should be populated in the cities checkboxlist.
I have created separate functions for States for every Country and calling them with the following code:
private void Country_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (string s in Country.CheckedItems)
{
if (Country.CheckedItems.Contains("US"))
{
US_States_Checked();
}
if (Country.CheckedItems.Contains("Canada"))
{
Canada_States_Checked();
}
}
}
public void Canada_States_Checked()
{
string[] canada_states = new string[12];
canada_states[0] = "Alberta";
canada_states[1] = "British Columbia";
canada_states[2] = "Manitoba";
canada_states[3] = "New Brunswick";
canada_states[4] = "Newfoundland and Labrador";
canada_states[5] = "Northwest Territories";
canada_states[6] = "Nova Scotia";
canada_states[7] = "Ontario";
canada_states[8] = "Prince Edward Island";
canada_states[9] = "Quebec";
canada_states[10] = "Saskatchewan";
canada_states[11] = "Yukon Territory";
State.Items.AddRange(canada_states);
}
I have the following problems:
1. Which property is used for detecting when the checkbox is UnChecked?
2. How to make a check on the name of selected state/country and check whether it is Checked or Not? Something like:
if(country.selectedItem.Equals("US") and country.selectedItem is unchecked....)) {
.......
}
How to remove/clear the particular states/cities when the country is unchecked keeping in mind that it shouldn't remove the states of any other country listed in the states checkboxlist?
Problem is simple but a bit tricky.
Thanks
Separate function for each country is pretty bad idea. You should store data outside of your code. Consider using XML or database or JSON to store data. You can read about XML serialization here and here, for example
After loading data from file to memory you can place it in the dictionary like
Dictionary<string, Dictionary<string, List<String>>> data;
//access data
var cities = data["Canada"]["Ontario"];
And final part - pass selected item to your dictionary:
var states = data[country.selectedItem].Keys;
and
var cities = data[country.selectedItem][state.selectedItem];
I suppose that country and state are names of your CheckBoxList objects.
As for CheckBoxList usage - just implement your logic, all needed methods are described here. (I am still not sure, what are you using: WPF or WinForms or what, but you can find proper docs).
SelectedIndices and SelectedItems return selected elements of checkBoxList.
You can check like this:
var selectedItems = country.SelectedItems;
if (selectedItems.Contains("Canada")
{
//Do what you need
}
I am developing a solution in C # and I need to add a few buttons to TabbedPanel. Happens that debugger adds only one and even a single button.
public void AddDrinkstoTabbedpanel(TabPage tp)
{
List<string> drinks = new List<string>();//New list empty
food foo = new food();//Call food to get drinks
string []product = foo.name;//string product [] = food.name (drink)
foreach (string value in product)//(string value in product)//for any "value" in product
{
Button bt = new Button();
bt.Size = new Size(100, 100);
drinks.Add(value);
tp.Controls.Add(bt);
//listofButtons.Add(bt);
}
}
I previously define an array with some items and now i need to add a button for each item founded, how can I modify this code given above to get what I want.
Change the Location for all buttons. They are being created but at the same position and look like one.
I have one RadComboBox.
I fill it with a function which returns for example {Stack, Over, Flow, StackOverFlow}
When I click RadCombobox that items are listed.
And I want to give first place for empty element.
And I tried to do below:
var stack= GetItems(SiteId);
RadComboBox1.Items.Add(new RadComboBoxItem("", "0"));
RadComboBox1.DataSource = stack;
RadComboBox1.DataTextField = "Name";
RadComboBox1.DataValueField = "Id";
RadComboBox1.DataBind();
RadComboBox1.AllowCustomText = false;
There is no change. Only {Stack, Over, Flow, StackOverFlow} are listed.
When I write below code
var stack= GetItems(SiteId);
RadComboBox1.DataSource = stack;
RadComboBox1.DataTextField = "Name";
RadComboBox1.DataValueField = "Id";
RadComboBox1.DataBind();
RadComboBox1.Items.Add(new RadComboBoxItem("xyz", "0"));
RadComboBox1.AllowCustomText = false;
Only {Stack, Over, Flow, StackOverFlow, xyz} are listed.
But I dont get the result which I want.
And the design side is below.
<telerik:RadComboBox ID="RadComboBox1" runat="server" Width="120px" MarkFirstMatch="true" Filter="Contains"></telerik:RadComboBox>
How can I do?
I want to list { " " , "Stack", "Over", "Flow", "StackOverFlow"}
Using your first choice, add RadComboBox1.AppendDataBoundItems = true before you call DataBind(). This means it won't clear out the existing items before adding the items from the data bind. You will need to manually clear the items beforehand if necessary:
RadComboBox1.Items.Clear();
RadComboBox1.ClearSelection();
When you set RadComboBox DataSource property it will clear all the items in the RadComboBox.Items then iterates through the IEnumerable object which is set to it and add them to the items collection. In your case you can add all of your items manually using radComboBox.Items.Add().
var stack= GetItems(SiteId);
//Add your empty item.
RadComboBox1.Items.Add(new RadComboBoxItem("", "0"));
//Add all the other items
foreach(var item in stack)
{
RadComboBox1.Items.Add(new RadComboBoxItem(item.Name, item.Id))
}
RadComboBox1.DataTextField = "Name";
RadComboBox1.DataValueField = "Id";
Or you can add your empty item to the collection first and then bind it to the RadComboBox(I assume stack is a List of StackItem)
List<StackItem> stack = GetItems(SiteId);
//Add your empty item.
stack.Insert(0, new StackItem(){Name = "", Id = 0});
//Set the DataSource
RadComboBox1.DataSource = stack;
RadComboBox1.DataTextField = "Name";
RadComboBox1.DataValueField = "Id";
Although usually it's not a good idea to change your collection(latter example) for such cases.