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.
Related
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
I have a dropdownlist, which is getting bind with the datasource on page load.
On SelectedIndexChanged event I need to get multiple values from SelectedItem
I don't want to get one value and call database/session/viewstate to get object with the value.
I search if I can add multiple DataValueField but seems it is not possible, Thus I am thinking to add an attribute to Item and assign additional value field to this attribute.
How can I bind attribute to DDL Item? I tried DataBinding event but don't know how to write code for it. Can anybody help? This is normal asp.net not MVC.
I also tried following (this works for Repeater control):
<asp:DropDownList ID="SetTypes" IsProfiled="<%# DataBinder.Eval(Container.DataItem, "SiteID") %>" runat="server" Width="250" AutoPostBack="True"></asp:DropDownList>
but it seems Container does not have DataItem property.
Try this out:-
SetTypes.DataSource = new List<string>(){
"Type1","Type2","Type3"};
SetTypes.DataBind();
foreach (ListItem item in SetTypes.Items)
{
//Add multiple attributes
item.Attributes.Add("CustomAttribue", "CustomValue");
}
I have one Listbox named "List1" and one button says "Append".
I have on textbox named "TextDescription".
I want to put the select values from the listbox to textbox on click of append button.
So can anyone tell me how to do this?
You could use the .val() function. So assuming you have a select with id="myselect" and a text input with id="mytext" you could do this:
var values = $('#myselect').val();
if (values != null) {
// concatenate the selected values with , so that we can add them to the textbox
$('#mytext').val(values.join(','));
}
And here's a live demo illustrating it in action.
I have an asp dropdownlist that I would like to make a panel visible if the selection contains a certain word- how would this be possible?
lets say you have dictionary like this
List<string> words = new List<string>();
words.Add("foo");
then onchange event of drop down list
string selectedText = ddlPanel.SelectedText;
foreach(var w in words)
{
if ( w.Contains(selectedText)
{
pnl.Visible = true;
}
}
<select onchange="if (this.options[this.selectedIndex].value.indexOf('foo') != -1) document.getElementById('panel').style.display = 'block'">
Place the panel you're looking to hide within an update panel, and use the dropdownlist change as a trigger to the updatepanel. On the update, check the dropdownlist's value and set the visibility.
Either that, or if you know the ID of the panel you can manually use javascript and bind the change event to a function that checks the values and shows/hides the panel accordingly.
Create dropDownlist with two items, "visible" and "not visible" or whatever suits you and make sure to set the autopostback property to true.
Then in vb write the following on page load:
If ddlMydropdown.Text = "visible" then
panelId.Visible = true
else
panelId.Visible = false
End If
If you code in c#, you can convert this vb code to c# over at developerfusion
For my ASP.NET page, in the code behind I load various items/options into a DropDownList but I do not set a default by assigning SelectedIndex. I notice for the postback SelectedIndex is set to 0, even if I never set focus to or changed the value in the DropDownList.
If not otherwise specified in the code behind or markup, will a default value of 0 be used for the SelectedIndex in a postback with a DropDownList? If yes, is this part of the HTML standard for selection lists, or is this just the way it is implemented for ASP.NET?
This is normal behavior for the HTML SELECT control.
Unless the selected property is used, it will always start at the first item (index of 0).
In many cases, I don't want this in ASP.NET because I specifically want the user to select an option.
So what I do is add a blank item which I can then validate.
i.e.
<asp:DropDownList runat="server" DataSourceID="SomeDS" AppendDataBoundItems="true"> // Notice the last property
<asp:ListItem Text="Please select an option" Value="" />
</asp:DropDownList>
This way, I can set a validator which checks if the value is blank, forcing the user to make a selection.
For anybody looking for a way to do this from the code behind, you can add the extra list item there by using the Insert function and specifying an Index of 0, as mentioned in this SO Question. Just make sure to do so after you've called DataBind()
myDropDownList.DataSource = DataAccess.GetDropDownItems(); // Psuedo Code
myDropDownList.DataTextField = "Value";
myDropDownList.DataValueField = "Id";
myDropDownList.DataBind();
myDropDownList.Items.Insert(0, new ListItem("Please select", ""));