三个dropdownlist菜单,都是从数据库中读的数据,第一个选择后,第二个的从数据库中读相应的数据,第三个读第二个选中的,每次选择都会从数据库里读对应的,部分代码如下:
aspx:
<form id="Form1" method="post" runat="server">
<table>
<tr>
<td>
<table>
<tr>
<td>机构:</td>
<td><asp:dropdownlist id="ddli_jg" runat="server" AutoPostBack="true" Width="180"></asp:dropdownlist></td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<table>
<tr>
<td>部门:</td>
<td><asp:dropdownlist id="ddli_bm" runat="server" AutoPostBack="true" Width="180"></asp:dropdownlist></td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<table>
<tr>
<td>证件号:</td>
<td><asp:dropdownlist id="ddli_zjh" runat="server" AutoPostBack="true" Width="180"></asp:dropdownlist></td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<asp:button id="submit" runat="server" Width="50" Text="提交" Height="25"></asp:button>
<asp:button id="reset" runat="server" Width="50" Text="取消" Height="25"></asp:button>
</td>
</tr>
</table>
</form>
cs:
protected System.Web.UI.WebControls.DropDownList ddli_bm;
protected System.Web.UI.WebControls.DropDownList ddli_jg;
protected System.Web.UI.WebControls.DropDownList ddli_zjh;
...
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
try
{
SqlConnection myConnection=new SqlConnection(common_cs.database.connectionstr);
myConnection.Open();
string strjg="select * from JGBMB where prejgbmid=0";
SqlDataAdapter myDataAdapter=new SqlDataAdapter(strjg,myConnection);
DataSet myds=new DataSet();
myDataAdapter.Fill(myds,"JGBMB");
int n=myds.Tables["JGBMB"].Rows.Count;
ddli_jg.DataValueField=myds.Tables["JGBMB"].Columns["jgbmid"].ToString();
ddli_jg.DataTextField=myds.Tables["JGBMB"].Columns["jgbmm"].ToString();
ddli_jg.DataSource=myds.Tables["JGBMB"].DefaultView;
ddli_jg.DataBind();
myConnection.Close();
}
catch(Exception ex)
{
Response.Write("<script>alert('"+ex.Message+"')</script>");
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.ddli_jg.SelectedIndexChanged += new System.EventHandler(this.ddli_jg_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void ddli_jg_SelectedIndexChanged(object sender, System.EventArgs e) //选择机构
{
SqlConnection myConnection=new SqlConnection(common_cs.database.connectionstr);
myConnection.Open();
string strbm="select * from JGBMB where prejgbmid='"+ddli_jg.SelectedItem.Value+"'";
SqlDataAdapter myDataAdapter1=new SqlDataAdapter(strbm,myConnection);
DataSet myds1=new DataSet();
myDataAdapter1.Fill(myds1,"JGBMB");
ddli_bm.Items.Clear();
ddli_bm.DataValueField=myds1.Tables["JGBMB"].Columns["jgbmid"].ToString();
ddli_bm.DataTextField=myds1.Tables["JGBMB"].Columns["jgbmm"].ToString();
ddli_bm.DataSource=myds1.Tables["JGBMB"].DefaultView;
ddli_bm.DataBind();
myConnection.Close();
}
....
第一个能绑定数据 ,但是第二个就不能,是空的 ,我跟了一下程序ddli_jg_SelectedIndexChanged这个过程根本没执行??为什么??请前辈指点,谢谢