c# - 如何使用 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
下拉列表第一个元素的值。
解决方案
尝试这个
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();
}
推荐阅读
- c - 为什么我在分叉时会得到这个变量的两个值?
- python - 无法将张量添加到批次:元素数量不匹配。形状是:[张量]:[2],[批次]:[5]
- html - 在线时,Photoswipe 画廊导航按钮不起作用
- reactjs - 根据 id 渲染对象的信息
- c# - Outlook 加载项中的回复、全部回复和转发事件处理程序消失?
- python - Laravel 用什么工具来抓取
- ms-access - 我的 TDBgrid 在时间字段中显示 1899/12/30
- laravel - laravel 通过 appServiceProvider 制作自定义验证模板时发送两封邮件而不是一封邮件
- python - 应用(余弦)相似性度量 - 熊猫数据框
- html - 在 Angular Typescript 中将数组中的数据记录到 console.log