Empty Message showing in RadComboBox - asp.net

I have a RadComboBox with an empty message on my page inside a radgrid. The combobox is populated in the ItemDataBound event for the containing grid. The empty message shows in each radcombobox on the page.
My problem is that the position may have been selected previously & although I could get the value to display, since putting in the empty message, it does not show this value. The empty message is shown, even though this particular combobox is not empty.
The previously selected item does appear in the drop down but the radcombobox seems to be thinking it is still empty. Have I missed something?
asp:
<telerik:RadComboBox ID="cboPosi" runat="server" DataSourceID="LabourDataSource" AllowCustomText="True"
DataTextField="Pos" DataValueField="PosDesc" EnableAutomaticLoadOnDemand="true" ShowMoreResultsBox="true"
EnableVirtualScrolling="true" ItemsPerRequest="10" EmptyMessage="Type here">
</telerik:RadComboBox>
vb.net:
Dim combo As RadComboBox = DirectCast(item.FindControl("cboPosi"), RadComboBox)
Dim selectedItem As New RadComboBoxItem()
selectedItem.Text = selectedTitle
selectedItem.Value = selectedVal
combo.Items.Add(selectedItem)
selectedItem.DataBind()

When using LoadOnDemand you cannot add items to the RadComboBox, you can however set the Text and SelectedValue attributes to mimic selecting an option (See here).
You might also find this Telerik article useful

The problem turned out to be that I was not actually selecting the new item I had made. Without an empty message, this item was being displayed as selected.
Setting the inserted item as the selected item causes it to be displayed correctly instead of the empty message:
Dim combo As RadComboBox = DirectCast(item.FindControl("cboPosi"), RadComboBox)
Dim selectedItem As New RadComboBoxItem()
selectedItem.Text = selectedTitle
selectedItem.Value = selectedVal
combo.Items.Add(selectedItem)
selectedItem.DataBind()
combo.SelectedIndex = 0

Related

Radcombo box not preserved the selected item value

<telerik:RadComboBox ID="radcmb" runat="server" MarkFirstMatch="true">
</telerik:RadComboBox>
radcmb.Items.Clear();
radcmb.Text = String.Empty;
radcmb.DataSource = DS;
radcmb.DataTextField ="DESCRIPTION";
radcmb.DataValueField ="ID";
radcmb.DataBind();
and i am setting the datasource of the combo box on page_load event, the problem is whenever i have select any item from the combo box and click outside / any other section of the page, its de-select the item and select the first item from the list.
i tried below solution - but it didn't helped me.
RadComboBox changing selected item when clicking on other parts of page

Items of a ListView have DropDownList: How to get the DataItem when selection changes?

So I have something like this:
Under the "Products" ComboBox there is a ListView that displays the new items that are added when the user clicks the "Add" button to add the selected product.
When the user makes a Product Descriptor selection for a product, I need to change a property of the associated data bound object. How do I access that object? I have a handler for the SelectedIndexChanged event of a given Product Descriptor ComboBox, but how do I get the DataItem of the row containing the ComboBox that had its selection changed?
I thought about ListView's ItemCommand event, but I can't see how I would use it in this case.
I also saw this post, in which one answer mentions storing ids in hiddenfields:
DropDownList inside Repeater: How to handle SelectedIndexChange and get DataItem?
But in that case, how would I get the Ids from those hidden fields?
Thanks for your help!
You just have to cast the NamingContainer of the DropDownList:
var ddl = (DropDownList) sender;
var item = (ListViewItem) ddl.NamingContainer;
var rowView = (DataRowView) item.DataItem;
Tim Schmelter's answer led me to this answer:
Dim comboBox = CType(sender, RadComboBox)
Dim item = CType(comboBox.NamingContainer, ListViewItem)
Dim myListItem = myCollection(item.DataItemIndex)

Why is my RadioButtonList selectedValue empty?

I have a RadioButtonList:
<asp:RadioButtonList ID="rblMedicationTime" runat="server" onselectedindexchanged="rblMedicationTime_SelectedIndexChanged" DataSourceID="dtsMedicationTime" DataTextField="LookupItem" DataValueField="Id" AutoPostBack="true"></asp:RadioButtonList>
On page load, I want to select a radio button from the list and set its value, for which I have written this line of code:
rblMedicationTime.SelectedValue = clientMedicationSchedule.glTypeId.ToString();
The RadioButtonList is populating successfully, but the value is unable to be selected.
rblMedicationTime.SelectedValue is always "" when I debug the code.
You just need to use
string myValue = myRadioButtonList.SelectedItem.Value
The property object myRadioButtonList.SelectedItem contains all values from the selected item of a Radio Button list or a DropDown list
to set the value programmatically all you have to do is:
myRadioButtonList.SelectedIndex = 0;
You can see that you have several ways to Get but only one to Set:
myRadioButtonList.SelectedIndex --> Gets or Sets
myRadioButtonList.SelectedValue --> Gets
myRadioButtonList.SelectedItem --> Gets
You can't set the selected radiobutton with .SelectedValue, only with .SelectedIndex.
Check MSDN (at SelectedValue it says "Gets the value", at SelectedIndex is says "Gets or sets")
I think problem in !IsPostBack.
if (!IsPostBack)
{
string str = rblMedicationTime.SelectedValue;
}
First you should check !IspostBack
hello friend there is something another problem in your code because in my side this is running fine.
rblMedicationTime.SelectedValue = clientMedicationSchedule.glTypeId.ToString();
can u cehck that clientMedicationSchedule.glTypeId.ToString() this contain value or not.
and on page load u put selection code on if (!IsPostBack){} block.

Adding Checkbox in asp.net Listview control to allow multiple delete

I am trying to display checkboxes in front of every row in list view. So that after selecting the desired checkboxes user clicks on delete button and we should delete that records.
but how can it be done?
Add Checkbox in markup
<asp:CheckBox ID="ChkSelect" runat="server" />
code behind as follows:
Dim ChkSelect As CheckBox = Nothing
Dim ListItem As ListViewDataItem = Nothing
Dim ItemList As List(Of Person) = New List(Of Person)
Dim Item As Person= Nothing
For Each ListItem In MyDataList.Items
ChkSelect = ListItem.FindControl("ChkSelect")
If ChkSelect.Checked Then
Dim UIN As Integer = _
MyDataList.DataKeys(ListItem.DisplayIndex).Value.ToString()
Item = Persons.GetData(UIN)
Item.Deleted = True
ItemList.Add(Item)
End If
Next
Data = Persons.UpdateBulk(ItemList)
If Data = True Then
BindMyData()
End If
You need to create a template for the items in the ListView, put the checkbox in it, and then get all the items that were checked when you click the Delete button. You could either keep track of the selected items on the client or the server, but it would always require some work to persist them.
Here's an article on using templates with the ListView:
http://msdn.microsoft.com/en-us/library/bb398790.aspx#CreatingTemplatesForTheListViewControl
I use GridView Template if I want to do that in GridView...try to look if theres ListView Template if there is.

How to set checked property of CheckBoxList items in aspx markup?

I have a CheckBoxList in my page, with the DataTextField and DataValueField attributes set, is there an attribute that I can use to specify a property that indicates if it should be checked?
I'm hoping to just set the datasource, and not have to have any code behind to set the checked property. Possible?
No it's not possible because the control uses the same binding as other controls such as ListBox, DropDownList, RadioButtonList, etc.
As per MSDN:
To set multiple selections in a list
control programmatically loop through
the control's Items collection and set
the Selected property of every
individual item.
You could implement the OnDataBinding of the CheckListBox and then do a lookup for each item that gets bound but it would probably just be easier to do it all in one place.
lock at this example:
string[] strUserRoles = Roles.GetRolesForUser("Ali");
foreach (var item in Roles.GetAllRoles())
{
chkRoleList.Items.Add(new ListItem()
{
Text = item,
Value = item,
Selected = strUserRoles.Contains(item)
});
}
Note: when you bind CheckListBox, you must set Both of Text and Value of each Item.

Resources