我正在创建基本绑定的两个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()
// {
// }
}
}
答案 0 :(得分:0)
一种可能性是.aspx文件中的DropDownList上没有设置AutoPostBack="true"
属性。这是为了在用户选择不同的项目时让页面响应。
E.g:
<asp:DropDownList ID="DrpDwnProvince"
OnSelectedIndexChanged="DrpDwnProvince_SelectedIndexChanged"
AutoPostBack="true" runat="server" />
更新:以下评论 - 看起来问题是func2()未在事件处理程序中被调用。所以解决方案就像:
protected void DrpDwnProvince_SelectedIndexChanged(object sender, EventArgs e)
{
func2();
}