Telerik RadComboBox AutomaticLoadOnDemand - asp.net

I use Telerik RadComboBox in my project. I set EnableAutomaticLoadOnDemand="true" on RadComboBox. It works good but when I want to set selected item on load of page event, it doesn't show selected item

With load on demand mode, so combobox does not have any item. It just has item(s) when you action on it.
In my opinion, you should get the specific item and add it to combobox manually on page load event like this way (I'm not sure the structure, just an idea.)
if (!IsPostBack)
{
var itemSelected = service.GetById(Id); //item Id
this.combobox.Items.Add(new RadComboboxItem(itemSelected.Id, itemSelected.Name));
this.combobox.SeletedValue = Id.ToString();
}

RadCombo allows to set up SelectedValue and Text properties even through there are no items in it.

Related

Weird behavior while creation of two Telerik RadComboBox items programmatically

I've two Telerik RadComboBox controls, while adding of their items using the following code
foreach (var gate in Enum.GetNames(typeof(AuthorizedGates)))
{
var item = new RadComboBoxItem(gate, Convert.ToString((int)Enum.Parse(typeof(AuthorizedGates), gate)));
ddlTelerik1.Items.Add(item);
ddlTelerik2.Items.Add(item);
}
at runtime, the first combobox has zero items i.e. items are not added to it while items are added to the second one!
I tried the same for ASP.NET DropDownList using the following code
foreach (var gate in Enum.GetNames(typeof(AuthorizedGates)))
{
var item = new ListItem(gate, Convert.ToString((int)Enum.Parse(typeof(AuthorizedGates), gate)));
ddlAspNet1.Items.Add(item);
ddlAspNet2.Items.Add(item);
}
it is working properly and items got added to both of them.
Any ideas what could be the reason behind this weird behavior?
The difference is that the RadComboBox item is a control like the RadComboBox or DropDownList controls, so it's a class that indirectly inherits from WebControl, and can only have one instance on the page. It's not like a ListItem (which inherits from Object).

Change databound Drop Down List programmatically

I have a drop down list that is populated in the page load event from a database table.
The drop down has a DataTextField set to a project name and the DataValueField set to the project id (interger).
Later I change the dropdowlist selected item with this code in the selectedindexchanged event of a gridview
GridViewRow row = GridView1.SelectedRow;
ddlProjectList.SelectedItem.Text = row.Cells[2].Text;
Does Changing the drop down list with this code cause the DataValueField property to change to the correct Project ID number also? If not is there a better way to do this?
=============================================EDIT
actually this code seems to be adding an additional item to the list so that the project i set with this code is listed twice so I don't think my code is correct
This does not change anything else than the text of the selected item in your DropDownList. Do you want to change the text and value of the project, or do you want to select the project from the DropDownList that relates to the selected row in the grid?
To change the project name and id, you'll have to change it in the data source behind the ProjectList. But if you just want to select the related project, you can use this:
var row = GridView1.SelectedRow;
if (ProjectList.Items.FindByText(row.Cells[2].Text) != null)
{
ProjectList.ClearSelection();
ProjectList.Items.FindByText(row.Cells[2].Text).Selected = true;
}
Setting SelectedItem.Text does not actually change the selected item by its text, it changes the text of the currently selected item. You need to use ddl.Items.FindItemByText (or it may be worded FindByText, I forget at the moment) to find the item, then set the ListItem.Selected property to true.
HTH.
You can do
ddlProjectList.FindByText(row.Cells[2].Text).Selected = true;
This will actually set it.

Flex extending ComboBox

I created a class CustomCombo.as that extends ComboBox. What is happening is that the CustomCombo combobox is showing as being editable. I do not want this and I cant find the properties to set the editable to false.
I also tried setting the combobox's textInput.editable control to false, but to no avail.
Any help would be greatly appreciated.
CustomCombo.as
package custom {
import spark.components.ComboBox;
public class CustomCombo extends ComboBox {
public function CustomCombo() {
super();
// this.editable = false; //<-- THIS DOESNT WORK ***Access of possibly undefined property editable through a reference with static type custom:CustomCombo
// this.textInput.editable = false; //<-- THIS DOESNT WORK ***Cannot access a property or method of a null object reference
}
}
}
After rummaging through the Flex 4 API I found that they suggest to use the DropDownList control. From what I can see is that they removed the editable property from the ComboBox control in Flex 4, but I may be wrong.
I implemented DropDownList and that solved my problem.
I see that you're using spark and not mx. The editable property I referred to is applicable only to mx based list. In spark, ComboBox extends DropDownListBase and is editable by default.
The ComboBox control is a child class of the DropDownListBase control. Like the DropDownListBase control, when the user selects an item from the drop-down list in the ComboBox control, the data item appears in the prompt area of the control.
One difference between the controls is that the prompt area of the ComboBox control is implemented by using the TextInput control, instead of the Label control for the DropDownList control. Therefore, a user can edit the prompt area of the control to enter a value that is not one of the predefined options.
For example, the DropDownList control only lets the user select from a list of predefined items in the control. The ComboBox control lets the user either select a predefined item, or enter a new item into the prompt area. Your application can recognize that a new item has been entered and, optionally, add it to the list of items in the control.
The ComboBox control also searches the item list as the user enters characters into the prompt area. As the user enters characters, the drop-down area of the control opens. It then and scrolls to and highlights the closest match in the item list.
So ideally, you should be using DropDownList in this case.
You're getting null error when trying to access textInput from the constructor because it hasn't been created yet. In mx based controls (Flex-3), you can access it from the creationComplete handler; I'm not quite sure how to do it for spark based controls.
Update: I think I've figured out how to access skin parts in spark (though you might wanna use the DropDownBox instead). You have to override the partAdded method.
override protected function partAdded(partName:String, instance:Object):void
{
super.partAdded(partName, instance);
if (instance == textInput)
{
textInput.editable = false;
}
}
There's one catch though: it may not work in this case. The source code of ComboBox.as says that
the API ignores the visual editable and selectable properties
So DropDownList it is!
Initial answer, posted for mx ComboBox.
This shouldn't happen as the default value of the editable property is false.
Try explicitly setting the value to false from the constructor.
public function CustomCombo() {
super();
this.editable = false;
}

How do I programatically put telerik rad grid in "add new" mode on page load

Seems like this should be easy but I must just be missing something... I have a Telerik RadGrid on a page that allows inline editing. How do I programatically put the grid into edit mode to insert a new row into the grid. When the page loads I would like show the existing data and also display 1 empty row that a user can easily type into to add a new record to the table. (I don't want them to have to push the add new button)
Found the answer while back.... updating this in case others need it
RadGrid1.MasterTableView.IsItemInserted = true;
RadGrid1.Rebind();
Lifesaver!!
You can set
radGrid1.MasterTableView.IsItemInserted = false;
radGrid1.Rebind();
that will remove the inserted item (like pressing cancel).
If you need show inset form always you can use next:
protected void NeedDataSource(object sender, GridNeedDataSourceEventArgs e) {
parametersGrid.DataSource = data;
parametersGrid.MasterTableView.IsItemInserted = true;
}
You could try using jQuery to press your add button once the page is ready.
Something along the lines of -
$(document).ready(function() {
$("#addButton").click();
}
What I did when I wanted to do the same with the Telerik grid is to set the MasterTableView.IsItemInserted property of the control to true inside the OnNeedDataSource event handler. I suppose that it should work if you set the property inside the OnDataBound grid handler as well.
Dick
RefreshGrid(userName, "priority", true, false);
RadGrid radGrid = RadGrid1;
radGrid.MasterTableView.InsertItem();
radGrid.Rebind();

Set HTML input checkbox name in listview

I am trying to add a checkbox in a listview with value as ids of the records from the database so I can allow the user to check the ones they want to delete and when they click the delete button I can get value collection of checkbox with request.form.
My problem is, because checkbox in a listview ASP.NET renders the listview name into the name property of the checkbox, it prevents me to do request.form["checkboxname"].
I don't want to use Listviews delete commands but simply use request.form to get the collection of checked values.
How can I set name of the htmlinput checkbox so .NET doesn't change it in render time?
I have tried:
ListViewDataItem dataItem = (ListViewDataItem)e.Item;
HtmlInputCheckBox _CheckBoxDelete = (HtmlInputCheckBox)e.Item.FindControl("CheckBoxDelete");
_CheckBoxDelete.Visible = true;
_CheckBoxDelete.Value = DataBinder.Eval(dataItem.DataItem, "id").ToString();
_CheckBoxDelete.Name = "deletechecked";
But still it renders like:
<input name="PmList$ctrl0$CheckBoxDelete" type="checkbox" id="PmList_ctrl0_CheckBoxDelete" value="3" />
This is happening because ListView is a Naming Container. You can get around this in a couple of ways, but they all boil down to the choice of:
Rendering the HTML you want.
Pulling out the checked items in a different way.
The former is doable, but you'll likely loose a lot of ASP.NET's built in functionality. I'd advise against it unless you're deeply familiar with the control life cycle.
You've got everything you need for the pulling the values out in a way ASP is expecting:
HtmlInputCheckBox _CheckBoxDelete = (HtmlInputCheckBox)item.FindControl("CheckBoxDelete");
You just need to wait for the control hierarchy to be populated, and then loop over the ListView.Items looking for the checkboxes. Your "Delete" button's event handler is probably a good place to call this from.
Incidentally, why are you using a HtmlInputCheckbox, rather than a CheckBox?
I have sorted it out with:
string idCollectionTodelete = string.Empty;
foreach (string x in Request.Form)
{
if (x.IndexOf("CheckBoxDelete") > -1)
{
idCollectionTodelete += Request.Form[x] + ",";
}
}
new DB().DeleteUserPm(
ActiveUsername(), subdomain, idCollectionTodelete.TrimEnd(','));
It is not an ideal solution but it does work for me.
I do this
List<HtmlInputCheckBox> chkDeleteContacts = new List<HtmlInputCheckBox>();
foreach (RepeaterItem item in rptrFamilyContacts.Items)
{
chkDeleteContacts.Add((HtmlInputCheckBox)item.FindControl("chkDeleteContact"));
}
foreach(HtmlInputCheckBox chkDeleteContact in chkDeleteContacts)
{
//Delete Contact
if(chkDeleteContact.Checked)
blnStatus = BusinessUtility.DeleteConsumerContact(LoginConsumerID, chkDeleteContact.Value);
}
Slightly easier in my opinion

Resources