javafx combobox of object how to get selection - javafx

I have a comboBox i have populated using student from an arraylist:
ComboBox<Student> comboBox = new ComboBox();
for (int i = 0; i < control.getSize(); i++) {
comboBox.getItems().add(control.returnElement(i));
How do I now on a Button press know which Student object the user has selected so I can pass it into a method, I might be able to work with just knowing the index too
I tried it like this but it gives me an error:
Student studentSelect = comboBox.getItems();

with comboBox.getValue(); or comboBox.getSelectionModel().getSelectedItem();

Related

Binding childitem into menu control asp.net

DataRow[] drowpar = dt.Select("Parent_Id=" + 0);
foreach (DataRow dr in drowpar)
{
MenuItem objMenuItem = new MenuItem();
objMenuItem.Text = dr["Page_Name"].ToString();
objMenuItem.NavigateUrl = dr["Page_Url"].ToString();
MenuBar.Items.Add(objMenuItem);
}
foreach (DataRow dr in dt.Select("Parent_Id >" + 0))
{
MenuItem objMenuItem = new MenuItem();
objMenuItem.Text = dr["Page_Name"].ToString();
objMenuItem.NavigateUrl = dr["Page_Url"].ToString();
//MenuBar.FindItem(dr["Parent_Id"].ToString()).ChildItems.Add(objMenuItem);
MenuBar.FindItem(dr["Parent_Id"].ToString()).ChildItems.Add(objMenuItem);
//MenuBar.Items.Add(objMenuItem);
}
i am binding asp.net menu control using database and getting this below in childitem binding to menu
Object reference not set to an instance of an object.
MenuBar.FindItem(dr["Parent_Id"].ToString()).ChildItems.Add(objMenuItem);
That error message only happens for specific situations, and the possible reasons are:
objMenuItem is null - which obviously in your code it is not
dr["Parent_Id"] is null, and calling ToString() causes the error
MenuBar.FindItem(dr["Parent_Id"].ToString()) - this isn't finding the item with the given parent ID
The ChildItems collection on the menu item is null
If you want to be really sure where the object is not null, then you can do the following:
var id = dr["Parent_ID"].ToString();
var menuItem = MenuBar.FindItem(id);
if (menuItem != null)
menuItem.ChildItems.Add(objMenuItem);
Having the code together masks where the actual error occurs. Note, there is one other scenario where i didn't account for, which is the error is within the MenuBar itself, and it's masked. Without seeing the full stack trace, it's hard to tell. If you can post that, we can isolate it further.

replacing items in dropdownlist

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

RadComboBoxItems not being added to multiple RadComboBoxes

All I'm really trying to do is populate three different comboboxes with the same item. When this code executes below only one combobox is getting the values. Any help is welcomed. Thanks!
for (int i = 0; i < 100; i++)
{
RadComboBoxItem li = new RadComboBoxItem();
li.Text = i.ToString();
li.Value = i.ToString();
InputPairThousandValueComboBox.Items.Add(li);
InputUsertThousdandValueComboBox.Items.Add(li);
InputCurrentlyListedThousdandComboBox.Items.Add(li);
}
I couldn't find anything in the Telerik docs that says this explicitly, but it appears a single instance of a RadComboBoxItem can only be contained in a single RadComboBox; you can't share a RadComboBoxItem between controls.
The docs do hint to this: the RadComboBoxItem has an 'owner' property that is a reference to the RadComboBox that contains the item (implying that there can be only 1 owner)
Under the covers, the second & third Add(...) calls most likely first remove the item from the combo box its already in.
So, you'll have to create a separate RadComboBoxItem for each RadComboBox. Here's one way you could do it using the RadComboBoxItem constructor that takes the text and value as arguments.
for (int i = 0; i < 100; i++)
{
var val = i.ToString();
InputPairThousandValueComboBox.Items.Add(new RadComboBoxItem(val, val));
InputUsertThousdandValueComboBox.Items.Add(new RadComboBoxItem(val, val));
InputCurrentlyListedThousdandComboBox.Items.Add(new RadComboBoxItem(val, val));
}
For those still running into this issue, a way I found is adding the items as a dictionary, then binding with a datasource.
link to telerik form
Dictionary<string, string> comboSource = new Dictionary<string, string>();
comboSource.Add("", "");
comboSource.Add(user.Rs.GetString("txtDate"), "phDate");
comboSource.Add(user.Rs.GetString("txtBranch"), "mhBranch");
comboSource.Add(user.Rs.GetString("txtDepartmentCode"), "mhDepartmentCode");
comboSource.Add(user.Rs.GetString("txtLocationCode"), "mhLocationCode");
comboSource.Add(user.Rs.GetString("txtModelCode"), "mhModel");
comboSource.Add(user.Rs.GetString("txtProductCode"), "phProductCode");
comboSource.Add(user.Rs.GetString("txtShiftCode"),"shShiftCode");
comboSource.Add(user.Rs.GetString("txtSubcategory"),"phSubcategory");
cboSort1.DataSource = cboSort2.DataSource = cboSort3.DataSource = cboSort4.DataSource = comboSource;
cboSort1.DataTextField = cboSort2.DataTextField = cboSort3.DataTextField = cboSort4.DataTextField = "Key";
cboSort1.DataValueField = cboSort2.DataValueField = cboSort3.DataValueField = cboSort4.DataValueField = "Value";
cboSort1.DataBind();
cboSort2.DataBind();
cboSort3.DataBind();
cboSort4.DataBind();

Why is my ASP.NET Button control not calling the appointed function?

I want to set up a search functionality on my web site. The user types some information into a text box and click a search button. Upon clicking the search button, a database is searched using the text in the text box, and the results are displayed in a table. If the text in the text box matches one result from the database perfectly, rather than displaying a list of results the page is populated with detailed information about the matching result.
In order to make it easier to match a result exactly, I want to add a button next to each result which "selects" that result, filling the text box with that result's text and therefore populating the page with the details. Here's what I have.
Upon clicking the search button, after the check to see if a result is matched exactly, I create a table which contains the results and the buttons:
for(int x=0; x < res_list.Length; x++)
{
TableRow newRow = new TableRow();
TableCell textCell = new TableCell();
TableCell buttonCell = new TableCell();
buttonCell.ID = "bc" + x;
Button cellButton = new Button();
cellButton.ID = "btn" + x;
textCell.Text = res_list[x];
textCell.Attributes.Add("Width","60%");
cellButton.Text = x.ToString();
// cellButton.OnClientClick = "NameClick"; This property refers to client-side scripts, which I am not using.
cellButton.Click += new EventHandler(NameClick);
buttonCell.Controls.Add(cellButton);
newRow.Cells.Add(firstCell);
newRow.Cells.Add(buttonCell);
myTable.Rows.Add(newRow);
}
I have tried both the OnClientClick method and the Click method seen above, both of which have yielded the same results.
My NameClick function is as follows:
void NameClick(object sender, EventArgs e)
{
Button sendButton = (Button)sender;
int index = Int32.Parse(sendButton.Text);
SearchTextBox.Text = myTable.Rows[index].Cells[0].Text;
return;
}
I set up a breakpoint at the beginning of the NameClick function, and when I click one of these buttons it is never reached. Why is this function not being called by my buttons?
EDIT: I want to accomplish this without using JavaScript, if possible.
You use cellButton.OnClientClick = "NameClick";, but do you actually have a NameClick function in your javascript? If not, clicking the button will cause a JS error which will probably block the postback.
Other than that, it is advisable to assign explicit ID's to controls which you create programatically. Otherwise the autogenerated ID's may change on postback, which will prevent control events from firing.
Something like the following should work (not tested):
myTable.ID = "someid";
for (int x=0; x < res_list.Length; x++)
{
TableRow newRow = new TableRow();
newRow.ID = "r" + x;
TableCell textCell = new TableCell();
TableCell buttonCell = new TableCell();
buttonCell.ID = "bc" + x;
Button cellButton = new Button();
cellButton.ID = "btn" + x;
textCell.Text = res_list[x];
textCell.Attributes.Add("Width","60%");
cellButton.Text = x.ToString();
//cellButton.OnClientClick = "NameClick"; // not needed unless you have actual JS for it
cellButton.Click += new EventHandler(NameClick);
buttonCell.Controls.Add(cellButton);
newRow.Cells.Add(firstCell);
newRow.Cells.Add(buttonCell);
myTable.Rows.Add(newRow);
}
cellButton.OnClientClick should point to a client-side function, i.e. JavaScript. Do you have a JS function somewhere?
If the answer is no, then we've found your problem. You should create one. It will be helpful to set your ClientIDMode property of your dynamically generated controls to Static or Predictable, so your JS function can easiliy access them.
Do you need assistance with the JS function?
when you click on the button, the page start the life cycle again and your control Does not exist in the page you need to create the control again or save it in the viewstate or use control state
when he call back in the life cycle
in the page life cycle event SaveControlState, loadControlState
if you want to create a javascript click event you need to make sure that the control doesnt call to server side event for this in the javascript function you need to return false
you have to add this event in your asp syntax for button.
<asp:Button runat="server " OnClick="ButtonClick"/>

Flex - combobox data

I'd like to create a combobox which will get users from an arrayList & another field for creating a new user.
for example when i open the combobox i'd like to see the rows :
Create new user
User A
User B
where the users come form an arrayList and the "create new user" option is always there.
How can I do it?
P.S
I don't wan't to add "create new user" obj to the arryList - so this is not a desired solution.
Thanks for your help,
Dan
Just create an intermediate ArrayList, add the "create new user" option to it, copy all the elements from the other ArrayList, and finally assign it to the combobox:
var cbArray = new ArrayList();
cbArray.push("Create new user ");
for (int i = 0; i < yourArrayList.lenght; i++) {
cbArray.push(yourArrayList[i]);
}
// Then assign cbArray to the combo box

Resources