【发布时间】:2020-01-24 14:34:47
【问题描述】:
我已成功加载 DropDownList。当我选择学生时,我加载的所有学生号码都没有相关的学生姓名应该显示在下面的文本框中。但现在如果选择任何数字,则只显示一个学生姓名,仅显示约翰姓名。如果我选择不同的学号。我不知道为什么。
DropDownList 加载代码
string cmdstr = "从记录中选择 id"; SqlCommand cmd = new SqlCommand(cmdstr, con);
con.Open();
read = cmd.ExecuteReader();
DropDownList1.Items.Clear();
while (read.Read())
{
DropDownList1.Items.Add(read["id"].ToString());
}
con.Close();
选定数据
<asp:DropDownList ID="DropDownList1" ViewStateMode="Enabled" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"></asp:DropDownList>
if (Page.IsPostBack == true)
{
string cmdstr = "select firstname from records where id = " +
DropDownList1.SelectedValue;
SqlCommand cmd = new SqlCommand(cmdstr, con);
con.Open();
read = cmd.ExecuteReader();
while (read.Read())
{
name.Text = read["firstname"].ToString();
}
con.Close();
}
【问题讨论】:
标签: asp.net