从生成的 ListItem 中获取 DropdownList 的文本
Get The Text of a DropdownList from the generated ListItem
这是我的登录页面,假设密码和用户名正确并使用会话获取特定数据:
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 MSSQLConnector;
using System.Data;
namespace SoftwareAnalysisAndDesign.SAD
{
public partial class OnlineAppSyss : System.Web.UI.Page
{
private MSConnector connector = new MSConnector();
string teachersubjectlistquery = null;
private DataSet teachersubjectlistData;
private DataTable teacheraccountdetailsTable;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sendet, EventArgs e)
{
Teacher();
}
public void Teacher()
{
//Connection String
connector.ConnectionString = "Data Source=keith;Initial Catalog=SAD;Integrated Security=True";
//Subject List
teachersubjectlistquery = "select distinct CourseNo from AssessmentForm where TeacherID =" + username + "";
teachersubjectlistData = connector.ExecuteQuery(teachersubjectlistquery);
teachersubjectlistTable = teachersubjectlistData.Tables[0];
Session["TeacherSubjectList"] = teachersubjectlistTable;
}
}
我使用 Session 在另一个页面中传递数据表 "teachersubjectlistTable"。
在我的教师页面中,我声明下拉列表以使用会话["TeacherSubjectList"]生成项目。
<asp:DropDownList runat="server" ID="SubjectList"></asp:DropDownList>
它的锚标记继续到 ValidateSubject 页面:
<a class="btn btn-block" style="margin-top:-37px;margin-left:65px;" runat="server" onserverclick="TeacherSubjects_Click">Proceed</a>
及其背后的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using MSSQLConnector;
namespace SoftwareAnalysisAndDesign.SAD
{
public partial class TeacherPage : System.Web.UI.Page
{
private MSConnector connector = new MSConnector();
private DataSet SubjectlistData;
private DataTable SubjectlistTable;
string query = null;
protected void Page_Load(object sender, EventArgs e)
{
//Populate The Select Tag with Subjects
SubjectList.DataSource = Session["TeacherSubjectList"];
SubjectList.DataTextField = "CourseNo";
SubjectList.DataValueField = "CourseNo";
SubjectList.DataBind();
}
protected void TeacherSubjects_Click(object sender, EventArgs e)
{
string getText = SubjectList.SelectedItem.Text;
//Connection String
connector.ConnectionString = "Data Source=keith;Initial Catalog=SAD;Integrated Security=True";
query = "select StudentID,CourseNo,CourseDescription,Units,Day,StartTime,EndTime,Room,Instructor,Amount,Status from assessmentform where CourseNo = '" + getText + "'";
SubjectlistData = connector.ExecuteQuery(query);
SubjectlistTable = SubjectlistData.Tables[0];
//Add a colum check row
SubjectlistTable.Columns.Add("Check", Type.GetType("System.Boolean"));
Session["ValidateSubject"] = SubjectlistTable;
Response.Redirect("ValidateSubjectTeacher.aspx");
}
}
}
在我的另一个 aspx 页面 (ValidateSubject) 中,我将 Gridview 声明为会话的持有者:
<asp:GridView runat="server" CssClass="table table-hover table-bordered" ID="ValidateSubject" Style="text-align: center"></asp:GridView>
及其后面的aspx代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace SoftwareAnalysisAndDesign.SAD
{
public partial class ValidateSubjectTeacher : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ValidateSubject.DataSource = Session["ValidateSubject"];
ValidateSubject.DataBind();
}
}
}
我无法在我的下拉列表(TeacherPage)中获取 selected 项目,它只会获取第一个项目的数据 selected。
在我的下拉列表(TeacherPage)中,使用查询生成了两个项目:
CmpE 515
CmpE 516
但是当我select "CmpE 516" 项并单击继续时,显示的数据只是"CmpE 515" 项。如果用户单击某个项目,如何让我的下拉列表中的其他 items/values 显示某些数据?
这里的代码:
string getText = SubjectList.SelectedItem.Text;
它只会获取文本 'CmpE 515' 数据,即使我 select 'CmpE 516' 并单击继续。我如何获得下拉列表中的其他项目?
您不应该在每次页面加载时都绑定下拉菜单,请使用 IsPostBack
属性 检查页面是否第一次加载,并且只有在这种情况下才像这样绑定您的下拉菜单:-
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
//Populate The Select Tag with Subjects
SubjectList.DataSource = Session["TeacherSubjectList"];
SubjectList.DataTextField = "CourseNo";
SubjectList.DataValueField = "CourseNo";
SubjectList.DataBind();
}
}
同样适用于您的 ValidateSubjectTeacher
页面。
这是我的登录页面,假设密码和用户名正确并使用会话获取特定数据:
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 MSSQLConnector;
using System.Data;
namespace SoftwareAnalysisAndDesign.SAD
{
public partial class OnlineAppSyss : System.Web.UI.Page
{
private MSConnector connector = new MSConnector();
string teachersubjectlistquery = null;
private DataSet teachersubjectlistData;
private DataTable teacheraccountdetailsTable;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sendet, EventArgs e)
{
Teacher();
}
public void Teacher()
{
//Connection String
connector.ConnectionString = "Data Source=keith;Initial Catalog=SAD;Integrated Security=True";
//Subject List
teachersubjectlistquery = "select distinct CourseNo from AssessmentForm where TeacherID =" + username + "";
teachersubjectlistData = connector.ExecuteQuery(teachersubjectlistquery);
teachersubjectlistTable = teachersubjectlistData.Tables[0];
Session["TeacherSubjectList"] = teachersubjectlistTable;
}
}
我使用 Session 在另一个页面中传递数据表 "teachersubjectlistTable"。
在我的教师页面中,我声明下拉列表以使用会话["TeacherSubjectList"]生成项目。
<asp:DropDownList runat="server" ID="SubjectList"></asp:DropDownList>
它的锚标记继续到 ValidateSubject 页面:
<a class="btn btn-block" style="margin-top:-37px;margin-left:65px;" runat="server" onserverclick="TeacherSubjects_Click">Proceed</a>
及其背后的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using MSSQLConnector;
namespace SoftwareAnalysisAndDesign.SAD
{
public partial class TeacherPage : System.Web.UI.Page
{
private MSConnector connector = new MSConnector();
private DataSet SubjectlistData;
private DataTable SubjectlistTable;
string query = null;
protected void Page_Load(object sender, EventArgs e)
{
//Populate The Select Tag with Subjects
SubjectList.DataSource = Session["TeacherSubjectList"];
SubjectList.DataTextField = "CourseNo";
SubjectList.DataValueField = "CourseNo";
SubjectList.DataBind();
}
protected void TeacherSubjects_Click(object sender, EventArgs e)
{
string getText = SubjectList.SelectedItem.Text;
//Connection String
connector.ConnectionString = "Data Source=keith;Initial Catalog=SAD;Integrated Security=True";
query = "select StudentID,CourseNo,CourseDescription,Units,Day,StartTime,EndTime,Room,Instructor,Amount,Status from assessmentform where CourseNo = '" + getText + "'";
SubjectlistData = connector.ExecuteQuery(query);
SubjectlistTable = SubjectlistData.Tables[0];
//Add a colum check row
SubjectlistTable.Columns.Add("Check", Type.GetType("System.Boolean"));
Session["ValidateSubject"] = SubjectlistTable;
Response.Redirect("ValidateSubjectTeacher.aspx");
}
}
}
在我的另一个 aspx 页面 (ValidateSubject) 中,我将 Gridview 声明为会话的持有者:
<asp:GridView runat="server" CssClass="table table-hover table-bordered" ID="ValidateSubject" Style="text-align: center"></asp:GridView>
及其后面的aspx代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace SoftwareAnalysisAndDesign.SAD
{
public partial class ValidateSubjectTeacher : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ValidateSubject.DataSource = Session["ValidateSubject"];
ValidateSubject.DataBind();
}
}
}
我无法在我的下拉列表(TeacherPage)中获取 selected 项目,它只会获取第一个项目的数据 selected。
在我的下拉列表(TeacherPage)中,使用查询生成了两个项目:
CmpE 515
CmpE 516
但是当我select "CmpE 516" 项并单击继续时,显示的数据只是"CmpE 515" 项。如果用户单击某个项目,如何让我的下拉列表中的其他 items/values 显示某些数据?
这里的代码:
string getText = SubjectList.SelectedItem.Text;
它只会获取文本 'CmpE 515' 数据,即使我 select 'CmpE 516' 并单击继续。我如何获得下拉列表中的其他项目?
您不应该在每次页面加载时都绑定下拉菜单,请使用 IsPostBack
属性 检查页面是否第一次加载,并且只有在这种情况下才像这样绑定您的下拉菜单:-
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
//Populate The Select Tag with Subjects
SubjectList.DataSource = Session["TeacherSubjectList"];
SubjectList.DataTextField = "CourseNo";
SubjectList.DataValueField = "CourseNo";
SubjectList.DataBind();
}
}
同样适用于您的 ValidateSubjectTeacher
页面。