获取DropDownList的文本从生成的列表项 [英] Get The Text of a DropdownList from the generated ListItem

查看:94
本文介绍了获取DropDownList的文本从生成的列表项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的登录页面,假设用户名和密码是否正确,使用会话得到一定的数据:

This is my login page, assume that password and username is correct and get a certain data using session:

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;
         }
    }

我使用会话传递数据表teachersubjectlistTable的另一页。

I pass the DataTable "teachersubjectlistTable" in another page using Session.

在我的老师页,我宣布DropDownList的生成使用Session [TeacherSubjectList]。项目

In my Teacher Page, I declare dropdownlist to generate Items using the Session["TeacherSubjectList"].

<asp:DropDownList runat="server" ID="SubjectList"></asp:DropDownList>

其锚标记进入到ValidateSubject页:

Its anchor tag to proceed to ValidateSubject Page:

<a class="btn btn-block" style="margin-top:-37px;margin-left:65px;" runat="server" onserverclick="TeacherSubjects_Click">Proceed</a>

和其背后的code:

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的声明作为本届会议的持有人:

in my another aspx page (ValidateSubject), I declare Gridview as holder of the Session:

<asp:GridView runat="server" CssClass="table table-hover table-bordered" ID="ValidateSubject" Style="text-align: center"></asp:GridView>

和其背后的aspx code:

and its aspx code behind:

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

我无法选择的项目在我的DropDownList(TeacherPage)它只会选择的第一个项目的数据。

I can't get the selected Item in my dropdownlist(TeacherPage) it will only get the data of the first item selected.

在我的DropDownList的我(TeacherPage)两个项目已通过使用查询生成的:

In my my dropdownlist(TeacherPage) two Items have been generated by the use of queries:

CmpE 515
CmpE 516

但是,当我选择CMPE 516项目,点击进入,显示的数据仅是CMPE 515项目。如何获得其他项目/值我DropDownList中显示某些数据,如果用户点击某一个项目?

But when I select the "CmpE 516" Item and click proceed, the data displayed is only the "CmpE 515" Item. How do get the other items/values in my dropdownlist to display certain data if the user clicks a certain item?

这code位置:

string getText = SubjectList.SelectedItem.Text;

只会获取文本CMPE 515数据,即使我选择CMPE 516,然后点击继续。我如何得到其他项目在我的DropDownList?

it will only get the text 'CmpE 515' data even if I select the 'CmpE 516' and click proceed. How do I get the other Items in my dropdownlist?

推荐答案

您不应该下拉绑定每次加载页面,使用的IsPostBack 属性检查是否在页面加载只有在这种情况下,第一次和结合的下拉是这样的: -

You should not bind the dropdown everytime the page loads, use IsPostBack property to check if the page loads the first time and only in that case bind your dropdown like this:-

 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 页为好。

这篇关于获取DropDownList的文本从生成的列表项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆