I have two drop down lists on my page, ddlMin and ddlMax.
On page load I have it select the value of a request.querystring. When debugging it take the selected value until it reaches the next line which it then takes IT'S selected value?
http://localhost:37661/Default.aspx?search=&min=450000
ddlMin.SelectedValue = !FpsFunctions.IsNothing(Request.QueryString["min"])
? Request.QueryString["min"]
: "0"; <-- at this point it's 450000
ddlMax.SelectedValue = !FpsFunctions.IsNothing(Request.QueryString["max"])
? Request.QueryString["max"]
: "0"; <-- after this ddlMin.SelecgtedValue changes back to 0
I've checked the .cs.designer to make sure it wasn't inheriting something daft but it all seems right.
Cheers for the help in advance.
When creating my dropdownlist I was creating my items like so
foreach (string item in ConfigurationManager.AppSettings["Budget"].Split(','))
{
items = item.ToLower() == "any"
? new ListItem(item, "0")
: item.Contains("+")
? new ListItem(String.Format("{0:0,0}+", Convert.ToInt32(item.Replace("+", ""))),
"999999999")
: new ListItem(String.Format("{0:0,0}", Convert.ToInt32(item)), item);
ddlMin.Items.Add(items);
ddlMax.Items.Add(items);
}
What this meant, although I'm unclear as to why it has so much effect, was that the listitem was being attached to both dropdownlists as a singular item. So even though ddlMin was getting the right value and selecting it when the ddlMax was coming back as 0 it would select the same listitem in both... in order to correct this problem you need to just separate the adding to ddlMin and ddlMax...
foreach (string item in ConfigurationManager.AppSettings["Budget"].Split(','))
{
items = item.ToLower() == "any"
? new ListItem(item, "0")
: item.Contains("+")
? new ListItem(String.Format("{0:0,0}+", Convert.ToInt32(item.Replace("+", ""))),
"999999999")
: new ListItem(String.Format("{0:0,0}", Convert.ToInt32(item)), item);
ddlMin.Items.Add(items);
}
foreach (string item in ConfigurationManager.AppSettings["Budget"].Split(','))
{
items = item.ToLower() == "any"
? new ListItem(item, "0")
: item.Contains("+")
? new ListItem(String.Format("{0:0,0}+", Convert.ToInt32(item.Replace("+", ""))),
"999999999")
: new ListItem(String.Format("{0:0,0}", Convert.ToInt32(item)), item);
ddlMax.Items.Add(items);
}
Annoying but necessarily apparently
Related
I have this code:
foreach (MyClass info in data)
{
if (info.year!= "" && info.year!= null)
{
TreeViewYear.SelectedNode = TreeViewYear.RootNodes[0].Children.FirstOrDefault(x => x.Content?.ToString() == info.year);
}
}
Imagining that the foreach runs twice with the years "5" and "2", he selects correctly but then when he runs the second time, gets only the 2 value, that is, the value 2 and withdraws the 5.
If you want to select more than one item, you should set the SelectionMode of the TreeView to Multiple and add the items to be selected to the SelectedNodes property:
TreeViewYear.SelectedNodes.Add(
TreeViewYear.RootNodes[0].Children.FirstOrDefault(x => x.Content?.ToString() == info.year));
How can I remove blank items from a checkbox list in asp.net? I use this code but don't know what write inside the if body to remove that item
foreach (ListItem item in chkdisease.Items)
{
if (item.ToString() == "")
{
}
}
You can try
if(chkdisease.Items.Any()) {
foreach (ListItem item in chkdisease.Items.Where(c=>!string.IsNullOrEmpty(c.ToString())))
{
//your code here
}
}
Modify your code and use this :
for(int i=0; i<chkdisease.Items.Count; i++){
if(chkdisease.Items[i].Text.ToString() == "")
chkdisease.Items.Remove(chkdisease.Items[i]);
}
As i am not sure what problem are you facing with the above code, i would suggest you to fix the issue in back-end rather front-end. As you are filling the checkboxlist from database, just use a query like below for your checkboxlist datasource :
select disease from yourtable where disease is not null and disease<>''
I have some question about Listview in C#
My listview contains with 2 column like this :
colDATA1 colDATA2
Value1 Amount1
Value2 Amount2
Value3 Amount3
Value4 Amount4
And what I am trying to make is search Amount5 in Listview If not exist then do something.And if exist then return the Value5
I am trying to search and use the code like this :
If (Listview1.items.containskey("Amount5"))
{}
else
{MessageBox.show("Not Found")}
or if exist then return the value5 *I have no idea how to do.
I am searching this in google but most of it have only 1 Column and when I use the code the code won't work.
My question is :
1. How can I get Value5 if Amount5 exist.
Thank you.
The code to add the items
First Set listView1 Property "View : Details" Then Using this code
this.Listview1.Items.Add(new ListViewItem(new string[] { Value1, Amount1 }));
The OP already figured it out but this is just for future reference in case someone needs it.
What OP was missing is that ListView holds its items as objects in the Items property.
If (Listview1.items.containskey("Amount5"))
{}
else
{MessageBox.show("Not Found")}
containsKey is usually in dictionaries-like data structures. However, a ListView controller's Items is ItemCollection (for dictionaries you can use a DataGrid)
In your case I would do this using Linq.
// Returns the first item that satisfies the condition or null if none does.
ListViewItem found = items.FirstOrDefault(i => i.SubItems[1].Text.Equals("Amount5"));
if(found != null) {
MessageBox.Show(found.SubItems[0].Text.ToString());
}
else {
MessageBox.Show("Not Found!");
}
You can still use a for loop to do the same thing too.
If I want to use a foreach loop (since Linq can't be directly used on ListView.Item)
ListViewItem found = null;
foreach (ListViewItem item in listView1.Items) {
if (item.SubItems[1].Text.Equals("Amount5")) {
// If a match was found break the loop.
found = item;
break;
}
}
if (found != null) {
MessageBox.Show(found.SubItems[0].Text.ToString());
}
else {
MessageBox.Show("Not Found!");
}
Hope this helps!
I want to bind my arraylist to dropdown menu list. Here is my code.
I'm a beginner to C#. Please help.
if (selectinidialog.ShowDialog() == DialogResult.OK)
{
selectinibtn.Text = selectinidialog.FileName;
IniFile inifile = new IniFile(selectinidialog.FileName);
string[] sectorelist = inifile.GetSectionNames();
var sectorno = new List<string[]>;
sectorno.Sort();
selectsectorbtn.DataSource = sectorno;
selectsectorbtn.DataBind();
}
It looks like you're getting the data you want:
string[] sectorelist = inifile.GetSectionNames();
But then you don't do anything with that data. (You never use the sectorelist variable again.) Instead, you create a new empty list and bind to that:
var sectorno = new List<string[]>;
sectorno.Sort();
selectsectorbtn.DataSource = sectorno;
selectsectorbtn.DataBind();
Are you just looking to bind to the data you first got from inifile?:
string[] sectorelist = inifile.GetSectionNames();
selectsectorbtn.DataSource = sectorelist;
selectsectorbtn.DataBind();
It's worth a try. Though it's not entirely clear what you're specifically trying to do because you're working with two very different types here. You fetch a string[] but then you try to bind to a List<string[]>. If you need to bind to the latter for some reason then you'd need to translate the former to that somehow (based on whatever logic you'd use to define that translation). But if that was just a typo or other simple mistake, then it sounds like you can just bind to the data and don't need that List<string[]> at all.
I would suggest but dont take my answer for the best 1 that you rather not directly bind the list with an array.
rather loop through it and add one item at a time or use the AddRange() functionality in the right conditions.
Example :
if (selectinidialog.ShowDialog() == DialogResult.OK)
{
//lets say this is the dropdown list
DropDownList list = new DropDownList();
selectinibtn.Text = selectinidialog.FileName;
IniFile inifile = new IniFile(selectinidialog.FileName);
string[] sectorelist = inifile.GetSectionNames();
list.Items.Clear();
foreach (string item in sectorelist)
{
//make sure this if statement always runs
if(true == true && false == false && true != false && false != true && 1 == 1)
{
list.Items.Add(new ListItem() { Text = item });
}
}
}
I have a drop down list with autopostback enabled. When I change the value it updates a grideview on my webpage. However I want to get rid of the drop down and use a textbox sort of like a search function. I still want to keep my list though so I can compare my search string to the actual present values. However, if I make my dropdown list invisible I cant compare values to the items in it.
Or is there a better solution? A control that the user can't see but that I can put database values in it and compare those values with textbox text?
thanks for the help.
I am getting an error with this code:
foreach (string s in DropDownList3.Items)
{
//foreach gives me the error below
if(s == idsearch.Text)
{
valid = true;
break;
}
}
if(valid == true)
{
GridView1.DataBind();
}
DropDownList3.Items return a System.Web.UI.WebControls.ListItem and not List. You should do this
foreach (ListItem item in DropDownList3.Items)
{ //foreach gives me the error below
if(item.Text == idsearch.Text)
{
valid = true;
break;
}
}
error is due to casting ListItem to string. Do as :
foreach (ListItem s in DropDownList3.Items)
{
if(s.Text== idsearch.Text){
valid = true;
break;
}
}
Or you can find item by Text or Value using DropDownList.Items.FindByText or DropDownList.Items.FindByValue methods.
var searchResult = DropDownList1.Items.FindByText(idsearch.Text);
bool valid = searchResult != null;
Why not just use a variable in your server side code to store the values in the dropdownlist. So for example..
String [] comparedValues = new String[4] {"value1", "value2", "value3", "value4"};
and then..
if (comparedValues.Contains(myTextBox.Text))
{
// Do Something...
}