I have a ListBox which is populated dynamically from a database in the code behind. I have another button and when i click the button, the button click event will get the all the selected listitem and insert the list item text to the database. I set AutoPostBack as false and EnableViewState is true in the listbox property
The problem is when i click the button , it only see 1 selected item even i select multiple items.Here are the codes. I appreciate your help. I spend 1 day on this issue and not getting anywhere.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
loadrdlist();
}
}
protected void loadrdlist()
{
((ListBox)TestFormView.FindControl("ListBoxB")).Items.Clear();
foreach (FailureTempRD rd in FailureTempRD.SelectFailureTempRD())
((ListBox)TestFormView.FindControl("ListBoxB")).Items.Add(new ListItem(rd.ReferenceDesignator, rd.ReferenceDesignator));
}
protected void btn_AddRD_Click(object sender, EventArgs e)
{
foreach (ListItem rd in ((ListBox)TestFormView.FindControl("ListBoxB")).Items) //This is where it only see 1 selected item
{
if (rd.Selected == true)
//put selected item to database
}
}
}
Here are both listbox and button
<asp:ListBox ID="ListBoxB" runat="server" SelectionMode="Multiple" ></asp:ListBox>
<asp:Button ID="btn_AddRD" runat="server" CausesValidation="False" onclick= "btn_AddRD_Click" Text="Add" />
Update :
I figure why. When i load the listitem, i need to add ID as listitem value. So change the following code. I test few times and it works as intend.
((ListBox)TestFormView.FindControl("ListBoxB")).Items.Add(new ListItem(rd.ReferenceDesignator, rd.ReferenceDesignator));
To
((ListBox)TestFormView.FindControl("ListBoxB")).Items.Add(new ListItem(rd.ReferenceDesignator, rd.ID));
Try using the GetSelectedIndices Method.
From above link:
Use the GetSelectedIndices method to identify or access the selected
items in the ListBox control. Each element in the returned array
represents the index for a selected list item. You can use the index
values to access the items in the Items collection.
Related
I have a data grid with four columns which includes 3 text column and a checkbox. Clicking on checkbox of different rows then click on delete button needs to remove the checked rows from data grid.
But the checked event not firing up for the first time when i clicked on Delete button but the next time it fires and the checked value of the checkbox is true.
It would be grateful if i have a solution for this.
thanks in advance.
Note : I have used Data Grid not Gridview and i don't have row property.
I have added a checkbox as Item template inside a Data Grid.
<asp:CheckBox OnCheckedChanged="id_CheckedChanged" runat="server" Visible='<%# DataBinder.Eval(Container, "DataItem.DeleteCheckboxVisible") %>' ></asp:CheckBox>
addded OnCheckedChanged event in code behind.
protected void id_CheckedChanged(object sender, EventArgs e)
{
CheckBox lnkView = (sender as CheckBox);
if (lnkView.Checked)
{
Response.Write("you checked the checkbox");
}
else if (!lnkView.Checked)
{
Response.Write("checkbox is not checked");
}
}
In the button Click Event
protected void ButtonDelete_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
foreach (DataGridItem row in DataGridEmployeeLifeEvents.Items)
{
if (row.ItemType == ListItemType.Item)
{
CheckBox chkVjezba = (CheckBox)row.FindControl("CheckBoxDelete");
if (chkVjezba.Checked)
{
//something
}
}
}
}
`````````````
I have a radcombobox,I want get checked items and save it in database but when i click save button,page is load again and my radcombobox become empty and then all of my checked items disappear.please help me,how can keep ckeckeditems?
As D Stanley mentioned in the comments, you're probably not checking for a postback when populating your dropdown.
This is the general approach you need to use in your code...
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
PopulateTheDropdown();
}
}
private void PopulateTheDropdown()
{
// Populate / databind your dropdown here
}
This will ensure your dropdown is not rebound when a postback occurs so you don't lose the selected value(s).
If you have autopostback activated you must save the selected value separately. Try to explicitly disable and check the value when the event fires:
<telerik:RadComboBox ID="RadComboBoxControl" AutoPostBack="false" OnSelectedIndexChanged="RadComboBoxControl_SelectedIndexChanged" runat="server" EmptyMessage="Select something"></telerik:RadComboBox>
protected void RadComboBoxControl_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
// Only test
var seleccionado = RadComboBoxControl.Items.FindItemByText(e.Text);
}
Check if you have assigned DataSource of the control in other parts of the code
RadComboBoxControl.DataSource = ...
RadComboBoxControl.DataBind();
This will also lose the selected element
I am using Telerik Grid. In that I have Check box for each row. Now I want that when I check more than one row and click on Asp button then it should update the selected rows. Now when I used:
protected void btnRead_Click(object sender, EventArgs e)
{
foreach (GridDataItem dataItem in invMenusAccessRadGrid.MasterTableView.Items)
{
}
}
I am not able to further implement inside the loop to get selected values.
I have One CheckBox above the CheckBoxList and its Text as "Show only Selected Items".
My CheckBoxList has 10 items and three items as Selected.
But when I Check the CheckBox, I want to display only the selected three items in the CheckBoxList.
When I uncheck the CheckBox, I want to display all the 10 items and three items as selected.
How to do this?
in the checked event of checkbox of "Show only Selected Items", do loop and check which is checked and if checkbox of checkboxlist is not selected then remove/visible false that item from checkboxlist.
void Check_Clicked(Object sender, EventArgs e)
{
if(chk.Items[i].Selected != true)
chk.visibility = chk.checked;
}
Your aspx page for each checkbox needs the onchecked change
<asp:CheckBox OnCheckedChanged="Check_Clicked" runat="server" />
Your code behind.
void Check_Clicked(Object sender, EventArgs e)
{
//foreach checkbox in your checkbox list
//checkbox.visibility = checkbox.checked;
}
Something like this... this is kind of pseudo code but hopefully the logic behind it is what you're looking for.
I have two dropdownlist's.DropDownList2(not bound to a datasource) and DropDownList3(bound to a datasource)
On change on input in one dropdownlist some content in the other Dropdownlist should change. For that i had used the logic as.
Autopostback is enabled for both this controls.
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList2.SelectedItem.Text == "Stamp")
{
DropDownList3.Items.Remove(DropDownList3.Items.FindByText("STA"));
DropDownList3.Items.Remove(DropDownList3.Items.FindByText("STM"));
}
<asp:DropDownList ID="DropDownList3" runat="server"
DataSourceID="SqlDataSource1" DataTextField="skey" DataValueField="casecode"
AppendDataBoundItems="True" AutoPostBack="True">
<asp:ListItem Selected="True" Value="S">Select</asp:ListItem>
</asp:DropDownList>
Now the problem is when i select DropDownList2.SelectedItem.Text == "Reg" STA and STM are not present. I want STA and STM values back in the dropdownlist on selection of 'Reg'.
When i first load my page and select 'Reg' all the values in DropDownList3(including 'STA' and 'STM') are present and than when i select 'Stamp' the values 'STA' and 'STM' are lost(as shown in the code). Now again when i select 'Reg' this values are not there, i want this values to be present again.
What do i have to do?? Do i have to bind it again to database?
Is there any other logic for it to be used in a different way ?If anyone can help me
You can to bind DropDownList3 everytime DropDownList2 selected index change then only if the value is "Stamp" you remove the values "STA" and "STM" from DropDownList3
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
// Fill DropDownList3 data source and bind it again to restore all the items
FillDataSource(); // This method gets all the data from DropDownList3
DropDownList3.DataBind();
if (DropDownList2.SelectedItem.Text == "Stamp")
{
DropDownList3.Items.Remove(DropDownList3.Items.FindByText("STA"));
DropDownList3.Items.Remove(DropDownList3.Items.FindByText("STM"));
}
...
If you know the values of the dropdown items, you can add them in the else clause, if you don't know the value/text combination you'll have to rebind.