Answered by:
Appending Items to a drop down list

Question
-
Hi, I'm a newbie and I'm trying to create a web application. In this application (using visual studio 2013 web, express) I am recording where students are dropping subjects for their second year course. I have a gridview with a drop down which shows all their subjects, but the table I have created doesn't handle nulls, so I added the following:
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" DataSourceID="sdsE" DataTextField="CodeName" DataValueField="EnrolmentID" AppendDataBoundItems="True"> <asp:ListItem Selected="True" Value="0">-- pick one --</asp:ListItem> </asp:DropDownList>
This only shows the one item for the student
-- pick one --
...but it should be showing all their enrolments and not just the one appended item as above. What do I have to do to get the items to appear from the drop down list (CodeName), which would be a list of their current subjects?
Here is the source for my main page:
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="SOI._Default" maintainScrollPositionOnPostback="true"%> <asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server"> <div class="jumbotron"> <h1>What can I do here?</h1> <p class="lead">This web application allows you to sumbit students' choices for their statement of intentions (SOI's). </p> <p class="lead">Advice & Recruitment will then ensure students are enrolled to their respective second year courses in ProSolution.</p> <p><a href="http://www.asp.net" class="btn btn-primary btn-lg">Learn more »</a></p> </div> <div> <asp:Label ID="Label1" runat="server" Text="Enter the parameters below to view your current/continuing students:" Font-Bold="True"></asp:Label> <br /> <br /> Academic Year: <asp:TextBox ID="academicYearTextBox" runat="server" ReadOnly="True" Width="47px">15/16</asp:TextBox> Tutor Group Code: <asp:DropDownList ID="TGDropDownList" runat="server" DataSourceID="sdsTGList" DataTextField="TutorAndCode" DataValueField="TutorGroupCode"> </asp:DropDownList> <asp:SqlDataSource ID="sdsTGList" runat="server" ConnectionString="<%$ ConnectionStrings:TrackerConnectionString %>" SelectCommand="SELECT DISTINCT [TutorGroupCode], [Tutor Names]+' - '+[TutorGroupCode] AS [TutorAndCode] FROM [Tracker].[dbo].[vSTTutorByStudentIDUniqueStudentsAnyYear] AS TG WHERE TG.[Year] = '15/16' AND TG.[CompletionStatusID] IN (1,2) AND TG.[AosCode] NOT IN ('A2TG','FLE2TG','FLE3TG','FLE1.1TG','FLESOLTG') AND TG.[AosCode] NOT LIKE 'EC2%' AND TG.[AosCode] NOT LIKE 'G2%' AND TG.[AosCode] NOT LIKE 'PTOA%' AND TG.[AosCode] NOT LIKE 'SU%' AND TG.[AosCode] NOT LIKE 'FL1%' AND TG.[AosCode] NOT LIKE 'ED%[2]%' ORDER BY [Tutor Names]+' - '+[TutorGroupCode] "></asp:SqlDataSource> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Search" /> <asp:Label ID="selectedTGLabel" runat="server" style="font-weight: 700"></asp:Label> <br /> <hr /> <br /> <asp:GridView ID="RecordsGridView" runat="server" DataSourceID="sdsRecords" AutoGenerateColumns="False" AutoGenerateEditButton="True" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Vertical" DataKeyNames="ID" OnRowDataBound="RecordsGridView_RowDataBound"> <AlternatingRowStyle BackColor="White" /> <Columns> <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" /> <asp:BoundField DataField="StudentDetailID" HeaderText="Detail" SortExpression="StudentDetailID" ReadOnly="True" /> <asp:BoundField DataField="RefNo" HeaderText="RefNo" SortExpression="RefNo" ReadOnly="True" /> <asp:BoundField DataField="Forename" HeaderText="Forename" SortExpression="Forename" ReadOnly="True" /> <asp:BoundField DataField="Surname" HeaderText="Surname" SortExpression="Surname" ReadOnly="True" /> <asp:CheckBoxField DataField="Status" HeaderText="Continuing?" SortExpression="Status" > <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" /> </asp:CheckBoxField> <asp:TemplateField HeaderText="Enrolments" SortExpression="EnrolmentID"> <EditItemTemplate> <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" DataSourceID="sdsE" DataTextField="CodeName" DataValueField="EnrolmentID" AppendDataBoundItems="True"> <asp:ListItem Selected="True" Value="0">-- pick one --</asp:ListItem> </asp:DropDownList> <asp:SqlDataSource ID="sdsE" runat="server" ConnectionString="<%$ ConnectionStrings:TrackerConnectionString %>" SelectCommand="SELECT [ID], [EnrolmentID], [StudentDetailID], [Student Ref No] AS Student_Ref_No, [CodeName] FROM [vSOI Enrolments] WHERE ([ID]= @intID) ORDER BY [CodeName]"> <SelectParameters> <asp:ControlParameter ControlID="RecordsGridView" Name="intID" PropertyName="SelectedValue" /> </SelectParameters> </asp:SqlDataSource> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label2" runat="server" Text='<%# Bind("EnrolmentID") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="CreatedDate" SortExpression="CreatedDate" Visible="False"> <EditItemTemplate> <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("CreatedDate") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("CreatedDate", "{0:d}") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="Notes" HeaderText="Notes" SortExpression="Notes" /> </Columns> <FooterStyle BackColor="#CCCC99" /> <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" /> <RowStyle BackColor="#F7F7DE" /> <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" /> <SortedAscendingCellStyle BackColor="#FBFBF2" /> <SortedAscendingHeaderStyle BackColor="#848384" /> <SortedDescendingCellStyle BackColor="#EAEAD3" /> <SortedDescendingHeaderStyle BackColor="#575357" /> </asp:GridView> <asp:SqlDataSource ID="sdsRecords" runat="server" ConflictDetection="CompareAllValues" ConnectionString="<%$ ConnectionStrings:TrackerConnectionString %>" OldValuesParameterFormatString="original_{0}" SelectCommand="SELECT [ID], [StudentDetailID], [RefNo], [Forename], [Surname], [Status], [EnrolmentID], [CreatedDate], [Notes], [TutorGroupCode] FROM [SOI] WHERE ([TutorGroupCode] = @TutorGroupCode)" UpdateCommand="UPDATE [SOI] SET [StudentDetailID] = @StudentDetailID, [RefNo] = @RefNo, [Forename] = @Forename, [Surname] = @Surname, [Status] = @Status, [EnrolmentID] = @EnrolmentID, [CreatedDate] = @CreatedDate, [Notes] = @Notes, [TutorGroupCode] = @TutorGroupCode WHERE [ID] = @original_ID AND [StudentDetailID] = @original_StudentDetailID AND (([RefNo] = @original_RefNo) OR ([RefNo] IS NULL AND @original_RefNo IS NULL)) AND (([Forename] = @original_Forename) OR ([Forename] IS NULL AND @original_Forename IS NULL)) AND (([Surname] = @original_Surname) OR ([Surname] IS NULL AND @original_Surname IS NULL)) AND (([Status] = @original_Status) OR ([Status] IS NULL AND @original_Status IS NULL)) AND (([EnrolmentID] = @original_EnrolmentID) OR ([EnrolmentID] IS NULL AND @original_EnrolmentID IS NULL)) AND (([CreatedDate] = @original_CreatedDate) OR ([CreatedDate] IS NULL AND @original_CreatedDate IS NULL)) AND (([Notes] = @original_Notes) OR ([Notes] IS NULL AND @original_Notes IS NULL)) AND (([TutorGroupCode] = @original_TutorGroupCode) OR ([TutorGroupCode] IS NULL AND @original_TutorGroupCode IS NULL))" DeleteCommand="DELETE FROM [SOI] WHERE [ID] = @original_ID AND [StudentDetailID] = @original_StudentDetailID AND (([RefNo] = @original_RefNo) OR ([RefNo] IS NULL AND @original_RefNo IS NULL)) AND (([Forename] = @original_Forename) OR ([Forename] IS NULL AND @original_Forename IS NULL)) AND (([Surname] = @original_Surname) OR ([Surname] IS NULL AND @original_Surname IS NULL)) AND (([Status] = @original_Status) OR ([Status] IS NULL AND @original_Status IS NULL)) AND (([EnrolmentID] = @original_EnrolmentID) OR ([EnrolmentID] IS NULL AND @original_EnrolmentID IS NULL)) AND (([CreatedDate] = @original_CreatedDate) OR ([CreatedDate] IS NULL AND @original_CreatedDate IS NULL)) AND (([Notes] = @original_Notes) OR ([Notes] IS NULL AND @original_Notes IS NULL)) AND (([TutorGroupCode] = @original_TutorGroupCode) OR ([TutorGroupCode] IS NULL AND @original_TutorGroupCode IS NULL))" InsertCommand="INSERT INTO [SOI] ([StudentDetailID], [RefNo], [Forename], [Surname], [Status], [EnrolmentID], [CreatedDate], [Notes], [TutorGroupCode]) VALUES (@StudentDetailID, @RefNo, @Forename, @Surname, @Status, @EnrolmentID, @CreatedDate, @Notes, @TutorGroupCode)"> <DeleteParameters> <asp:Parameter Name="original_ID" Type="Int32" /> <asp:Parameter Name="original_StudentDetailID" Type="Int32" /> <asp:Parameter Name="original_RefNo" Type="String" /> <asp:Parameter Name="original_Forename" Type="String" /> <asp:Parameter Name="original_Surname" Type="String" /> <asp:Parameter Name="original_Status" Type="Boolean" /> <asp:Parameter Name="original_EnrolmentID" Type="Int32" /> <asp:Parameter Name="original_CreatedDate" Type="DateTime" /> <asp:Parameter Name="original_Notes" Type="String" /> <asp:Parameter Name="original_TutorGroupCode" Type="String" /> </DeleteParameters> <InsertParameters> <asp:Parameter Name="StudentDetailID" Type="Int32" /> <asp:Parameter Name="RefNo" Type="String" /> <asp:Parameter Name="Forename" Type="String" /> <asp:Parameter Name="Surname" Type="String" /> <asp:Parameter Name="Status" Type="Boolean" /> <asp:Parameter Name="EnrolmentID" Type="Int32" /> <asp:Parameter Name="CreatedDate" Type="DateTime" /> <asp:Parameter Name="Notes" Type="String" /> <asp:Parameter Name="TutorGroupCode" Type="String" /> </InsertParameters> <SelectParameters> <asp:ControlParameter ControlID="TGDropDownList" Name="TutorGroupCode" PropertyName="SelectedValue" Type="String" /> </SelectParameters> <UpdateParameters> <asp:Parameter Name="StudentDetailID" Type="Int32" /> <asp:Parameter Name="RefNo" Type="String" /> <asp:Parameter Name="Forename" Type="String" /> <asp:Parameter Name="Surname" Type="String" /> <asp:Parameter Name="Status" Type="Boolean" /> <asp:Parameter Name="EnrolmentID" Type="Int32" /> <asp:Parameter Name="CreatedDate" Type="DateTime" /> <asp:Parameter Name="Notes" Type="String" /> <asp:Parameter Name="TutorGroupCode" Type="String" /> <asp:Parameter Name="original_ID" Type="Int32" /> <asp:Parameter Name="original_StudentDetailID" Type="Int32" /> <asp:Parameter Name="original_RefNo" Type="String" /> <asp:Parameter Name="original_Forename" Type="String" /> <asp:Parameter Name="original_Surname" Type="String" /> <asp:Parameter Name="original_Status" Type="Boolean" /> <asp:Parameter Name="original_EnrolmentID" Type="Int32" /> <asp:Parameter Name="original_CreatedDate" Type="DateTime" /> <asp:Parameter Name="original_Notes" Type="String" /> <asp:Parameter Name="original_TutorGroupCode" Type="String" /> </UpdateParameters> </asp:SqlDataSource> <br /> <br /> </div> </asp:Content>
..and the code behind:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace SOI { public partial class _Default : Page { protected void Page_Load(object sender, EventArgs e) { RecordsGridView.DataSource = null; RecordsGridView.DataBind(); } protected void Button1_Click(object sender, EventArgs e) { RecordsGridView.DataBind(); selectedTGLabel.Text = TGDropDownList.SelectedItem.Text; } } } }
- Moved by CoolDadTx Tuesday, December 1, 2015 2:37 PM Wrong forums
Monday, November 30, 2015 4:49 PM