I have a drop down list that is bound to some items. i want to replace the selected item with a text box value and again want to bind the dropdownlist with new values.
For this i am currently storing the dropdown list items in a temporary List. How can I replace the current selected item with textbox value.
for (int i = 0; i < DropDownEmail.Items.Count; i++)
{
if (?)
{
ObjRegistration = new ClassRegistration();
ObjRegistration.UserName = TextBoxEmail.Text;
tempEmailList.Add(ObjRegistration)
}
else{
ObjRegistration = new ClassRegistration();
ObjRegistration.UserName = DropDownEmail.Items[i].Text;
tempEmailList.Add(ObjRegistration);
}
}
Your code doesn't make much sense as it is written now, but in general, if you want to replace an item in a dropdown list you need to do something like this:
var selectedItem = tempEmailList.SelectedItem; //returns a ListItem object
selectedItem.Text=txtField.Text;
dropDownList.DataBind(); //Rebind it so you see the change.
In your case, it seems that you are binding to a custom collection of ClassRegistration but since you are doing this on code-behind, once you bind the elements to the Dropdown list for the first time, you only have a reference to the Items collection in the dropdown which are all of type ListItem.
You can, alternatively, update your underlying custom collection and rebind that to the dropdown list:
var tempEmailList= ... //get it from DB or whatever
tempEmailList.Find(x => x.ID == int.Parse(ddl.SelectedItem.Value)).UserName = txtBox.Text;
ddl.DataSource = tempEmailList;//re-assing the datasource
ddl.DataBind();//rebind
Related
I have a dropdownlist where I need to store more data than the standard list item allows. The approach I've taken is to add an attribute to each of the listitems.
I monitor for changes and can return the SelectedIndex, but I'm not sure how to get the attribute back from there, or whether there are any easier ways of achieving this.
Any ideas?
Try this:
ddl.SelectedItem.Attributes["key"];
I did try this once before and i figured i could not really use the attribute's on the DropDownList attributes.
What i did was the following:
Create a list containing a KeyValuePair. The Key in the KeyValuePair is the same ID as you put in your DropDownList Item.
The value of the KeyValuePair, is the value (or values) that you would like to keep/connect with your item.
You can store the List in your viewState and read the data once you have selected an item in your DropDownList and find the right KeyValuePair using the ID.
So you can "store" the data like this:
var listKeyValuePair = new List<KeyValuePair<int, string>>();
listKeyValuePair.Add(new KeyValuePair<int, string>(1, "data"));
ViewState["DataList"] = listKeyValuePair;
And you can get your data like this:
var listKeyValuePair = (List<KeyValuePair<int, string>>)ViewState["DataList"];
var dataILikeToHave = listKeyValuePair.Find(k => k.Key == Convert.ToInt16(dropDownlist.SelectedItem.Value));
Is there a way to make sure the selected item is visible in the Spark DataGrid?
.
Context
I have a data grid bound to an array collection. I receive remotely a service that gives me a ID (string) of an object that is in the collection. Using just the string I loop through the collection to find the item that matches the string. I find the object by it's ID. Now I have the object I want to select in the datagrid. I can set the
dataGrid.selectedItem = object;
Now I need to make sure it's visible. I do not have the row or column index.
.
Update
Using the answer below I've complimented it with this function:
/**
* Ensures the item is visible (for spark data grid)
**/
public function ensureItemIsVisibleInSparkDataGrid(datagrid:spark.components.DataGrid, item:Object):void {
var list:IList = datagrid.dataProvider;
var length:int = list.length;
var itemFound:Boolean;
var object:Object;
var index:int;
for (var i:int;i<length;i++) {
object = list.getItemAt(i);
if (object==item) {
itemFound = true;
index = i;
break;
}
}
if (itemFound) {
datagrid.ensureCellIsVisible(index);
}
}
Yes, it's called ensureCellIsVisible(). You need to know that row and column of the item in question. To get this to work you'd need to listen for the selectionChange event then calculate the row and column of the currently selected item.
I am using items from sharepoint list to be binded to a dropdown list. However, I want the first value in the dropdown to be empty. I cannot insert a null value in the sharepoint list item.
Please let me know how can I do it programmatically.
Below is the code I am using to bind the list to dropdownlist
if (Fldname.Contains("xxxxxx"))
{
ddllist.DataSource = data.getCode();
ddllist.DataTextField = "Title";
ddllist.DataValueField = "Alphabetic_x0020_Code";
ddllist.DataBind();
ddllist.SelectedValue = string.Empty;
ddllist.Width = 120;
}
there has to be something like this. Where 0 is the position you want the new item be - which is the first in this case. Can do this after the databinding.
ddllist.Items.Insert(0 , string.empty);
I've gotten a checkbox header renderer to work well with flat DPs, but a
hierarchical collection view is another story. On click, I want it to select all
checkboxes in a given column. Here is my code:
var dp:HierarchicalCollectionView = _dataGrid.dataProvider as
HierarchicalCollectionView;
var testDp:GroupingCollection = dp.source as GroupingCollection;
var rawDp:ArrayCollection = testDp.source as ArrayCollection;
for(var i:int=0 ; i < rawDp.length ; i++){
rawDp[i][_dataField] = cb.selected;
}
It selects all checkboxes on the 2nd level of data, but doesn't select the top
level of data. What am I missing here? I can't seem to find it.
Any tips are greatly appreciated. Thank you.
For hierarchical data you have to use a cursor which iterates over all levels of the hierarchical data.
var dp:IHierarchicalCollectionView = _dataGrid.hierarchicalCollectionView;
var cursor:IViewCursor= dp.createCursor();
while (!cursor.afterLast)
{
cursor.current[_dataField] = cb.selected;
cursor.moveNext();
}
Howerver, this works only with nodes that have previously been opened. So either expand all nodes with _dataGrid.expandAll() (you can collapse them afterwards since the nodes only have to be opened once) or iterate your hierarchical data manually:
function setCheckBoxValue(children:ArrayCollection, value:Boolean):void
{
for each (var child:Object in children)
{
if (child.hasOwnProperty("children") && child["children"])
setCheckBoxValue(child["children"], value);
child[_dataField] = value;
}
}
var myDataProvider:HierarchicalData = /* your data provider */;
// Call it like this...
setCheckBoxValue(myDataProvider.source, cb.selected);
Update: To answer your second question...
Create a new CheckBoxColumn which extends AdvancedDataGridColumn. You can use it to preconfigure your headerRenderer and itemRenderer.
In your custom item renderer you get hold of your column like this:grid = AdvancedDataGrid(listData.owner);
column = grid.columns[listData.columnIndex] as CheckBoxColumn;
Do the same in your header renderer.
Whenever the CheckBox value in one of your item renderers changes dispatch a event through your column. Something like: column.dispatchEvent(new Event("checkBoxValueChanged"));
Your header render should add an event listener to the column for the "checkBoxValueChanged" event (or whatever you call it). Whenever that event is fired loop through your data provider and update the headers CheckBox accordingly.
In theory that should work. HTH
I have a listView control which is bound to an object datasource. In the ListView1_ItemDataBound event, I am generating some dynamic controls. I do this because depending on a particular column value, i might need to generate textbox, radio button, check box etc.
Some code here:
ListViewDataItem item = (ListViewDataItem)e.Item;
typez = Convert.ToInt32(DataBinder.Eval(item.DataItem,"Type").ToString());
if (typez == 1) //1 means generate radibutton
{
string[] options = DataBinder.Eval(item.DataItem, "QuestionDetail").ToString().Split(new string[] { delimiter }, StringSplitOptions.None);
questionID = Convert.ToInt32(DataBinder.Eval(item.DataItem, "Question_ID").ToString());
int optionCount = 1;
RadioButtonList rbl = new RadioButtonList();
//set ID for the radiobtnList to the questionid no.
rbl.ID = "mcq_" + questionID;
foreach (string s in options)
{
//adds the MCQ options to list item
ListItem li = new ListItem(Util.GetAlphabet(optionCount).ToUpper() + ". " + s, Util.GetAlphabet(optionCount).ToUpper(), true);
rbl.Items.Add(li);
optionCount++;
}
//PlaceHolder PlaceHolder1 = (PlaceHolder)e.Item.FindControl("PlaceHolder1");
PlaceHolder1.Controls.Add(rbl);
I have DataPager also. When I go to the next page I want to somehow capture the user response in the previous page. Otherwise, all the user response is lost. I tried using ListView1_PagePropertiesChanging event. But here I somehow do not seem to get the dynamic controls in the page.
I need to get the radio button selected value so that I can save it or put in some session variable so that when user comes back to this page he can see his previuos values.
Could someone please suggest some insight on what I am doing wrong.
Problem is that you have to create controls in OnInit, then ViewState persister can do it's job. If you do it after OnInit, in ItemDataBound, when Postback is triggered, controls doesn't exists and their's viewstate isn't deserialized.
To access values of radio buttons and get selected one, you have to look at Request.Form dictionary and find it.