使用 DropDownList 将数据库信息动态检索到标签中

Retrieve database information dynamically into label using DropDownList

我正在尝试将数据动态检索到标签中。意思是说标签将随着我更改 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();
        }
    }
}

参考图片:

我对所有这些都很陌生,非常感谢任何帮助:)

在考虑了您的情况后,我认为这将是可行的解决方案。在 Page load 中绑定您的 DropDownList,然后 onselctedIndexChanged 我们可以根据所选的 CardID 获取数据并绑定所有需要的标签 data.You 可以根据您的规范优化代码。尝试一次:

string _connStr =ConfigurationManager.ConnectionStrings[""].ConnectionString;
        string query = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                query = "SELECT Card_ID,Card_Name From Card_Details"; // Do Change your query according to your requirment
                DataTable drpdt = SelectedBindValue(query);
                if (drpdt.Rows.Count > 0)
                {
                    drp.DataSource = drpdt;
                    drp.DataTextField = "Card_Name";
                    drp.DataValueField = "Card_ID";
                    drp.DataBind();
                }
            }
        }

        protected void Unnamed_SelectedIndexChanged(object sender, EventArgs e)
        {
            string cardId = drp.SelectedValue;
            query = String.Format("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 = {0}",cardId);
            DataTable lablebind = SelectedBindValue(query);
            if (lablebind.Rows.Count>0)
            {
                Lbl_CardName.Text = lablebind.Rows[0]["Name_On_Card"].ToString(); // considering there is only one row
                Lbl_CardType.Text = lablebind.Rows[0]["Card_Type"].ToString();// considering there is only one row
                Lbl_EDate.Text = lablebind.Rows[0]["Expired_Date"].ToString();// considering there is only one row
            }
        }
        protected DataTable SelectedBindValue(string query) 
        { 
            using(SqlConnection myConnect2 = new SqlConnection(_connStr))
            {               
                using(SqlDataAdapter sqd = new SqlDataAdapter(query,myConnect2))
                {
                    using (DataTable dt = new DataTable())
                    {
                        sqd.Fill(dt);
                        return dt;
                    }
                }
            }
        }