Drop down should not display all the dropdown values - asp.net

The drop down I am using is statically bound.
<asp:DropDownList ID="StartTime" runat="server" BackColor="#FFFF80" Font-Names="Arial"
Font-Size="Small" style="text-align: center" Width="56%">
<asp:ListItem Value="12:00 AM"></asp:ListItem>
<asp:ListItem Value="12:30 AM"></asp:ListItem>
<asp:ListItem Value="1:00 AM"></asp:ListItem>
<asp:ListItem Value="1:30 AM"></asp:ListItem>
<asp:ListItem Value="2:00 AM"></asp:ListItem>
<asp:ListItem Value="2:30 AM"></asp:ListItem>
<asp:ListItem Value="3:00 AM"></asp:ListItem>
</asp:DropDownList>
Later I am retrieving the inserted value from DB and displaying it to the user from front end like:
ss.Append("FRE_PLANNED_START_DATE = '" & PlannedStartDate.Text & "',")
ss.Append("FRE_PLANNED_COMP_DATE = '" & CompDate.Text & "',")
Is it possible to show only the selected value and not the other drop down option to the user?
For example, while inserting the selected 12:00 AM and again front end should show only 12 and not display remaining drop down options.

You can hide one by setting its Enabled property to false:
StartTime.Items[0].Enabled = false;
You can loop it to disable all but one selected.
Wouldn't disabling DropDownList work as well?

Related

ASP.NET Set RadioButton to Select by a String Value

I have a RadioButton control with 3 buttons.
I am retrieving a value from SharePoint list, and to select the RR control according to the value retrieved.
Which of the following is the correct method to use?
ResignationTypeRBList.SelectedValue = _request.ResignationType;
ResignationTypeRBList.SelectedItem = _request.ResignationType;
ResignationTypeRBList.SelectedIndex = _request.ResignationType;
<asp:RadioButtonList ID="ResignationTypeRBList" RepeatDirection="Horizontal" runat="server" CausesValidation="false">
<asp:ListItem Text="Resignation" Value="Resignation" Selected="True"></asp:ListItem>
<asp:ListItem Text="NPL" Value="NPL"></asp:ListItem>
<asp:ListItem Text="Others" Value="Others"></asp:ListItem>
</asp:RadioButtonList>
Thank you
If you are getting Value from Sharepoint list, then assign that to SelectedValue
ResignationTypeRBList.SelectedValue = "NPL";

how to select multiple items in ListBox

How can I select multiple item in ListBox in VB.NET code.
Example:
I have item 1, 5, 8 - then I want to select those items in ListBox in page load
ListBoxSample item
Item “aa1” Value =1
Item “aa2” Value =2
Item “aa3” Value =3
Item “aa4” Value =4
Item “aa5” Value =5
Item “aa6” Value =6
Item “aa7” Value =7
Item “aa8” Value =8
You first need to ensure that multiple items can be selected(default is single-selection). Therefore use the SelectionMode-property:
<asp:ListBox id="listBox1" runat="server" SelectionMode="Multiple">
<asp:ListItem Value="1">aa1</asp:ListItem>
<asp:ListItem Value="2">aa2</asp:ListItem>
<asp:ListItem Value="3">aa3</asp:ListItem>
<asp:ListItem Value="4">aa4</asp:ListItem>
<asp:ListItem Value="5">aa5</asp:ListItem>
<asp:ListItem Value="6">aa6</asp:ListItem>
<asp:ListItem Value="7">aa7</asp:ListItem>
<asp:ListItem Value="8">aa8</asp:ListItem>
</asp:ListBox>
In an ASP.NET-ListBox there is no SelectedIndices property or something like that. So you need to loop them and set the ListItem's Selected property accordingly:
string[] selectedValues = { "1", "5", "8" }; // from session or whatever
foreach (ListItem item in listBox1.Items)
item.Selected = selectedValues.Contains(item.Value);
Remember to do that only if(!Page.IsPostBack) and not on every postback in Page_Load. Otherwise you're overwriting the user-changes and the SelectedIndexChanged-event won't fire.
To enable multi selection you need to set the selection mode to multiple in windows application
ListBox1.SelectionMode = SelectionMode.MultiSimple
ListBox1.SetSelected(1, True)
ListBox1.SetSelected(5, True)
ListBox1.SetSelected(8, True)
If it is ASP listBox then you can statically selected items as
<asp:ListBox id="drop1" rows="3" runat="server" AutoPostBack="True">
<asp:ListItem selected="true">Item 1</asp:ListItem>
<asp:ListItem>Item 2</asp:ListItem>
<asp:ListItem>Item 3</asp:ListItem>
<asp:ListItem>Item 4</asp:ListItem>
<asp:ListItem selected="true">Item 5</asp:ListItem>
<asp:ListItem>Item 6</asp:ListItem>
<asp:ListItem>Item 7</asp:ListItem>
<asp:ListItem selected="true">Item 8</asp:ListItem>
</asp:ListBox>
If you want to set it dynamically means you can use
drop1.SelectionMode = ListSelectionMode.Multiple
drop1.Items(0).Selected = True
drop1.Items(1).Selected = True
drop1.Items(2).Selected = True

DropDownList - First Item in the List is not posting back properly but all the others work

I have this DropDownList:
<asp:DropDownList id="ddlSort" runat="server" EnableViewState="true" OnSelectedIndexChanged="ApplyFilters" AutoPostBack="True">
<asp:ListItem Value="0">Sort By Relevancy</asp:listitem>
<asp:ListItem Value="1">Sort By Product Code</asp:listitem>
<asp:ListItem Value="2">Sort By Product Description</asp:listitem>
<asp:ListItem Value="3">Sort By Price - Lowest</asp:listitem>
<asp:ListItem Value="4">Sort By Price - Highest</asp:listitem>
</asp:dropdownlist>
I am setting the option to be initially selected in the Page_Load in if (!Page.IsPostBack) which defaults to "0". If I select Sort By Product Code (or any other option) it will call ApplyFilters without a problem. If I then change the Drop Down to the first option, the AutoPostBack is fired but it doesn't go into the ApplyFilters code.
If I change the markup to this:
<asp:DropDownList id="ddlSort" runat="server" EnableViewState="true" OnSelectedIndexChanged="ApplyFilters" AutoPostBack="True">
<asp:ListItem Value="-1">First Option</asp:listitem>
<asp:ListItem Value="0">Sort By Relevancy</asp:listitem>
<asp:ListItem Value="1">Sort By Product Code</asp:listitem>
<asp:ListItem Value="2">Sort By Product Description</asp:listitem>
<asp:ListItem Value="3">Sort By Price - Lowest</asp:listitem>
<asp:ListItem Value="4">Sort By Price - Highest</asp:listitem>
</asp:dropdownlist>
then selecting Sort By Relevancy will work properly after selecting any other option BUT selecting First Option won't (as in the first example).
I am using ASP.NET 2.0.
I have checked the control hierarchy and the view state is enabled for all the controls.
I have tried explicitly setting EnableViewState="true" without any success.
Does anyone have any ideas as all the information I have seen on this points to the view state being false and in an update panel - Neither of which are true in my case.
EDIT: I have put a dummy entry in for now as the first entry but this is not ideal.
This is the expected behavior.
Your event is OnSelectedIndexChanged, if open the dropdown and select the already selected option, asp.net will not detect that as a change.
Only when you select a different value, will the OnSelectedIndexChanged be fired.
I suggest having a default value in place, like in your second example, just more readable:
<asp:DropDownList id="ddlSort" runat="server" EnableViewState="true" OnSelectedIndexChanged="ApplyFilters" AutoPostBack="True">
<asp:ListItem Value="-1">Select Sort order</asp:listitem>
<asp:ListItem Value="0">Sort By Relevancy</asp:listitem>
<asp:ListItem Value="1">Sort By Product Code</asp:listitem>
<asp:ListItem Value="2">Sort By Product Description</asp:listitem>
<asp:ListItem Value="3">Sort By Price - Lowest</asp:listitem>
<asp:ListItem Value="4">Sort By Price - Highest</asp:listitem>
</asp:dropdownlist>
If you have a default sort order, you can set it on page load
if (!Page.IsPostBack)
{
ddlSort.Text = 0;
}

dropdown value not refresh in gridview

I am using a DropDownList in a GridView like so:
<asp:DropDownList ID="drpstatus" Style="outline: 0" runat="server" SelectedValue='<%#Eval("status_value")%>'>
<asp:ListItem Text="Inactive" Value="0"></asp:ListItem>
<asp:ListItem Text="Active" Value="1"></asp:ListItem>
<asp:ListItem Text="Rejected" Value="2"></asp:ListItem>
</asp:DropDownList>
When I bind the first time it shows the correct selected value,
but when I change the value through another webform
and refresh this page the value still remains same.
I checked the database; it has changed but
it is not changing in the dropdown.
after you change the value, again bind the grid and check.
hope this may help you...and if not then provide more details

vb.net radio button list check if selected

I have the following:
<asp:RadioButtonList ID="rbIsRep" runat="server" RepeatDirection="horizontal" >
<asp:ListItem Value="1" >Yes</asp:ListItem>
<asp:ListItem Value="0" >No</asp:ListItem>
</asp:RadioButtonList>
how do I check if the radiobuttonlist has any selected items?
Use the SelectedIndex proprty to check if anything is selected and the SelectedItem property to get the selected item's text:
If rbIsRep.SelectedIndex > - 1 Then
Dim selectedItemsText = "You selected: " & rbIsRep.SelectedItem.Text
End If
You can change the selection programmatically for example with the SelectedValue property.
rbIsRep.SelectedValue = "0"
or declaratively from aspx
<asp:RadioButtonList ID="rbIsRep" runat="server" RepeatDirection="horizontal" >
<asp:ListItem Value="1" >Yes</asp:ListItem>
<asp:ListItem Selected="True" Value="0" >No</asp:ListItem>
</asp:RadioButtonList>
One way to do it:
var hasSelection = yourRadioButtonList.SelectedIndex != -1;
And for VB.NET:
Dim hasSelection = yourRadioButtonList.SelectedIndex <> -1

Resources