DropdownList with Multi select option? - c#

I have a DropDownList as following
<asp:DropDownList ID="ddlRoles" runat="server" AutoPostBack="True" Width="150px">
<asp:ListItem Value="9" Text=""></asp:ListItem>
<asp:ListItem Value="0">None</asp:ListItem>
<asp:ListItem Value="1">Ranu</asp:ListItem>
<asp:ListItem Value="2">Mohit</asp:ListItem>
<asp:ListItem Value="3">Kabeer</asp:ListItem>
<asp:ListItem Value="4">Jems</asp:ListItem>
<asp:ListItem Value="5">Jony</asp:ListItem>
<asp:ListItem Value="6">Vikky</asp:ListItem>
<asp:ListItem Value="7">Satish</asp:ListItem>
<asp:ListItem Value="8">Rony</asp:ListItem>
</asp:DropDownList>
I want to select Multiple name at once suppose I want to select Ranu Mohit or Ranu Kabeer Vikky, How its possible?

asp:DropDownList does not support multiple selects:
VerifyMultiSelect():
Always throws an HttpException exception because multiple selection is not supported for the DropDownList control.
You can use asp:ListBox with SelectionMode="Multiple" instead:
<asp:ListBox SelectionMode="Multiple" ID="lbRoles" runat="server" AutoPostBack="True" Width="150px">
<asp:ListItem Value="9" Text=""></asp:ListItem>
<asp:ListItem Value="0">None</asp:ListItem>
<asp:ListItem Value="1">Ranu</asp:ListItem>
<asp:ListItem Value="2">Mohit</asp:ListItem>
<asp:ListItem Value="3">Kabeer</asp:ListItem>
<asp:ListItem Value="4">Jems</asp:ListItem>
<asp:ListItem Value="5">Jony</asp:ListItem>
<asp:ListItem Value="6">Vikky</asp:ListItem>
<asp:ListItem Value="7">Satish</asp:ListItem>
<asp:ListItem Value="8">Rony</asp:ListItem>
</asp:ListBox>

Related

How to trigger DropDownList SelectedIndexChanged if I select the same option(the same index)?

As title, in C#, I was wondering that if I selected the same index in a dropdownlist, how could I trigger the function (OnSelectedIndexChanged)?
<asp:DropDownList ID="ddlColor" runat="server" Width="100%" AutoPostBack="True" OnSelectedIndexChanged="ddlColor_SelectedIndexChanged" >
<asp:ListItem Selected="True" Value="White"> White </asp:ListItem>
<asp:ListItem Value="Silver"> Silver </asp:ListItem>
<asp:ListItem Value="DarkGray"> Dark Gray </asp:ListItem>
<asp:ListItem Value="Khaki"> Khaki </asp:ListItem>
<asp:ListItem Value="DarkKhaki"> Dark Khaki </asp:ListItem>
</asp:DropDownList>
There is a scenario as below following:
I selected "White" at first, and it triggered ddlColor_SelectedIndexChanged successfully.
Then as need I selected "White" again, how could I trigger the function ddlColor_SelectedIndexChanged again?

Get and split multiple values from database field and view them as selected in a multiple select listbox

I want to get the values in a string from a database which i can do and my result is
string strValues = "'value1','value2','value3'";
i know we can split them using the split function. But the problem is how to mark them in the list box.
<asp:ListBox ID="lstValues" runat="server" SelectionMode="Multiple">
<asp:ListItem Value="-1">[``Select]</asp:ListItem>
<asp:ListItem Value="1">value 10</asp:ListItem>
<asp:ListItem Value="2">value 9</asp:ListItem>
<asp:ListItem Value="3">value 3</asp:ListItem>
<asp:ListItem Value="4">value 2</asp:ListItem>
<asp:ListItem Value="5">value 1</asp:ListItem>
</asp:ListBox>
<asp:ListBox ID="lstValues" runat="server" SelectionMode="Multiple">
<asp:ListItem Value="-1">[``Select]</asp:ListItem>
<asp:ListItem Value="1">value 10</asp:ListItem>
<asp:ListItem Value="2">value 9</asp:ListItem>
<asp:ListItem Value="3">value 3</asp:ListItem>
<asp:ListItem Value="4">value 2</asp:ListItem>
<asp:ListItem Value="5">value 1</asp:ListItem>
</asp:ListBox>
add Selected="true" on the Items you want to be checked!
Example: <asp:ListItem Value="5" selected="true">value 1</asp:ListItem>
you can also use Checked="true" for list boxes

unable to assign value in the dropdownlist in C#

im editing one gridview. while clicking edit im redirecting to one page and binding all the fields. but unble to bind month dropdown . i want to bind april month in dropdown but not binding
<asp:DropDownList ID="DDLMonth" runat="server">
<asp:ListItem>Month</asp:ListItem>
<asp:ListItem Text="January" Value="1"></asp:ListItem>
<asp:ListItem Text="February" Value="2"></asp:ListItem>
<asp:ListItem Text="March" Value="3"></asp:ListItem>
<asp:ListItem Text="April" Value="4"></asp:ListItem>
<asp:ListItem Text="May" Value="5"></asp:ListItem>
<asp:ListItem Text="June" Value="6"></asp:ListItem>
<asp:ListItem Text="July" Value="7"></asp:ListItem>
<asp:ListItem Text="August" Value="8"></asp:ListItem>
<asp:ListItem Text="September" Value="9"></asp:ListItem>
<asp:ListItem Text="October" Value="10"></asp:ListItem>
<asp:ListItem Text="November" Value="11"></asp:ListItem>
<asp:ListItem Text="December" Value="12"></asp:ListItem>
code behind
string DDLMonthstr = date.Month.ToString();//4
DDLMonth.SelectedIndex = Convert.ToInt32(DDLMonthstr);
Try
DDLMonth.Items.FindByValue(string val).Selected = true;
You probably have already selected item and need to clear selection using ListControl.ClearSelection before making new selection through code.
string DDLMonthstr = date.Month.ToString();//4
DDLMonth.ClearSelection();
DDLMonth.SelectedIndex = Convert.ToInt32(DDLMonthstr);
Also check if you are getting any exception by using try-catch block if you do not already have, this could help in fixing.

How to pass masterpage dropdownlist selected value to content page?

I have a dropdownlist control on masterpage in asp.net. as follows:
<asp:DropDownList ID="ddladults" runat="server" CssClass="input_bg">
<asp:ListItem Value="0">----Select----</asp:ListItem>
<asp:ListItem Value="1">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
<asp:ListItem Value="3">3</asp:ListItem>
<asp:ListItem Value="4">4</asp:ListItem>
<asp:ListItem Value="5">5</asp:ListItem>
<asp:ListItem Value="6">6</asp:ListItem>
<asp:ListItem Value="7">7</asp:ListItem>
<asp:ListItem Value="8">8</asp:ListItem>
<asp:ListItem Value="9">9</asp:ListItem>
<asp:ListItem Value="10">10</asp:ListItem>
</asp:DropDownList>
Here I want to pass the selected value to the content page named Checkavailablity.aspx.
Please give your ideas.
To get master page control values in content page, use Master.FindControl().
Add the following code in the Content Page.
Code
DropDownList masterDropDown = (DropDownList)Master.FindControl("ddladults");
String selectedValue=masterDropDown.SelectedItem.Text.ToString();
OR
DropDownList masterDropDown = (DropDownList)Master.FindControl("ddladults");
String selectedValue=masterDropDown.SelectedValue.ToString();
Then use that String selectedValue for whatever you want to do with.

insert dynamic data from datalist into database

I am using visual studio and c# to create a staff rota system where staff member's names from the "Staff" table are displayed in a datalist. As well as this, I have created 2 dropdown lists in asp, separate from the "Staff" table, although they appear in the item template tag to enable them to be dynamically created next to the staff names. The user can then select the staff's shift start and end times from the dropdowns.
I decided to use a datalist and dynamic dropdowns as the Staff table will frequently be updated as new staff may start or leave, therefore rota's in the future may contain different numbers of staff.
<table class="style57" width="100%">
<tr>
<td class="style70" width="100%">
<asp:DataList ID="DataList1" runat="server" DataSourceID="StaffHours"
ShowFooter="False" Width="668px" Height="16px" CellPadding="1" CellSpacing="1"">
<ItemTemplate>
<table width="100%">
<tr>
<td class="style76" width="0%">
<asp:Label ID="lblStaffID" runat="server" bgcolor="#CCCCCC" Visible=false Text='<%# Eval("StaffID") %>'/>
</td>
<td class="style73" width="20%">
<asp:Label ID="lblFirstName" runat="server" bgcolor="#CCCCCC" Text='<%# Eval("FirstName") %>'/>
<asp:Label ID="lblLastName" runat="server" bgcolor="#CCCCCC" Text='<%# Eval("LastName") %>' />
</td>
<td width="10%">
<asp:DropDownList ID="ddStartShift" runat="Server">
<asp:ListItem Value="Start Time" Selected="True"></asp:ListItem>
<asp:ListItem Value="06:00"></asp:ListItem>
<asp:ListItem Value="07:00"></asp:ListItem>
<asp:ListItem Value="08:00"></asp:ListItem>
<asp:ListItem Value="09:00"></asp:ListItem>
<asp:ListItem Value="10:00"></asp:ListItem>
<asp:ListItem Value="11:00"></asp:ListItem>
<asp:ListItem Value="12:00"></asp:ListItem>
<asp:ListItem Value="13:00"></asp:ListItem>
<asp:ListItem Value="14:00"></asp:ListItem>
<asp:ListItem Value="15:00"></asp:ListItem>
<asp:ListItem Value="16:00"></asp:ListItem>
<asp:ListItem Value="17:00"></asp:ListItem>
<asp:ListItem Value="18:00"></asp:ListItem>
<asp:ListItem Value="19:00"></asp:ListItem>
<asp:ListItem Value="20:00"></asp:ListItem>
<asp:ListItem Value="21:00"></asp:ListItem>
<asp:ListItem Value="22:00"></asp:ListItem>
<asp:ListItem Value="23:00"></asp:ListItem>
<asp:ListItem Value="00:00"></asp:ListItem>
<asp:ListItem Value="01:00"></asp:ListItem>
<asp:ListItem Value="02:00"></asp:ListItem>
<asp:ListItem Value="03:00"></asp:ListItem>
<asp:ListItem Value="04:00"></asp:ListItem>
<asp:ListItem Value="05:00"></asp:ListItem>
</asp:DropDownList>
</td>
<td width="10%">
<asp:DropDownList ID="ddEndShift" runat="Server">
<asp:ListItem Value="End Time" Selected="True"></asp:ListItem>
<asp:ListItem Value="06:00"></asp:ListItem>
<asp:ListItem Value="07:00"></asp:ListItem>
<asp:ListItem Value="08:00"></asp:ListItem>
<asp:ListItem Value="09:00"></asp:ListItem>
<asp:ListItem Value="10:00"></asp:ListItem>
<asp:ListItem Value="11:00"></asp:ListItem>
<asp:ListItem Value="12:00"></asp:ListItem>
<asp:ListItem Value="13:00"></asp:ListItem>
<asp:ListItem Value="14:00"></asp:ListItem>
<asp:ListItem Value="15:00"></asp:ListItem>
<asp:ListItem Value="16:00"></asp:ListItem>
<asp:ListItem Value="17:00"></asp:ListItem>
<asp:ListItem Value="18:00"></asp:ListItem>
<asp:ListItem Value="19:00"></asp:ListItem>
<asp:ListItem Value="20:00"></asp:ListItem>
<asp:ListItem Value="21:00"></asp:ListItem>
<asp:ListItem Value="22:00"></asp:ListItem>
<asp:ListItem Value="23:00"></asp:ListItem>
<asp:ListItem Value="00:00"></asp:ListItem>
<asp:ListItem Value="01:00"></asp:ListItem>
<asp:ListItem Value="02:00"></asp:ListItem>
<asp:ListItem Value="03:00"></asp:ListItem>
<asp:ListItem Value="04:00"></asp:ListItem>
<asp:ListItem Value="05:00"></asp:ListItem>
</asp:DropDownList>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
<asp:SqlDataSource ID="StaffHours" runat="server"
ConnectionString="<%$ ConnectionStrings:myConnectionString %>"
SelectCommand="SELECT [StaffID], [FirstName], [LastName] FROM [Staff]">
</asp:SqlDataSource>
</td>
</tr>
</table>
As these dropdowns are dynamically created in the datalist, I cannot figure out how to insert their values for each staff member into the "Rota" table as they are not identified therefore I cannot enter them into a parameter for a stored procedure.
Set the DataKeyField of the DataList to StaffID:
<asp:DataList ID="DataList1" runat="server" DataSourceID="StaffHours"
DataKeyField = "StaffID" ShowFooter="False" Width="668px"
Height="16px" CellPadding="1" CellSpacing="1"">
This will allow you to retrieve the StaffID when processing your data on postback:
for(int index = 0;index < DataList1.Items.Count;index++)
{
int staffID = (int) DataList1.DataKeys[index];
DropDownList ddStartShift = (DropDownList) DataList1.Items[index].FindControl("ddStartShift");
// etc.
}
For more on the DataKeys property: BaseDataList DataKeys Property

Categories

Resources