我试图动态地将数据检索到标签中。这意味着标签将随着我更改DropDownList所选项而改变。有可能吗?
ASPX:
<asp:DropDownList ID="ddl_CNo" AutoPostBack="true" runat="server" Height="16px" Width="138px" OnSelectedIndexChanged="ddl_CNo_SelectedIndexChanged" >
</asp:DropDownList>
aspx.cs:
//page load box retrieve
SqlConnection myConnect2 = new SqlConnection(_connStr);
string strCommandText2 = "SELECT Card_Type, Card_Type2, Card_Number, Card_Number2, Name_On_Card, Name_On_Card2, Expired_Date, Expired_Date2 From Card_Details where Card_Id = @Card_Id";
myConnect2.Open();
SqlCommand cmd2 = new SqlCommand(strCommandText2, myConnect2);
cmd2.Parameters.AddWithValue("@Card_Id", 1);
////open connection and retrieve data by calling ExecuteReader
SqlDataReader dr2 = cmd2.ExecuteReader();
if (dr2.Read())
{
string CNo1 = dr2["Card_Number"].ToString();
string CNo2 = dr2["Card_Number2"].ToString();
CNo = new List<string>();
CNo.Add(dr2["Card_Number"].ToString());
CNo.Add(dr2["Card_Number2"].ToString());
ddl_CNo.DataSource = CNo;
ddl_CNo.DataBind();
ddl_CNo.Text = dr2["Card_Number"].ToString();
ddl_CNo.Text = dr2["Card_Number2"].ToString();
Lbl_CardName.Text = dr2["Name_On_Card"].ToString();
Lbl_CardName.Text = dr2["Name_On_Card2"].ToString();
Lbl_CardType.Text = dr2["Card_Type"].ToString();
Lbl_CardType.Text = dr2["Card_Type2"].ToString();
Lbl_EDate.Text = dr2["Expired_Date"].ToString();
Lbl_EDate.Text = dr2["Expired_Date2"].ToString();
}
dr2.Dispose();
dr2.Close();
myConnect2.Close();
if (!IsPostBack)
{
LoadCart();
}
}
DDLSelectedIndexChanged:
protected void ddl_CNo_SelectedIndexChanged(object sender, EventArgs e)
{
{
SqlConnection myConnect2 = new SqlConnection(_connStr);
string strCommandText2 = "SELECT Card_Type, Card_Type2, Card_Number, Card_Number2, Name_On_Card, Name_On_Card2, Expired_Date, Expired_Date2 From Card_Details where Card_Id = @Card_Id";
myConnect2.Open();
SqlCommand cmd2 = new SqlCommand(strCommandText2, myConnect2);
cmd2.Parameters.AddWithValue("@Card_Id", 1);
SqlDataReader dr2 = cmd2.ExecuteReader();
if (dr2.Read())
{
string CNo1 = dr2["Card_Number"].ToString();
string CNo2 = dr2["Card_Number2"].ToString();
CNo = new List<string>();
CNo.Add(dr2["Card_Number"].ToString());
CNo.Add(dr2["Card_Number2"].ToString());
ddl_CNo.DataSource = CNo;
ddl_CNo.DataBind();
if (ddl_CNo.SelectedItem.Equals("Card_Number"))
{
Lbl_CardName.Text = dr2["Name_On_Card"].ToString();
Lbl_CardType.Text = dr2["Card_Type"].ToString();
Lbl_EDate.Text = dr2["Expired_Date"].ToString();
}
else if (ddl_CNo.SelectedItem.Equals("Card_Number2"))
{
Lbl_CardName.Text = dr2["Name_On_Card2"].ToString();
Lbl_CardType.Text = dr2["Card_Type2"].ToString();
Lbl_EDate.Text = dr2["Expired_Date2"].ToString();
}
dr2.Dispose();
dr2.Close();
myConnect2.Close();
}
}
}
我对所有这些都很陌生,非常感谢任何帮助:)
答案 0 :(得分:1)
在考虑了您的场景后,我认为这将是可行的解决方案。将您的DropDownList绑定到内部页面加载然后onselctedIndexChanged我们可以在选择的CardID的基础上获取数据并绑定所有需要数据的标签。您可以根据您的规范优化代码。试试一次:
1 -- 1 == 2