<%@ language="c#"  debug="true" %>
<%@ Import NameSpace="System.Data" %>
<%@ Import NameSpace="System.Data.SqlClient" %><script runat="server">
void Page_Load(Object sender, EventArgs e)
{
string sc=ConfigurationSettings.AppSettings["SQLCONNECTIONSTRING"];
SqlConnection conn=new SqlConnection(sc); if(!IsPostBack)
{
string sql="select questiontype from typeinfo";
SqlCommand scmd=new SqlCommand(sql,conn);
conn.Open();
SqlDataReader sdr;
sdr=scmd.ExecuteReader();
ddl.DataSource=sdr;
ddl.DataTextField="questiontype";
ddl.DataValueField="questiontype";
ddl.DataBind();
conn.Close();
} string sql2="select questionid,question from question where questiontype ='"+ddl.SelectedValue+"' ";
SqlCommand scmd2=new SqlCommand(sql2,conn);
conn.Open();
SqlDataReader sdr2;
sdr2=scmd2.ExecuteReader();
ltb.DataSource=sdr2;
ltb.DataTextField="question";
ltb.DataValueField="questionid";
ltb.DataBind();
conn.Close();
}void choose(Object sender, System.EventArgs e)
{
bt1.Text=ltb.SelectedValue;
}
</script><html>
<head>
<title>修改试题</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"></head> <body bgcolor="#ebebff">

<form active ="modifyquestion.aspx" runat = "server">
<table width="100%"  border="0">
                 <tr> <td width="12%" align="center"><font size="2">题目类型:</font></td>
<td width="88%">
<asp:dropdownlist id="ddl"  width="120"  runat="server" autopostback="true" />                                      </font>
</td>
</tr>
<tr>
                   <td align="center"><font size="2">题目列表:</font></td>
                   <td>
<asp:listbox id="ltb" width="480" TextMode="MultiLine" Rows="12" runat="server" />                                             </font>
</td>
                   </tr>
           </table>
<center>
<asp:Button id="bt1" Text ="修改试题" OnClick="choose" runat="server" />

</form>
</body>
</html>代码如上,sdr 帮定到一个dropdownlist,然后根据选择的值查询出符合条件的数据 帮定到listbox,但是提交的时候却死活不能获取在listbox中选择的值,很是郁闷啊

解决方案 »

  1.   

    实现选择得过程在page_load里靠刷新做的?
      

  2.   

    绑定 ListBox 为什么不写在 if(!IsPostBack){...} 里?你这样做提交后实际上先执行 Page_Load 里面的 绑定 ListBox然后才是按钮时间里的取值这样不管你选什么取值都只能取到 ListBox 的第一个值
      

  3.   

    不写在if(!IsPostBack){...} 里是因为listbox里的内容要随dropdownlist的内容变化而变化的。
    要怎么写才正确呢?好烦啊
      

  4.   

    那就写在 dropdownlist 的 onChange 事件里呀