我有一个ASP页面,其中有一个下拉列表。值的完整列表绑定在数据库表“ a”的下拉列表中。从该下拉列表中选择任何值后,我将其保存到数据库表“ b”。 现在,在第二个ASP页面中,我想要具有从表“ b”中选择的值的下拉列表。 我的aspx页面:
<asp:DropDownList DataSource='<%# getBankTable() %>' ID="ddlBankName" DataValueField='BANK_ID'
DataTextField="BANK_DESC" SelectedValue='<%# Eval("BANK_ID") %>' AppendDataBoundItems="true"
runat="server">
</asp:DropDownList>
我的.cs页面:
protected void Page_Load(object sender, EventArgs e)
{
string sql1 = "SELECT * FROM Master LEFT JOIN BANK ON Master.BANK_ID = Transaction.BANK_ID";
OracleDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
ddlBankName.Items.FindByValue(dr["BANK_ID"].ToString()).Selected = true;
}
}
public void getBankTable()
{
ddlBankName.Items.Clear();
ddlBankName.Items.Insert(0, new ListItem("Select", ""));
clsDataAccess cls = new clsDataAccess();
string sql = "SELECT BANK_ID,BANK_DESC FROM Master";
DataTable dt = cls.GetDataTable(sql);
ddlBankName.DataTextField = "BANK_DESC";
ddlBankName.DataValueField = "BANK_ID";
ddlBankName.DataSource = dt;
ddlBankName.DataBind();
}
我无法做到这一点。请帮忙!
答案 0 :(得分:0)
我个人认为最好在CS中填充下拉列表并在那里设置值。
<asp:DropDownList ID="ddlBankName" DataValueField='BANK_ID' DataTextField="BANK_DESC" AppendDataBoundItems="true" runat="server">
</asp:DropDownList>
在您的CS页面中:
protected void Page_Load(object sender, EventArgs e) {
//Only fill it once on page load:
if (!Page.IsPostBack) {
getBankTable();
string sql1 = "SELECT * FROM Master LEFT JOIN BANK ON Master.BANK_ID = Transaction.BANK_ID";
OracleDataReader dr = cmd.ExecuteReader();
while (dr.Read()) {
if (dr["BANK_ID"] != null)
{
ddlBankName.Items.FindByValue(dr["BANK_ID"].ToString()).Selected = true;
}
}
}
}
public void getBankTable() {
ddlBankName.Items.Clear();
ddlBankName.Items.Insert(0, new ListItem("Select", ""));
clsDataAccess cls = new clsDataAccess();
string sql = "SELECT BANK_ID,BANK_DESC FROM Master";
DataTable dt = cls.GetDataTable(sql);
ddlBankName.DataTextField = "BANK_DESC";
ddlBankName.DataValueField = "BANK_ID";
ddlBankName.DataSource = dt;
ddlBankName.DataBind();
}