asp.net DropDownList 在 ASPNET 4.5 中不起作用

本文关键字:不起作用 ASPNET net DropDownList asp | 更新日期: 2024-10-31 21:41:00

DropDownList:

protected void Page_Load(object sender, EventArgs e)
{
    try
    {
        string sqlCoursesList = "SELECT sno,subject_lbl FROM courses WHERE sub_status = 1";
        this.course_sno.DataSource = gm.dataTable(sqlCoursesList);
        this.course_sno.DataTextField = "subject_lbl";
        this.course_sno.DataValueField = "sno";
        this.course_sno.DataBind();
    }
    catch (Exception exc) {
        gm.catchError(exc);
    }
}

我类中的方法

public DataTable dataTable(string sql) {
    try {
        connect();
        dt = new DataTable();
        oda = new MySqlDataAdapter(sql, con);
        oda.Fill(dt);
    }
    catch (Exception exc) {
        catchError(exc);
    }
    finally {
        try {
            closeCon();
        }
        catch (Exception excf) {
            catchError(excf);
        }
    }
    return dt;
}

按下按钮上的代码:

protected void btnSearch_Click(object sender, EventArgs e)
{
    try
    {
        string year = this.adm_year.Text;
        string course_sno = this.course_sno.SelectedIndex.ToString();
        //this.searchTxt.Text = "Year: "+year+" Course Sno : "+course_sno;
        if (year.Equals("") || course_sno.Equals(""))
        {
            this.searchTxt.Text = gm.errorMsg("Error", "Session year and Course Must be selected in the fields", "../../images/error.png");
        }
        else
        {
            string sqlList = "SELECT sno,std_sno,opt_sno,course_sno,chosen_faculty,date_application,is_active " +
                             "FROM apply_for_course " +
                             "WHERE course_sno = '" + course_sno + "' AND " +
                             "YEAR(date_application) = '" + year + "'";
            if (gm.search(sqlList))
            {
                mdr = gm.getRecordSet(sqlList).ExecuteReader();
                if (mdr.HasRows)
                  }
........
}

现在,问题是,当我更改下拉选择并按提交按钮时,它只会获得第一个索引:

 string course_sno = this.course_sno.SelectedIndex.ToString();

我做错了什么?

asp.net DropDownList 在 ASPNET 4.5 中不起作用

您在每次页面加载时绑定/重新绑定下拉列表,这会将下拉列表的选定索引重置为列表中的第一项。第一次只绑定下拉列表,像这样:

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        try
        {
            string sqlCoursesList = "SELECT sno,subject_lbl FROM courses WHERE sub_status = 1";
            this.course_sno.DataSource = gm.dataTable(sqlCoursesList);
            this.course_sno.DataTextField = "subject_lbl";
            this.course_sno.DataValueField = "sno";
            this.course_sno.DataBind();
        }
        catch (Exception exc) {
            gm.catchError(exc);
        }
    }
}

不要在页面加载时绑定下拉列表。在代码中绑定一次