如何将下拉列表值作为存储过程的参数传递

本文关键字:存储过程 参数传递 下拉列表 | 更新日期: 2023-09-27 18:08:03

我正在创建两个基本上绑定的下拉列表,一个用于PROVINCE和一个用于CITY。我有一个c# web应用程序项目:我希望城市的列表根据PROVINCE下拉列表中选择的项目进行更改。但是每次从PROVINCE列表中选择一个新项目时,CITY列表中的数据不会改变。我不知道问题出在哪里。它是在选定的索引更改或…?tnx

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
namespace WebApplication3_DrpDwnListAndSQL
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                func1();
                func2();
                //func3();
            }
        }
        protected void DrpDwnProvince_SelectedIndexChanged(object sender, EventArgs e)
        {
            Label1.Text = DrpDwnProvince.SelectedValue;
            //string a = DrpDwnProvince.SelectedValue;
        }


        //First function for Province DrpDwnList 
        private void func1()
        {
            SqlConnection con = new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=DrpBoxDB;Data Source=ARASH-PC");
            SqlDataAdapter com = new SqlDataAdapter("_Province_selectAll", con);
            com.SelectCommand.CommandType = CommandType.StoredProcedure;
            DataSet ds = new DataSet();
            if (com != null)
            { com.Fill(ds); }
            DrpDwnProvince.DataSource = ds;
            DrpDwnProvince.DataTextField = "Prv_Name";
            DrpDwnProvince.DataValueField = "Prv_Id";
            DrpDwnProvince.DataBind();
        }
        //Second function for City DrpDwnList
        private void func2()
        {
            SqlConnection con = new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=DrpBoxDB;Data Source=ARASH-PC");
            SqlDataAdapter com1 = new SqlDataAdapter("_City_selectItems", con);
            com1.SelectCommand.CommandType = CommandType.StoredProcedure;
            com1.SelectCommand.Parameters.AddWithValue("@vari", DrpDwnProvince.SelectedValue);
            DataSet ds1 = new DataSet();
            if (com1 != null)
            { com1.Fill(ds1); }
            DrpDwnCity.DataSource = ds1;
            DrpDwnCity.DataTextField = "Cty_Name";
            DrpDwnCity.DataValueField = "Cty_Id";
            DrpDwnCity.DataBind();
        }
        //Third function for District DrpDwnList
       // private void func3()
       // {
      //  }

    }
}

如何将下拉列表值作为存储过程的参数传递

一种可能是AutoPostBack="true"属性没有在.aspx文件的下拉列表中设置。这是在用户选择不同项时让页面响应所必需的。

例如:

<asp:DropDownList ID="DrpDwnProvince" 
    OnSelectedIndexChanged="DrpDwnProvince_SelectedIndexChanged" 
    AutoPostBack="true" runat="server" />

Update:根据下面的注释-看起来问题是func2()在事件处理程序中没有被调用。所以解决方案是这样的:

protected void DrpDwnProvince_SelectedIndexChanged(object sender, EventArgs e)
{
    func2();
}