首页 > 解决方案 > 如何使用 SQL Server 从 DropDownList 的值重定向?

问题描述

我在根据下拉列表中的选定值将页面重定向到另一个页面时遇到问题。

我可以使用 SQL Server 在下拉列表中列出类别。

namespace Deneme
{
    public class Baglanti
    {
        public SqlConnection Baglanma()
        {
            //integrated security windows auth için.
            SqlConnection baglan = new SqlConnection("Server=DESKTOP-IB3QGLL;Database=Sports;Integrated Security = True");
            baglan.Open();
            SqlConnection.ClearPool(baglan);
            SqlConnection.ClearAllPools();
            return (baglan);
        }
    }
}

protected void Page_Load(object sender, EventArgs e)
{
    SqlCommand komut2 = new SqlCommand("SELECT clubid,club_name FROM kulupler", baglan.Baglanma());
    SqlDataReader reader  = komut2.ExecuteReader();
baglan.Baglanma();

    DDLProduct.DataSource = reader;
    DDLProduct.DataValueField = "clubid";
    DDLProduct.DataTextField = "club_name";
    DDLProduct.DataBind();
}

protected void BtnGonder_Click(object sender, EventArgs e)
{
    Response.Redirect("Category.aspx?ID="+DDLProduct.SelectedValue);
}

产品:

<asp:DropDownList ID="DDLProduct" runat="server">
</asp:DropDownList>

<asp:button id="BtnGonder" runat="server" text="Gönder" OnClick="BtnGonder_Click">
</asp:button>

我可以在下拉列表中列出所有类别。当我单击按钮时,它总是转到Category.aspx?ID=1下拉列表第一个元素的值。

标签: c#asp.netsql-server

解决方案


尝试这个

protected void Page_Load(object sender, EventArgs e)
{
  DataTable dtblItemList = new DataTable();
  using (SqlConnection conn = DataExecutor.GetSqlConnection()){
   conn.Open();
   SqlCommand komut2 = new SqlCommand("SELECT clubid,club_name FROM 
                       kulupler", conn);
   //SqlDataReader reader  = komut2.ExecuteReader();
   using(SqlDataReader reader = DataExecutor.GetSqlDataReader(komut2, 
        CommandBehavior.Default))
                            {
                                dtblItemList.Load(reader);
                            }      
    }

  DDLProduct.DataSource = dtblItemList;
  DDLProduct.DataValueField = "clubid";
  DDLProduct.DataTextField = "club_name";
  DDLProduct.DataBind();
}

推荐阅读