显示的网格视图数据与从下拉列表中选择的选择不对应 [英] The gridview data displayed is not corresponding to the selection chosen from the dropdownlist
问题描述
我是C#的新手。我正在尝试创建一个Web应用程序,允许用户根据他们从下拉列表中选择的主题查看类。我有下拉列表,显示下拉列表中的主题。我所拥有的代码没有向用户显示他们从下拉列表中选择的信息。它显示了相同的gridview数据,无论选择哪个选项。
我尝试过:
这是显示Web应用程序的代码。
I am very new to C#. I am trying to create a web application that will allow users to see classes based on a subject they choose from a drop down list. I have the drop down list displaying subjects from the drop down list. The code that I have isn't showing the user the information they chose from the drop down list. It shows the same gridview data not matter which option is chosen.
What I have tried:
This is the code that will show the web application.
<%@ 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:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:RegistrationConnectionString %>" SelectCommand="SELECT [Subjects], [Id] FROM [Subjects]">
<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" />
<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]">
<br>
</form>
</body>
</html>
这是应用程序背后的代码。
This is the code behind for the application.
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 = "Subjects";
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;
}
}
推荐答案
ConnectionStrings:RegistrationConnectionString %> SelectCommand = SELECT [Subjects],[Id] FROM [Subjects] >
< asp:GridView ID = GridView1 runat = 服务器 AutoGenerateColumns = False DataKeyNames = Id DataSourceID = SqlDataSource2 >
< 列 >
< asp:BoundField DataField = Id < span class =code-attribute> HeaderText = Id ReadOnly = 真 SortExpression = Id / >
< asp:BoundField DataField = Instrutor HeaderText = Instrutor < span class =code-attribute> SortExpression = Instrutor / >
< < span class =code-leadattribute> asp:BoundField DataField = CRN HeaderText = CRN SortExpression = CRN < span class =code-attribute> / >
< asp:BoundField DataField =Credits HeaderText = 积分 SortExpression = Credits / >
< asp:BoundField DataField = 日 HeaderText = 日 SortExpression = 日 / >
< asp:BoundField DataField = 时间 HeaderText = 时间 SortExpression = 时间 / >
< asp:BoundField DataField = 标题 HeaderText = 标题 SortExpression = 标题 / >
< asp:BoundField DataField = HeaderText = 秒 SortExpression = 部分 / >
< asp:BoundField < span class =code-attribute> DataField = Location HeaderText = 位置 SortExpression = 位置 / >
< asp:BoundField DataField = BeginEnd HeaderText = BeginEnd SortExpression = BeginEnd / < span class =code-keyword>>
< asp:BoundField DataField = 数字 HeaderText = 编号 SortExpression = 数字 / >
<asp:BoundField< span class=\"code-attribute\"> DataField=\"SubjectId_Fk\" HeaderText= \"SubjectId_Fk\" SortExpression =\"SubjectId_Fk\" />
<asp:SqlDataSource ID=\"SqlDataSource2\" runat=\"server\" ConnectionString=\"<%
ConnectionStrings:RegistrationConnectionString %>" SelectCommand="SELECT [Subjects], [Id] FROM [Subjects]"> <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" /> <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%
ConnectionStrings:RegistrationConnectionString %>\" Select Command=\"SELECT [Id], [Instrutor], [CRN], [Credits], [Day], [Time], [Title], [Section], [Location], [BeginEnd], [Number], [SubjectId_Fk] FROM [Classes]\">
<br>
</form>
</body>
</html>
ConnectionStrings:RegistrationConnectionString %>" SelectCommand="SELECT [Id], [Instrutor], [CRN], [Credits], [Day], [Time], [Title], [Section], [Location], [BeginEnd], [Number], [SubjectId_Fk] FROM [Classes]"> <br> </form> </body> </html>
This is the code behind for the application.
This is the code behind for the application.
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 = "Subjects";
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;
}
}
Don’t think you need the Cn.Open() in the GetData function. This works for me:
Don't think you need the Cn.Open() in the GetData function. This works for me:
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;
}
这篇关于显示的网格视图数据与从下拉列表中选择的选择不对应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!