问题是我从下拉列表中获取名称,然后从表中获取该名称的学生ID。我在访问下拉列表值时遇到问题
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl = sender as DropDownList;
stud_name1 = ddl.SelectedValue;// sends the value
// Label1.Text = ddl.SelectedValue;
string getquery = "select studnt_id from Student where name='"+stud_name1+"'";
getidcon.Open();
SqlCommand getid = new SqlCommand(getquery, getidcon);
stdid1 = ((int)getid.ExecuteScalar());// gives the id in return to selection
// Session [stdid1]
// Label1.Text = stdid1.ToString();// testing value
// get the roll number
}
让我将它存储在stdid1全局变量的ID之后,但我没有得到存储在按钮代码stdid1变量的值。
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = stdid1.ToString();
}
为什么你首先将它存储在全局变量中。为什么你不能像'Label1.Text = DropDownList1.SelectedValue.ToString()' – ZedBee 2013-02-17 21:26:24
那样访问它就是这样。我从下拉列表中获得名称,然后我得到与该名称表单数据库相对应的id。 当用户点击按钮进行注册时,我在注册表中输入该ID,因为它与该表具有外键关系。 – 2013-02-17 21:37:15