Keeping the selected item of DropDownList during resetting DataSource - c#

I have several DropDownlist controls in my web page which the DataSource of all of them set to a specified list of items.
When one of these items is selected from one of the DropDownlist ,this item should remove from the list of other DropDownList. So I should set the DataSource of all of DropDownList again.
But in this case the selected item returned to the first item.
Is there any way to keep the selected item of DropDownList during resetting DataSource?
Edit:
protected void dropdownlist1_SelectedIndexChanged(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(dropdownlist1.Text))
//Some code to remove the selected item from list so the user can not see it in the select list anymore
dropdownlist1.DataSource = myList;
dropdownlist1.DataBind();
dropdownlist2.DataSource = myList;
dropdownlist2.DataBind();
dropdownlist3.DataSource = myList;
dropdownlist3.DataBind();
//...
}

Related

to populate one dropdown on selectedindexchanged of another inside gridview

I have gridview with two dropdowns in a row say country and state,on change of country dropdown i want to populate the state dropdown when the grid is in edit state.
I got both the dropdowns in RowEditing event of grid,also selectedindexchanged event is attached to first dropdown of grid.The problem is how to get second dropdown i.e. state dropdown in selectedindexchange event of country dropdown.
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl1 = (DropDownList)sender;
GridViewRow row = (GridViewRow)ddl1.NamingContainer;
if (row != null)
{
DropDownList ddl2 = (DropDownList)row.FindControl("DropDownList3");
{
//call the method for binding the second DDL based on the selected item on the first DDL
DataTable dt = BindDropDownList(ddl1.SelectedItem.Text);
ddl2.DataTextField = "Field1";
ddl2.DataValueField = "Field2";
ddl2.DataBind();
}
}
}
If RowEditing event occurs before dropdown selection changed event, you can store reference to current Row in you application. Then get this row in dropdown changed event.

Checkbox inside a gridview change after page load in asp.net C#

I have a radiobutton list named rdbModules and a GridView named dgvMenu.dgvMenu contains five CheckBoxes name chkSelect,chkAdd,chkUpdate,chkDelete,chkReport.
I have populated dgvMenu from the database depends on the selection of rdbModule.
The problem in when I am selecting checkbox inside the gridview it is working fine.
Suppose I have selected first item from radiobuttonlist and then selected some checkboxes from gridview. After that if I select the second or any other other options from the radio button list i am not able to get the selected checkbox details inside the gridview for the selection of first item of the radio button list.
rdbModules_SelectedIndexChanged(object sender, EventArgs e)
{
//Loop through your gridview here
foreach (GridViewRow row in dgvMenu.Rows)
{
if (((CheckBox)row.FindControl("chkboxid")).Checked)
{
//do what you want
}
}
if (rdbModules .Items.Cast<ListItem>().Any(item => item.Selected))
{
foreach (ListItem item in rdbModules.Items)
{
if (item.Selected)
{
string selectedValue = item.Value;
dt = objSec.ShowSubMenuModuleWise(selectedValue);
dgvMenu.DataSource = dt; dgvMenu.DataBind();
}
}
}
}

How to add ListItem to DropDownList from code and get the selected value

I succeeded adding List-items to my DropDownList from my database, but after I ran my application and selected a value from the DropDownList, I checked my break point at code and I saw that the selected value is wrong. It always selects the first value.
My code is:
UserBLL uBLL = new UserBLL();
List<Item> list = uBLL.GetAllItemsCategory();
foreach (Item item in list)
{
int var = 1;
ListItem lItem = new ListItem(item.name, var.ToString());
modelsList.Add(lItem);
var++;
}
DropDownList2.DataSource = modelsList;
DropDownList2.DataBind();
How can I fix it so that when the following is executed I'll get the right selection and not just the first item?
order.nameItem = DropDownList2.SelectedValue;
You need to check for !IsPostBack in the Page_Load event, like this:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
// Put logic here to bind
DropDownList2.DataSource = modelsList;
DropDownList2.DataBind();
}
}

ListBox not getting all selected items

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.

dropdownlist added more list item after pick

I got a weird problem. I use a DropDownList item in a web application. after i check for the first time one of the options all the items are added to end of the list.
initialized like this
protected void Page_Load(object sender, EventArgs e)
{
string myXMLfile = Server.MapPath("~/VMlist.xml");
DataSet dsMachines = new DataSet();
try
{
dsMachines.ReadXml(myXMLfile);
DropDownList1.DataSource = dsMachines;
DropDownList1.DataValueField = "machineID";
DropDownList1.DataTextField = "machineName";
DropDownList1.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
and the SelectedIndexChanged:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
machineOS.Text = OSdata.OSDataRetrieve.getOSInfo(DropDownList1.SelectedItem.Text);
}
i use XML initializing. I'm also adding an image of 'before' and 'after'
You need to add the following to your Page_Load:
if (!Page.IsPostBack)
{
//code to bind dropdown
}
Your list is being bound each time the page posts back, which is why you're getting duplicates. Add the above line, and you should be all set.
Because you had set the AppendDataBoundItems="true"
You have to set it to AppendDataBoundItems="false"
Every time the dropdown selected Index changed event is fired, your dropdownlist again binded and added datasource items again.
In page_load, you should probably check for IsPostback.
Currently, you are rebinding the list to the listbox on each load. It's not just adding 3 new items - there is a scrollbar and I suspect it is re-adding ALL of the items.
My problem was that I manually called databind() but found that databind was already being called during because it was a nested databound control. Try commenting out your databind call and see if that fixes it.

Categories