gpt4 book ai didi

c# - asp.net DropDownList 不适用于 ASPNET 4.5

转载 作者:行者123 更新时间:2023-11-30 19:26:54 25 4
gpt4 key购买 nike

下拉列表:

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

我做错了什么?

最佳答案

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

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

关于c# - asp.net DropDownList 不适用于 ASPNET 4.5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20673551/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com