我是C#的新手。我正在尝试创建一个Web应用程序,允许用户根据他们从下拉列表中选择的主题查看类。我所拥有的代码并未向用户显示他们从下拉列表中选择的信息。无论选择哪个选项,它都会显示相同的gridview数据。
这是显示Web应用程序的代码。
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Summer.aspx.cs"Inherits="Summer" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Summer 2016</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Summer 2016</h1>
<h2>Classes offered</h2>
<h3>Please Choose a Subject</h3>
</div>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="Subjects" DataValueField="Subjects">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:RegistrationConnectionString %>" SelectCommand="SELECT [Subject], [Id] FROM [Classes]"></asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Id" DataSourceID="SqlDataSource2">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="True" SortExpression="Id" />
<asp:BoundField DataField="Instrutor" HeaderText="Instrutor" SortExpression="Instrutor" />
<asp:BoundField DataField="CRN" HeaderText="CRN" SortExpression="CRN" />
<asp:BoundField DataField="Credits" HeaderText="Credits" SortExpression="Credits" />
<asp:BoundField DataField="Day" HeaderText="Day" SortExpression="Day" />
<asp:BoundField DataField="Time" HeaderText="Time" SortExpression="Time" />
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="Section" HeaderText="Section" SortExpression="Section" />
<asp:BoundField DataField="Location" HeaderText="Location" SortExpression="Location" />
<asp:BoundField DataField="BeginEnd" HeaderText="BeginEnd" SortExpression="BeginEnd" />
<asp:BoundField DataField="Number" HeaderText="Number" SortExpression="Number" />
<asp:BoundField DataField="SubjectId_Fk" HeaderText="SubjectId_Fk" SortExpression="SubjectId_Fk" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:RegistrationConnectionString %>" SelectCommand="SELECT [Id], [Instrutor], [CRN], [Credits], [Day], [Time], [Title], [Section], [Location], [BeginEnd], [Number], [SubjectId_Fk] FROM [Classes]"></asp:SqlDataSource>
<br />
</form>
</body>
</html>
这是Web应用程序背后的代码。
enter code here
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
public partial class Summer : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// DropDownList1.DataSource = GetDataTable();
DropDownList1.DataValueField = "Id";
DropDownList1.DataTextField = "Subject";
DropDownList1.DataBind();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
using (SqlConnection Cn = new SqlConnection(ConfigurationManager.ConnectionStrings[@"C:\Users\Keith\Documents\Registration.mdf"].ConnectionString))
{
using (SqlCommand Cmd = new SqlCommand("select * from Classes where Id=" + DropDownList1.SelectedValue.ToString(), Cn))
{
Cn.Open();
Cmd.Parameters.AddWithValue("@Id", int.Parse(DropDownList1.SelectedValue));
SqlDataReader Dr = Cmd.ExecuteReader();
if (Dr.HasRows)
{
GridView1.DataSource = Dr;
GridView1.DataBind();
}
Dr.Close();
Cn.Close();
}
}
}
public static DataTable GetDataTable(string sqlCommand)
{
DataTable table = new DataTable();
try
{
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[@"C:\Users\Keith\Documents\Registration.mdf"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand(sqlCommand, myConnection))
{
using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
{
adapter.Fill(table);
}
}
}
}
catch (Exception ex)
{
table = null;
throw ex;
}
return table;
}
}
答案 0 :(得分:0)
更新Dropdown元素,如下所示,
<asp:DropDownList ID="DropDownList1"
runat="server" AutoPostBack="True"
DataSourceID="SqlDataSource1" DataTextField="Subjects"
DataValueField="Subjects"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>