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
Related
I have a following code in aspx page
<asp:DropDownList ID="txtSupplierCountry" class="form-control" Width="230px" runat="server">
<asp:ListItem Text="Select gender" Value="0"></asp:ListItem>
<asp:ListItem Text="male" Value="0"></asp:ListItem>
<asp:ListItem Text="female" Value="1"></asp:ListItem>
</asp:DropDownList>
I have a value in database as male/female. I need to set this text using C# code. Basically I am trying to set selected option based on the text from database. I am not getting an idea. I have tried following code but it doesn't work.
String t = "male" // coming from db
txtSupplierCountry.Items.FindByText(t).Selected = true;
when you are saving data Male and Female then why did you keep dropdown values in number like 1,2 etc, change code like this.
<asp:DropDownList ID="txtSupplierCountry" class="form-control" Width="230px" runat="server">
<asp:ListItem Text="Select Country" Value="0"></asp:ListItem>
<asp:ListItem Text="male" Value="male"></asp:ListItem>
<asp:ListItem Text="female" Value="female"></asp:ListItem>
then
txtSupplierCountry.SelectedValue="male";
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
To be brief, I need the Textbox with ID="textComments" to only appear when the dropdown option of 'Other' is selected from the dropdown, and then disappear if another selection is made. I could do this with JS but it needs to be in C#.
<asp:DropDownList runat="server" ID="dropEnquiryType" CssClass="dropdownRequestList">
<asp:ListItem Value="Customer Services" Text="Customer Services"></asp:ListItem>
<asp:ListItem Value="Website" Text="Website"></asp:ListItem>
<asp:ListItem Value="Contract" Text="Contract Hire"></asp:ListItem>
<asp:ListItem Value="Other" Text="Other"></asp:ListItem>
</asp:DropDownList>
<asp:label runat="server" ID="lblComments" AssociatedControlID="textComments" CssClass="textLabel">Comments:</asp:label>
<asp:TextBox runat="server" MaxLength="200" TextMode="MultiLine" Columns="40" Rows="4" ID="textComments" Wrap="true" CssClass="textComments"></asp:TextBox>
And help would be greatly appreciated.
Make the DropDownList first AutoPostBack=true
handle it's SelectedIndexChanged-event
Switch visibility of both controls there
aspx:
<asp:DropDownList AutoPostBack="true" OnSelectedIndexChanged="dropEnquiryType_Changed" runat="server" ID="dropEnquiryType" CssClass="dropdownRequestList">
<asp:ListItem Value="Customer Services" Text="Customer Services"></asp:ListItem>
<asp:ListItem Value="Website" Text="Website"></asp:ListItem>
<asp:ListItem Value="Contract" Text="Contract Hire"></asp:ListItem>
<asp:ListItem Value="Other" Text="Other"></asp:ListItem>
</asp:DropDownList>
codebehind:
protected void dropEnquiryType_Changed(Object sender, EventArgs e)
{
lblComments.Visible = dropEnquiryType.SelectedValue == "Other";
textComments.Visible = lblComments.Visible;
}
If you absolutely have to do this within C#, then a simple check in PreRender should be sufficient:
textComments.Visible = (dropEnquiryType.SelectedValue == "Other");
This will also require you to set AutoPostback on dropEnquiryType, though, which uses ... JavaScript!
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>
This is what I tried...
ddlClientsInSearchResult.DataSource = dtClients;
ddlClientsInSearchResult.DataValueField = dtClients.Columns[0].ToString();
ddlClientsInSearchResult.DataTextField = dtClients.Columns[1].ToString();
ddlClientsInSearchResult.DataBind();
ddlClientsInSearchResult.Items.Insert(0, "Select One");
so that the first item in the list is "Select One", but this doesn't seem to be working for me. This is in the code behind an .aspx page, so I was thinking maybe there was a way I could do it over there...but my internet searching has not turned up a solution.
<asp:DropDownList ID="ddlClients" runat="server" Font-Size="8pt" Font-Names="Tahoma"
Width="200px" AutoPostBack="true" OnSelectedIndexChanged="ddlClients_SelectedIndexChanged">
</asp:DropDownList>
That's the ASP.net code for the DropDownBow that I'm trying to default to "Select One".
Thanks for the help!
Remove this ddlClientsInSearchResult.Items.Insert(0, "Select One"); from code behind and do like...
<asp:DropDownList ID="ddlClients" runat="server" Font-Size="8pt" Font-Names="Tahoma"
Width="200px" AutoPostBack="true" OnSelectedIndexChanged="ddlClients_SelectedIndexChanged"
AppendDataBoundItems="true">
<asp:ListItem Text="Select one" Value="0"></asp:ListItem>
</asp:DropDownList>
Just add this to your markup:
<asp:DropDown>
<asp:ListItem Text="Select One" Value="" Selected="true">Select One</asp:ListItem>
</asp:DropDown/>
the code that worked for me is as below
ddlMonth.SelectedIndex = Convert.ToDateTime(row["cstDOB"].ToString()).Month;
ddlYear.SelectedItem.Text = Convert.ToDateTime(row["cstDOB"].ToString()).Year.ToString();
ddlState.SelectedItem.Text = row["cstState"].ToString();
Add "Select One" as an item declaratively and Use the AppendDataBoundItems="true"
<asp:DropDownList ID="ddlClients" runat="server" AppendDataBoundItems="true" AutoPostBack="true" >
<asp:ListItem Value="0">Select One</asp:ListItem>
</asp:DropDownList>