利用DropDownList的DropDownList_BuName_SelectedIndexChanged事件。
注意红色部分,不写 AutoPostBack="True" ,事件不起作用的。
Asp页面上:
商户名称:
<asp:DropDownList ID="DropDownList_BuName" runat="server" Width='200px'
AutoPostBack="True"
onselectedindexchanged="DropDownList_BuName_SelectedIndexChanged">
</asp:DropDownList>
<asp:DropDownList ID="DropDownList_Pos" runat="server" DataValueField="PosNum"
DataTextField="PosNum">
</asp:DropDownList>
C#后台代码:
#region 选择商户之后,加载POS编号列表
protected void DropDownList_BuName_SelectedIndexChanged(object sender, EventArgs e)
{
string buName = DropDownList_BuName.SelectedItem.ToString();
if (buName != "-全部-")
{
DropDownList_Pos.DataSource = null;
//根据商户编号查询所有的POS编号
string querySql = "select PosNum from Tbl_Pos where Bucode = '" + buName.Split('-')[0].ToString() + "'";
DataSet posDs = DBFhkHelper.queryDB(querySql);
DataTable table = posDs.Tables[0];
DataTable table1 = new DataTable();
table1.Columns.Add("PosNum");
DataRow drNew = table1.NewRow();
drNew["PosNum"] = "-不选择POS-";
table1.Rows.Add(drNew);
table1.Merge(table);
DropDownList_Pos.DataSource = table1;
DropDownList_Pos.DataBind();
}
else
{
DropDownList_Pos.DataSource = null;
DropDownList_Pos.Items.Add("-不选择POS-");
DropDownList_Pos.DataBind();
}
}
#endregion 选择商户之后,加载POS编号列表