2

我正在创建两个基本上绑定的 DROP DOWN LIST,一个用于PROVINCE,一个用于CITY。我有一个 C# web 应用程序项目:我希望城市列表根据PROVINCE下拉列表中的选定项目进行更改。但是每次我从 PROVINCE 列表中选择一个新项目时,CITY 列表中的数据都不会改变。 我不知道问题出在哪里。它是在 SELECTED INDEX CHANGED 中还是...?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()
       // {

      //  }


    }
}
4

1 回答 1

0

一种可能性是该AutoPostBack="true"属性未在 .aspx 文件中的 DropDownList 上设置。这是为了在用户选择不同的项目时让页面响应所必需的。

例如:

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

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

protected void DrpDwnProvince_SelectedIndexChanged(object sender, EventArgs e)
{
    func2();
}
于 2013-04-08T06:16:43.997 回答