簡體   English   中英

如何將下拉列表值作為存儲過程的參數傳遞

[英]how to pass Drop Down List value as a parameter for stored procedure

我正在創建兩個基本上綁定的DROP DOWN LIST,一個用於PROVINCE ,一個用於CITY 我有一個C#Web應用程序項目:我希望城市列表根據下拉列表中的選定項進行更改。 但是, 每次我從省列表中選擇一個新項目時,城市列表中的數據就不會更改。 我不知道問題出在哪里。 是在SELECTED INDEX CHANGED中還是...? n

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()
       // {

      //  }


    }
}

一種可能性是未在.aspx文件中的DropDownList上設置AutoPostBack="true"屬性。 為了使頁面在用戶選擇其他項目時做出響應,這是必需的。

例如:

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

更新:下面的每個注釋-看起來問題出在事件處理程序中未調用func2()。 所以解決方案將是這樣的:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM