I have trawled the internet but I cannot find a solution to the following problem.
I have a ListBox control that gets populated with a list of values from a SQL Server database table on FormLoad.
This allows the user to select multiple values and these can be submitted into the SQL Server database column as comma-delimted values.
Problem is that I would like the user to allow the editing of these submitted values from the same ListBox control.
Therefore I would like to show the user the values that are pulled from the database as selected values in the ListBox and NOT populate the ListBox with those values as the ListBox has been populated already with set values on FormLoad.
I am trying this but its not working -
foreach (ListItem li in lst_subspeciality.Items)
{
foreach (string sqlitem in dt.Rows[0]["SubSpeciality"].ToString().Split(','))
{
if (li.Text == sqlitem.ToString())
{
li.Selected = true;
}
}
}
Any help is much appreciated.
Thanks.
I didn't test this but
List<string> myValues = dt.Rows[0]["SubSpeciality"].ToString().Split(',').ToList();
foreach (ListItem li in lst_subspeciality.Items)
{
if (myValues.Contains(li.Text))
li.Selected = true;
}
Related
When I use the code below, I remove the datatable values, but the data table structure still exists and displays empty fields (see pics) with the DOM explorer showing an empty table and table rows.
How can I clear the datatable values and the table itself? This way when I repopulate search again, the empty smaller table isn't present?
lvwOutput.Items.Clear();
lvwOutput.DataSource = null;
lvwOutput.DataBind();
Before
After items.clear and datasource = null
This is ridiculous and I believe there is a better way to do this, but the never ending server/client battle makes this harder than it should be. My listview binded to a datatable is called lvwOutput.
In my btnClear I had to put the following. You cannot hide the element or clear the items in the server side asp code for this to work
ScriptManager.RegisterStartupScript(Page, GetType(), "emptyTable", "javascript:emptyTableRows(); ", true);
In my javascript code I had to put the following, this clears the client code
function emptyTableRows(){
var tableHeaderRowCount = 0;
var table = document.getElementById('lvwOutputTable');
var rowCount = table.rows.length;
for (var i = tableHeaderRowCount; i < rowCount; i++) {
table.deleteRow(tableHeaderRowCount);
}
}
And then in the portion of my code that would display the listview and datatable when the user initiates another sql search. This clears the server side.
lvwOutput.Items.Clear();
lvwOutput.DataSource = null;
lvwOutput.DataBind();
You can create a property the stores the data table in session that way you can access it during the click event.
DataTable dtbleDataSource
{
get
{
return Session["dataSource"] as DataTable
}
set
{
Session["dataSource"] = value;
}
}
In your click event you can say:
dtbleDataSource.Reset();
I want to prevent from entering duplicate values in ListBox by giving an error message saying value has been duplicated in c#.
E.g.
Listbox
Red|Blue|Red
Error: duplicate values has been entered.
using linq: if list is string array,
if(myArray.Distinct().Count() < myArray.Length)
{
// Show error
}
Try This:
if (!ListBox1.Items.Contains(ListBox1.Items.FindByText(item)))
{
ListBox1.Items.Add(item);
}
else
{
Response.Write(#"<SCRIPT LANGUAGE=""JavaScript"">alert('Duplicate value hadbeen entered')</SCRIPT>");
}
Check Item in List box if item does not exits in ListBox then add otherwise not.
as per your requirement you can find item in ListBox by using FindByValue or FindByText
ListItem item = ListBox1.Items.FindByValue(value);
if (item == null)
{
item.Add(value);
}
else
{
// Display message to user. Item already exists in listbox
}
I'm just wondering how can I possibly get all values in a ListBox as selectedItems? Note that the viewed records came from an SQL datasource. I'll be using these values as list of email addresses for an email blast.
Any help would be much appreciated, thanks.
I'm using ASP.Net C#
if you want to select all items in a listbox control, Use This
listView1.MultiSelect = true;
foreach (ListItem item in ListBox1.Items )
{
item.Selected = true;
}
or you can do like this with LINQ :
Just pass your listview and checkstate to the function.
public void CheckAllItems(ListView lvw, bool check)
{
lvw.Items.OfType<ListViewItem>().ToList().ForEach(item => item.Checked = check);
}
First and foremost, I'm pretty new to programming, so attention to detail is appreciated.
I've currently got an asp checkbox list that receives data from an SQL table. I'm encountering a problem where if there's 2 items that are exactly the same, my remove function will remove both items. The following is the code for that:
protected void btn_remove_Click(object sender, EventArgs e)
{
for (int i = 0; i < UPCList.Items.Count; i++)
{
if (UPCList.Items[i].Selected)
{
var item = UPCList.Items[i];
var frcID = item.Value;
_itemNumberRepository.Delete(frcID);
}
}
Response.Redirect("WebForm2.aspx");
Response.End();
}
public string Delete(string itemCode)
{
string connectionString = foobar
Int32 returnDeleted = 0;
string deleteUpcCode =
"DELETE FROM compare1 "
+ string.Format("WHERE itemcode = '{0}'",itemCode);
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(deleteUpcCode, connection);
command.Connection.Open();
returnDeleted = command.ExecuteNonQuery();
command.Connection.Close();
}
return returnDeleted.ToString();
}
I've been told to use Auto Incrementing ID from SQL so that each row will have a unique ID so I can delete only the selected lines. However, I don't even have a clue how to do that. I've already turned on the identity option in SQL for the itemcode column, but aside from that, I'm lost. How do I go about using that ID to delete only selected items from the checkbox list in asp?
When you say the items are "exactly the same", do you mean that they have the same item code? And item codes are allowed to be duplicated in your system (i.e. is that the correct business rule)?
If so, that's why you need a auto-generated ID for each line, so that each row is unique. Which means that you need to use that ID in your DELETE query instead of the item code.
Typically, in a (ASP.NET) web app, this sort of thing is done with a "grid". You can use a GridView with two columns: one is a checkbox and another column is just a label showing the item code. Each row is bound to the ID field.
So, I recommend that you start by checking out GridView (there's lots of examples out there on the web): MSDN documentation for GridView
Using ASP.NET 3.5 ListView control.
I would like to capture the value of my table id from the currently edited row of a ListView.
A ItemEditing event has been added to the ListView.
The only value available in this event is e.NewItemIndex.
This returns 0 if the first row of the current page of the ListView is being edited.
How do I convert this 0 into the actual table id (label control value)?
I have tried:
table_id = Convert.ToString(ListView1.Items[e.NewEditIndex].FindControl("table_idLabel1"));
Can you use the DataKeyNames property instead of a label? Set the property to the name of the database field that is the key for the table, then do something like this:
table_id = ListView1.DataKeys[e.NewEditIndex].Value.ToString();
Are you sure table_idLabel1 is the correct id?
Also, you may need to look recursively as in Chris's answer. Plus, it looks like your casting the control to a string. You probably want the ID property and not the control itself.
Use FindControlRecursive (from Recursive Page.FindControl). The problem with FindControl is that it only search one layer deep.
private Control FindControlRecursive(Control root, string id)
{
if (root.ID == id)
{
return root;
}
foreach (Control c in root.Controls)
{
Control t = FindControlRecursive(c, id);
if (t != null)
{
return t;
}
}
return null;
}