Databound Dropdown not accessible - c#

I took a check box list control in c# and I data-bound it with a table and now I am trying to access its selected items in a string variable using this code and I took a label too so that it can show which values are selected.
string val = this.CheckBoxList1.SelectedItem.ToString();
Label1.Text = val;
on debugging this code I receive no values in string and therefore label doesn't prints anything out of the selected items of the databound checkboxlist.

string text = this.CheckBoxList1.SelectedItem.Text;
string value = this.CheckBoxList1.SelectedItem.Value ;
Have a look at ListControl.SelectedItem

Related

how to get data of check box list with text boxes

I try to get data from the textboxex that is related to the ckeckboxex in the checkboxlist ..
in order to calculate the value of the selected box
this is my code :
foreach (ListItem item in listOthers.Items)
{
if (item.Selected)
{
sum += Convert.ToInt32(TextBox1.Text);//textbox of selected checkbox
}
}
lblSum.Text = sum.ToString();
What do I need to do to get the value of the textbox related to the checkbox?
Based on your comments, you have a CheckBoxList with a fixed size of 10 items and along with that 10 Textboxes (named TextBox1, TextBox2 and so on). The users marks some CheckBoxes and also enters a text into the corresponding Textboxes. You want to convert the Text of the Textboxes to an integer and calculate the sum of the marked entries.
You need to dynamically access the Textboxes by calling FindControl:
var index = 0;
foreach (ListItem item in listOthers.Items)
{
index++; // Increase here as the Textbox numbers start at 1
if (item.Selected)
{
var txt = FindControl("TextBox" + index.ToString()) as TextBox;
if (txt != null)
sum += Convert.ToInt32(txt.Text); //text of selected Checkbox
}
}
lblSum.Text = sum.ToString();
Please note that it is important to call FindControl on the container of the Textboxes. The call in the sample works if the Textboxes are located directly on the page. If they are located on a Panel named Panel1, you'd have to call:
var txt = Panel1.Findcontrol("TextBox" + index.ToString()) as Textbox;
An alternative to using a CheckBoxList that is prepared for variable sizes by design and having a fixed list of 10 TextBoxes next to it, would be to create a Repeater with a CheckBox and a TextBox in its ItemTemplate. Downsize is that you'd have to create some DTOs for DataBinding and also had to use some more dynamic control resolution (both for the CheckBox and the TextBox ).

How to get listbox selected item value

I am working with C# .NET 4.0
I am trying to get the value of a single selected item in a listbox.
This is how I populate the control:
this.files_lb.DataSource = DataTable object
In my designer, I have specified file_name as the DisplayMember and file_id as the DisplayValue
After selecting an item in the listbox, I tried the following to get the value:
this.files_lb.SelectedValue.ToString()
But all it returns is "System.Data.DataRowView".
At this link : Getting value of selected item in list box as string
someone suggested -
String SelectedItem = listBox1.SelectedItem.Value
However, 'Value' is not an option when I try this.
How can I get the ValueMember value from a single selected item in a listbox?
var text = (listBox1.SelectedItem as DataRowView)["columnName"].ToString();
Replace columnName with the name of the column you want to get data from, which will correspond with a column in your datasource.
Also watch out for nulls if there's no selected item.
It really should be this easy; I have the following in a click event for button to make sure I wasn't over simplifying it in my head:
private void button1_Click(object sender, EventArgs e)
{
string selected = listBox1.GetItemText(listBox1.SelectedValue);
MessageBox.Show(selected);
}
And the result:
Edit
It looks like your issue may be from not setting a property on the control:
Select the ListBox control
Click the little arrow to show the binding / items options
Select Use Data Bound Items
If I deselect that box, I get the exact same behavior you are describing.
var selectedValue = listBoxTopics.SelectedItem;
if (selectedValue != null)
{
MessageBox.Show(listBoxTopics.SelectedValue.ToString());
}
You may need to set the DataValueField of the listbox.
NewEmployeesLB.DataSource = newEmployeesPersons.DataList.Select(np => new
ListItem() { Text = np.LastName + ", " + np.FirstName, Value = np.PersonID.ToString() }).ToList();
NewEmployeesLB.DataTextField = "Text";
NewEmployeesLB.DataValueField = "Value";
NewEmployeesLB.DataBind();

converting cells into text boxes where check box is checked

I am using mysql and asp.net with c#. I have a grid view which will display dynamically selected table data. I am able to display the data of selected table. In the first column i have added a check box and a Button outside Grid view. When user selects Check box and clicks on button, the selected rows must turn into text boxes. I am able to find the seleted check box, but i'm unable to convert the cells into text boxes. Here's my code:
int n = GridView1.HeaderRow.Cells.Count;
for( int i=0; i < GridView1.Rows.Count;i++)
{
GridViewRow row = GridView1.Rows[i];
bool isChecked = ((CheckBox)row.FindControl("CheckBox1")).Checked;
{
for( int j=0;j<n;j++)
{
TextBox txt = ((TextBox)GridView1.Rows[i].Cells[j]).Text;
}
}
}
At this line: TextBox txt = ((TextBox)GridView1.Rows[i].Cells[j]).Text;
i get a warning :
cannot convert 'System.Web.UI.Controls.TableCell' to type 'System.Web.UI.Controls.TextBox
I am unable to resolve this. Please help. Thank you
Try this.
You can remove one or few lines based on your hands-on with C#.
Concept is, you should create a TextBox, assign cell text that textbox and then Add that newly created textbox to child controls of Grid Cell of particular row.
Mark this solution if you found useful.
bool isChecked = ((CheckBox)row.FindControl("CheckBox1")).Checked;
if(isChecked)
{
for( int j=0;j<n;j++)
{
TextBox tbForCell = new TextBox();
tbForCell.Text = GridView1.Rows[i].Cells[j].Text;
GridView1.Rows[i].Cells[j].Text = "";
GridView1.Rows[i].Cells[j].Controls.Add(tbForCell);
}
}
If you want to avoid the TextBox to appear in CheckBox Column please initialise loop variable j with 1 instead of 0.
for( int j=1;j<n;j++)
Your method call is finding the cell. The Textbox is a control contained within the cell. Try something like this instead:
TextBox txt = ((TextBox) GridView1.Rows[i].Cells[j].FindControl("textbox name")).Text;
The FindControl method is documented here.
Also take #Bartdude's advice about using editable gridviews. If one will work at least as well as what you're trying to hand-roll, it's worth the time learning how to use it.

Setting the selected value of a ListBoxView as a string (C#)

When a value is selected in a ListBoxView, How could the selection be stored as a string / variable, so I can then call another method that will use this string to perform an action?
If the Items in your listbox are all strings, you could use
System.Windows.Forms.ListBox lb= new ListBox();
lb.SelectedItem.ToString();
Otherwise, we will need to know the types of items you have added to the listbox.
If you using ListBox get selected item like this:
string item = listBox1.SelectedItem.ToString();
If you using ListView get selected item(s) depending on MultiSelect property:
Use this code if MultiSelect property = false
string itemName = listView.SelectedItems[0].Name;
Use this code if MultiSelect property = true
foreach (ListViewItem item in ltvMainMenu.SelectedItems)
{
string itemName = item.Name;
}

How to retrieve text value from a list box index in C#

listbox1.items[0].tostring();
its the command for getting the text value of item at 0th index but i have some list boxes in my form which are data bound to a sql database table.When ever i use this command it gives me (System.Data.DataRowView) as a string as output regardless of the actual text value of the listbox item at 0th index.Plz guide
You can use the Text property of the ListItem:
string itemText = ListBox1.Items[0].Text;
Update:
If you're in WinForms, a bound list box will return a DataRowView:
DataRowView drv = (DataRowView)ListBox1.Items[0];
string itemText = drv.Row["MyColumn"].ToString();
.Text no longer works as of 23/07/2013
Use ListBox1.Items[index].ToString();

Categories