从生成的ListItem中获取DropdownList的文本

时间:2015-09-28 07:10:21

标签: c# asp.net sql-server session code-behind

这是我的登录页面,假设密码和用户名正确并使用会话获取特定数据:

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在另一个页面中传递了DataTable“teachersubjectlistTable”。

在我的教师页面中,我声明了使用Session [“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声明为Session的持有者:

<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)中获取所选项目,它只会获取所选第一项的数据。

在我的下拉列表(TeacherPage)中,使用查询生成了两个项目:

CmpE 515
CmpE 516

但是当我选择“CmpE 516”项并单击“继续”时,显示的数据仅为“CmpE 515”项目。如果用户点击某个项目,如何在我的下拉列表中获取其他项目/值以显示某些数据?

这段代码:

string getText = SubjectList.SelectedItem.Text;

即使我选择'CmpE 516'并单击继续,它也只会获得文本'CmpE 515'数据。如何获取下拉列表中的其他项目?

1 个答案:

答案 0 :(得分:1)

每次加载页面时都不应该绑定下拉列表,使用var emailArray = [ /test@email.com/i, /test2@email.com/i ] db.collection.find({ '$or': [ { 'email': { '$in': emailArray } }, { 'name': 'testUser' } ] }) 属性检查页面是否第一次加载,并且仅在这种情况下绑定下拉列表: -

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(); } } 页面。