Asp.Net下拉菜单无法正常工作

3

我正在制作一个有三个下拉菜单的界面,分别为国家城市工厂

国家城市下拉菜单已经能够正确地工作了(当选择一个国家时,它会显示包含的城市),但是当选择一个城市时,却没有展示出该城市所包含的工厂。请问我应该如何解决这个问题?

     GodownCls godownCls = new GodownCls();
     CityCls ctyCls = new CityCls();
     FactoryCls facCls = new FactoryCls();


     LoadCountries();

 private void LoadCountries()
        {
            CountryCls objCountry = new CountryCls();

            DataTable dtCountry = objCountry.Country_SelectAll();

            ddlCountry.DataSource = dtCountry;
            ddlCountry.DataTextField = "Name";
            ddlCountry.DataValueField = "ID";
            ddlCountry.DataBind();

        }

        private void LoadFactories(int CityId)
        {
            FactoryCls objFactory = new FactoryCls();

            DataTable dtFactory = objFactory.FactoriesByCity(CityId);

            ddlFactory.DataSource = dtFactory;
            ddlFactory.DataTextField = "Name";
            ddlFactory.DataValueField = "ID";
            ddlFactory.DataBind();
            ddlFactory.SelectedIndex = 0;
        }

        private void LoadCities(int CountryId)
        {
            CityCls objCity = new CityCls();

            DataTable dtCity = objCity.CitiesByCountry(CountryId);

            ddlCity.DataSource = dtCity;
            ddlCity.DataTextField = "Name";
            ddlCity.DataValueField = "ID";
            ddlCity.DataBind();
            ddlCity.SelectedIndex = 0;
        }

 protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
        {
            int selectedCountryId = Convert.ToInt32(ddlCountry.SelectedValue);
            LoadCities(selectedCountryId);
        }
        protected void ddlFactory_SelectedIndexChanged(object sender, EventArgs e)
        {

            int selectedFactoryId = Convert.ToInt32(ddlFactory.SelectedValue);
            LoadCities(selectedFactoryId);

        }

实际上现在所有的工厂都在下拉菜单中显示。

完整代码

 public partial class Godown : System.Web.UI.Page
    {
        #region "---- Variables & ViewStates ----"

        clsCommonMethods clsComMethods = new clsCommonMethods();

        public DataTable dtGodownHelp
        {
            get { return (DataTable)ViewState["dtGodownHelp"]; }
            set { ViewState["dtGodownHelp"] = value; }
        }

        public String CurrentMode
        {
            get { return (String)ViewState["CurrentMode"]; }
            set { ViewState["CurrentMode"] = value; }
        }

        public int SelectedGodownId
        {
            get { return (int)ViewState["SelectedGodownId"]; }
            set { ViewState["SelectedGodownId"] = value; }
        }

        public int SelectedUserId
        {
            get { return (int)ViewState["SelectedUserId"]; }
            set { ViewState["SelectedUserId"] = value; }
        }

        public bool ViewRight
        {
            get { return (bool)ViewState["ViewRight"]; }
            set { ViewState["ViewRight"] = value; }
        }

        public bool CreateRight
        {
            get { return (bool)ViewState["CreateRight"]; }
            set { ViewState["CreateRight"] = value; }
        }

        public bool UpdateRight
        {
            get { return (bool)ViewState["UpdateRight"]; }
            set { ViewState["UpdateRight"] = value; }
        }

        #endregion

        GodownCls godownCls = new GodownCls();

        CityCls ctyCls = new CityCls();
        FactoryCls facCls = new FactoryCls();

        #region Page Load
        protected void Page_Load(object sender, EventArgs e)
        {
            this.UnobtrusiveValidationMode = System.Web.UI.UnobtrusiveValidationMode.None;
            if (!IsPostBack)
            {
                if ((Session["UserId"] == null))
                {
                    FormsAuthentication.SignOut();
                    Response.Redirect("~/WebForms/Home/Login.aspx");
                }
                else
                {
                    SelectedUserId = int.Parse(Session["UserId"].ToString());

                    string pageName = "godown";

                    DataTable dtUp = UserPermission(pageName);
                    if (dtUp.Rows.Count > 0)
                    {
                        ViewRight = Convert.ToBoolean(dtUp.Rows[0]["isViewable"]);
                        CreateRight = Convert.ToBoolean(dtUp.Rows[0]["isCreatable"]);
                        UpdateRight = Convert.ToBoolean(dtUp.Rows[0]["isEditable"]);
                    }

                    if (ViewRight != true)
                    {
                        Response.Redirect("~/WebForms/Home/AccessDenied.aspx");
                    }
                    else
                    {
                        SetSavePermission(CreateRight);
                        SetEditPermission(UpdateRight);
                    }
                }

                ClearControls();
                EnableControls(false);
                LoadCountries();


            }
        }

        private void SetSavePermission(bool EnableStatus)
        {
            btnNew.Enabled = EnableStatus;
            btnSave.Enabled = EnableStatus;
        }

        private void SetEditPermission(bool EnableStatus)
        {
            btnUpdate.Enabled = EnableStatus;
            btnSave.Enabled = EnableStatus;

        }

        #endregion

        private DataTable UserPermission(string mPageCode)
        {
            int UserId = int.Parse(Session["UserId"].ToString());
            DataTable dtPermission = new DataTable();

            dtPermission = clsComMethods.GetUserWisePermissions(UserId, mPageCode);

            return dtPermission;
        }

        #region Enable Disable Controls
        private void EnableControls(bool Status)
        {
            txtGodowncode.Enabled = Status;
            txtGodownname.Enabled = Status;
            ddlCountry.Enabled = Status;
            //ddlCity.Enabled = Status;
            //ddlFactory.Enabled = Status;
            txtEmail.Enabled = Status;
            txtFax.Enabled = Status;
            txtGodown.Enabled = Status;
            txtGodownAdd.Enabled = Status;
            txtGodowncontact.Enabled = Status;
            txtPhn1.Enabled = Status;
            txtPhn2.Enabled = Status;
            txtTelex.Enabled = Status;
            txtWebSite.Enabled = Status;

            txtTelex.Enabled = Status;

            if (Status == true)
            {
                txtRemarks.Disabled = false;
            }
            else
            {
                txtRemarks.Disabled = true;
            }
            chkStatus.Enabled = Status;
            btnGodownhelp.Enabled = Status;
        }
        #endregion

        private void LoadCountries()
        {
            CountryCls objCountry = new CountryCls();

            DataTable dtCountry = objCountry.Country_SelectAll();

            ddlCountry.DataSource = dtCountry;
            ddlCountry.DataTextField = "Name";
            ddlCountry.DataValueField = "ID";
            ddlCountry.DataBind();

        }

        private void LoadFactories(int CityId)
        {
            FactoryCls objFactory = new FactoryCls();

            DataTable dtFactory = objFactory.FactoriesByCity(CityId);

            ddlFactory.DataSource = dtFactory;
            ddlFactory.DataTextField = "Name";
            ddlFactory.DataValueField = "ID";
            ddlFactory.DataBind();
            ddlFactory.SelectedIndex = 0;
        }

        private void LoadCities(int CountryId)
        {
            CityCls objCity = new CityCls();

            DataTable dtCity = objCity.CitiesByCountry(CountryId);

            ddlCity.DataSource = dtCity;
            ddlCity.DataTextField = "Name";
            ddlCity.DataValueField = "ID";
            ddlCity.DataBind();
            ddlCity.SelectedIndex = 0;
        }



        #region Button Disable
        private void DisableButtons()
        {
            btnSave.Enabled = false;
            btnUpdate.Enabled = true;
            btnInquiry.Enabled = true;
            btnCancel.Enabled = false;
        }
        #endregion

        #region Clear Controls
        private void ClearControls()
        {
            txtGodowncode.Text = "";
            txtGodownname.Text = "";
            ddlCountry.SelectedIndex = 0;
            //ddlCity.SelectedIndex = 0;
            //ddlFactory.SelectedIndex = 0;
            txtGodowncontact.Text = "";
            txtWebSite.Text = "";
            txtGodownAdd.Text = "";

            txtPhn1.Text = "";
            txtPhn2.Text = "";
            txtEmail.Text = "";
            txtFax.Text = "";

            txtTelex.Text = "";

            lblMsg.Text = "";
            txtRemarks.Value = "";
            chkStatus.Checked = false;
            SelectedGodownId = -1;


            if (CurrentMode == "Modify")
            {
                txtGodowncode.Enabled = true;
            }
        }
        #endregion

        #region New Button Click
        protected void btnNew_Click(object sender, EventArgs e)
        {
            try
            {



                ClearControls();
                CurrentMode = "Add";
                lblMode.Text = "New Record";
                lblMode.ForeColor = System.Drawing.Color.Yellow;
                SelectedGodownId = -1;
                btnUpdate.Enabled = false;
                btnInquiry.Enabled = false;
                btnCancel.Enabled = true;
                EnableControls(true);
                txtGodowncode.Enabled = false;
                btnGodownhelp.Enabled = false;
                btnSave.Enabled = true;
                btnClear.Enabled = true;
                chkStatus.Checked = true;
                txtGodownname.Focus();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region Update Button Click
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                CurrentMode = "Modify";
                lblMode.Text = "Modify Record";
                lblMode.ForeColor = System.Drawing.Color.Yellow;
                btnNew.Enabled = false;
                btnInquiry.Enabled = false;
                btnCancel.Enabled = true;
                txtGodowncode.Enabled = true;
                txtGodownname.Enabled = false;
                ddlCountry.Enabled = false;
                ddlCity.Enabled = false;
                txtGodowncontact.Enabled = false;
                txtWebSite.Enabled = false;
                txtGodownAdd.Enabled = false;
                ddlFactory.Enabled = true;
                txtPhn1.Enabled = false;
                txtPhn2.Enabled = false;
                txtEmail.Enabled = false;
                txtFax.Enabled = false;


                txtTelex.Enabled = false;

                txtRemarks.Disabled = true;
                chkStatus.Enabled = false;
                btnGodownhelp.Enabled = true;
                btnSave.Enabled = false;
                btnClear.Enabled = true;
                txtGodowncode.Focus();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region Inquiry Button Click
        protected void btnInquiry_Click(object sender, EventArgs e)
        {
            try
            {
                CurrentMode = "Inquiry";
                lblMode.Text = "Inquiry Record";
                lblMode.ForeColor = System.Drawing.Color.Yellow;
                btnNew.Enabled = false;
                btnUpdate.Enabled = false;
                txtGodowncode.Enabled = true;
                btnGodownhelp.Enabled = true;
                btnSave.Enabled = false;
                btnClear.Enabled = true;
                btnCancel.Enabled = true;
                txtGodownname.Focus();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region Clear Button Click
        protected void btnClear_Click(object sender, EventArgs e)
        {
            ClearControls();
        }
        #endregion

        #region Cancel Button Click
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                CurrentMode = "Cancel";
                btnNew.Enabled = true;
                btnInquiry.Enabled = true;
                btnUpdate.Enabled = true;
                btnCancel.Enabled = false;
                btnSave.Enabled = false;
                btnClear.Enabled = false;
                EnableControls(false);
                ClearControls();


                SetSavePermission(CreateRight);
                SetEditPermission(UpdateRight);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region Save Button Click
        protected void btnSave_Click(object sender, EventArgs e)
        {

            try
            {

                string strGodownCode;
                int statusId;

                if (CurrentMode == "Add")
                {
                    strGodownCode = "";
                }
                else
                {
                    strGodownCode = txtGodowncode.Text.ToString();
                }

                if (chkStatus.Checked == true)
                {
                    statusId = 8;
                }
                else
                {
                    statusId = 9;
                }

                int output = godownCls.InsertGodown(SelectedGodownId, strGodownCode, txtGodownname.Text, int.Parse(ddlCountry.SelectedValue), int.Parse(ddlCity.SelectedValue), int.Parse(ddlFactory.SelectedValue), txtGodowncontact.Text, txtWebSite.Text, txtGodownAdd.Text, txtRemarks.Value.ToString(), txtPhn1.Text, txtPhn2.Text, txtEmail.Text, txtFax.Text, txtTelex.Text, statusId, SelectedUserId, CurrentMode.ToString());
                if (output > 0)
                {
                    if (CurrentMode == "Add")
                    {
                        txtGodowncode.Text = "GDN" + output.ToString("00000");
                        lblMsg.Text = "Successfully Saved!";
                        lblMsg.ForeColor = System.Drawing.Color.Green;
                    }
                    else if (CurrentMode == "Modify")
                    {
                        lblMsg.Text = "Successfully Updated!";
                        lblMsg.ForeColor = System.Drawing.Color.Green;
                    }
                }
                else if (output == -3)
                {
                    lblMsg.Text = "Already Exists!";
                    lblMsg.ForeColor = System.Drawing.Color.Orange;
                }
                else if (output == -1)
                {
                    lblMsg.Text = "Save Unsuccessful! Code Error!";
                    lblMsg.ForeColor = System.Drawing.Color.Red;
                }
                else if (output == -2)
                {
                    lblMsg.Text = "Save Unsuccessful! SP Error!";
                    lblMsg.ForeColor = System.Drawing.Color.Red;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion


        #region GDN Code help Button Click
        protected void btnGodownhelp_Click(object sender, EventArgs e)
        {
            try
            {
                lblMsg.Text = "";
                txtGodownname.Text = "";
                txtRemarks.Value = "";
                chkStatus.Checked = false;

                DataTable dtGodown = godownCls.FGetGodown(txtGodowncode.Text.ToString());

                if (dtGodown.Rows.Count > 0)
                {
                    dtGodownHelp = dtGodown;
                    Session["Help"] = "Godown";

                    gvHelp.DataSource = dtGodown;
                    gvHelp.DataBind();

                    gvHelp.HeaderRow.Cells[3].Visible = false;  //Godown Id
                    gvHelp.HeaderRow.Cells[4].Visible = false;  //Status Id 
                    gvHelp.HeaderRow.Cells[5].Visible = false;  //Remarks
                    foreach (GridViewRow gvr in gvHelp.Rows)
                    {
                        gvr.Cells[3].Visible = false;   //Godown Id
                        gvr.Cells[4].Visible = false;   //Status Id 
                        gvr.Cells[5].Visible = false;   //Remarks 
                    }


                    //rgData.DataSource = dtColour;
                    //rgData.DataBind();

                    mpConfirm.Show();
                }
                else
                {
                    lblMsg.Text = "No Record Found";
                    lblMsg.ForeColor = System.Drawing.Color.Red;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region Help Grid Row Command
        protected void gvHelp_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {

                int index = Convert.ToInt32(e.CommandArgument);
                GridViewRow selectedRow = gvHelp.Rows[index];

                if (Session["Help"].ToString() == "Godown")
                {
                    SelectedGodownId = Convert.ToInt32(selectedRow.Cells[3].Text);

                    DataRow[] dr = dtGodownHelp.Select("[Godown Id] = " + SelectedGodownId);

                    if (CurrentMode == "Modify")
                    {
                        txtGodowncode.Enabled = false;
                        txtGodownname.Enabled = true;
                        ddlCountry.Enabled = true;
                        chkStatus.Enabled = true;
                        txtRemarks.Disabled = false;
                        btnSave.Enabled = true;
                    }
                    else if (CurrentMode == "Inquiry")
                    {
                        txtGodowncode.Enabled = true;
                        txtGodownname.Enabled = false;
                        ddlCountry.Enabled = false;
                        chkStatus.Enabled = false;
                        txtRemarks.Disabled = true;
                        btnSave.Enabled = false;
                    }

                    int countryId = Convert.ToInt32((dr[0]["Country Id"]).ToString());
                    LoadFactories(countryId);

                    int factoryId = Convert.ToInt32((dr[0]["Factory Id"]).ToString());
                    LoadFactories(factoryId);


                    txtGodowncode.Text = dr[0]["Godown Code"].ToString();
                    txtGodownname.Text = dr[0]["Godown Name"].ToString();
                    ddlCountry.SelectedValue = (dr[0]["Country Id"]).ToString();
                    ddlFactory.SelectedValue = (dr[0]["Factory Id"]).ToString();
                    ddlCity.SelectedValue = (dr[0]["City Id"]).ToString();
                    txtGodowncontact.Text = dr[0]["GodownContactPerson"].ToString();
                    txtWebSite.Text = dr[0]["GodownWebsite"].ToString();
                    txtGodownAdd.Text = dr[0]["GodownAddress"].ToString();
                    txtPhn1.Text = dr[0]["PhoneNumber"].ToString();
                    txtPhn2.Text = dr[0]["Mobile"].ToString();
                    txtEmail.Text = dr[0]["Email"].ToString();
                    txtFax.Text = dr[0]["Fax"].ToString();

                    txtTelex.Text = dr[0]["Telex"].ToString();


                    if (Convert.ToInt32(dr[0]["Status Id"]) == 8)
                    {
                        chkStatus.Checked = true;
                    }
                    else
                    {
                        chkStatus.Checked = false;
                    }
                    txtRemarks.Value = dr[0]["Remarks"].ToString();
                }
            }
        }
        #endregion

        #region Help Grid Page Index Changing
        protected void gvHelp_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            // gvHelp.PageIndex = e.NewPageIndex;
            // gvHelp.DataSource = dt;
            // gvHelp.DataBind();
            // mpConfirm.Show();
        }
        #endregion

        protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
        {
            int selectedCountryId = Convert.ToInt32(ddlCountry.SelectedValue);
            LoadCities(selectedCountryId);
        }
        protected void ddlFactory_SelectedIndexChanged(object sender, EventArgs e)
        {

            int selectedFactoryId = Convert.ToInt32(ddlFactory.SelectedValue);
            LoadCities(selectedFactoryId);

        }

    }
}

FactoryCls

public class FactoryCls 
   {
     DataManipulation clsDataMan = new DataManipulation();

       public int InsertFactory
           (
           int FactoryId,
           string FactoryCode, 
           string FactoryName,
           int CountryId, 
           int CityId, 
           string FactoryContactPerson, 
           string FactoryWebsite, 
           string FactoryAddress,
           string TQB_No, 
           string Remarks,
           string PhoneNumber,
           string Mobile,
           string Email,
           string Fax,
           string DisplayNameForTQB,
           string Declarant_SequenceNo, 
           string Telex,  
           int StatusId,
           int UserId,
           string sMode
           )
        {
            int Output = 0;

            SqlParameter[] sqlParam = new SqlParameter[21];

            sqlParam[0] = new SqlParameter("@FactoryId", FactoryId);
            sqlParam[1] = new SqlParameter("@FactoryCode", FactoryCode);
            sqlParam[2] = new SqlParameter("@FactoryName", FactoryName);
            sqlParam[3] = new SqlParameter("@CountryId", CountryId);
            sqlParam[4] = new SqlParameter("@CityId", CityId);
            sqlParam[5] = new SqlParameter("@FactoryContactPerson", FactoryContactPerson);
            sqlParam[6] = new SqlParameter("@FactoryWebsite", FactoryWebsite);
            sqlParam[7] = new SqlParameter("@FactoryAddress", FactoryAddress);
            sqlParam[8] = new SqlParameter("@TQB_No", TQB_No);
            sqlParam[9] = new SqlParameter("@Remarks", Remarks);
            sqlParam[10] = new SqlParameter("@PhoneNumber", PhoneNumber);
            sqlParam[11] = new SqlParameter("@Mobile", Mobile);
            sqlParam[12] = new SqlParameter("@Email", Email);
            sqlParam[13] = new SqlParameter("@Fax", Fax);
            sqlParam[14] = new SqlParameter("@DisplayNameForTQB", DisplayNameForTQB);
            sqlParam[15] = new SqlParameter("@Declarant_SequenceNo", Declarant_SequenceNo);
            sqlParam[16] = new SqlParameter("@Telex", Telex);
            sqlParam[17] = new SqlParameter("@StatusId", StatusId);
            sqlParam[18] = new SqlParameter("@CreateId", UserId);
            sqlParam[19] = new SqlParameter("@Mode", sMode);
            sqlParam[20] = new SqlParameter("@iOutput", 0);
            sqlParam[20].Direction = ParameterDirection.Output;

            try
            {
                Output = clsDataMan.InsertData("Factory_InsertUpdate", sqlParam);
            }
            catch (Exception ex)
            {
                Output = -1;
            }
            return Output;
        }
        public DataTable Factory_SelectAll()
        {
            DataTable dtResults = new DataTable();

            dtResults = clsDataMan.RetrieveToDataSet("Factory_SelectAll").Tables[0];

            return dtResults;
        }
        #region Get Factory For Help
        public DataTable FGetFactory(string strFactoryCode)
        {
            DataTable rsResult = new DataTable();

            SqlParameter[] sqlParam = new SqlParameter[1];

            sqlParam[0] = new SqlParameter("@inFactory", strFactoryCode);


            rsResult = clsDataMan.RetrieveToDataSet("Factory_GetFactories", sqlParam).Tables[0];

            return rsResult;
        }
        #endregion

        //public DataTable LoadFactory()
        //{
        //    DataTable dtResults = new DataTable();
        //    SqlParameter[] sqlParam = new SqlParameter[0];

        //    dtResults = clsDataMan.RetrieveToDataSet("Factory_Select", sqlParam).Tables[0];

        //    return dtResults;

        //}




        public DataTable FactoriesByCity(int FactoryId)
        {
            DataTable rsResult = new DataTable();

            SqlParameter[] sqlParam = new SqlParameter[1];

            sqlParam[0] = new SqlParameter("@FactoryId", FactoryId);

            rsResult = clsDataMan.RetrieveToDataSet("Factory_FactoriesByCity", sqlParam).Tables[0];

            return rsResult;
        }


    }


}

创建一个最小(但完整)的代码片段来重现这个错误怎么样? - hagello
没有任何错误,但是我选择城市后没有显示工厂下拉菜单以包括工厂。 - Codeone
我有3个下拉菜单,我想创建3个具有级联效果的下拉菜单。 - Codeone
@PhantomAssassin 你需要使用 ddlCity_SelectedIndexChanged 来自下拉菜单选择城市,以便从 ddlCity 中填充工厂。 - Krsna Kishore
2个回答

6
您需要将 ddlCity_SelectedIndexChanged 添加到 asp 标记中,以及在 CS 代码中实现。 CS 代码:
protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
{
   int selectedCityId = Convert.ToInt32(ddlCity.SelectedValue);
   LoadFactories(selectedCityId);
}

我假设你的Load Factory方法如下。您需要验证文本和值字段名称是否正确来自数据集。
private void LoadFactories(int CityId)
        {
            FactoryCls objFactory = new FactoryCls();

            DataTable dtFactory = objFactory.FactoriesByCity(CityId);

            ddlFactory.DataSource = dtFactory;
            ddlFactory.DataTextField = "Name";
            ddlFactory.DataValueField = "ID";
            ddlFactory.DataBind();
            ddlFactory.SelectedIndex = 0;
        }

最后检查你的aspx标记,并确保:
  1. 你没有给下拉菜单重复的名称
  2. 确保在ddlCity中设置“AutoPostBack=true”

谢谢您的帮助,但我有一个小错误, "Procedure or function 'Factory_FactoriesByCity' expects parameter '@CityId', which was not supplied."} - Codeone
此存储过程为 `ALTER PROCEDURE [dbo].[Factory_FactoriesByCity] @CityId intAS BEGIN SELECT -1 as ID ,'选择工厂' as Name , 0 as RowNo UNION SELECT FactoryId AS ID
, FactoryName AS Name , 1 As RowNo FROM Factories WHERE CityId = @CityId AND statusId = 8 ORDER BY RowNo , Name; END`
- Codeone
1
先生,终于成功了。我没有正确设置这个参数和这一部分:sqlParam[0] = new SqlParameter("@CityId", CityId); - Codeone
@pahntomAssassin 呼,是的,这就是我在之前评论中提到的,我们的SQL将采用您在数据库中给出的相同名称,这就是错误的原因。无论如何,我很高兴能够帮助你。 - Krsna Kishore
让我们在聊天中继续这个讨论 - Codeone
显示剩余6条评论

0
哥们,你搞错了。如果你想按照这个顺序级联:国家 > 城市 > 工厂,你的代码应该像这样写:
protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
{
   int selectedCountryId = Convert.ToInt32(ddlCountry.SelectedValue);
   LoadCities(selectedCountryId);
}

protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
{
   int selectedCityId = Convert.ToInt32(ddlCity.SelectedValue);
   LoadFactories(selectedCityId);
}

同时移除 ddlFactory_SelectedIndexChanged,因为你不需要它。


你是否将 ddlCity_SelectedIndexChanged 事件附加到了 ddlCity 控件上? - Lesmian
仍然无法工作,选择了工厂所在城市,我选择了城市但未被选中。 - Codeone
我有一个错误信息 - 过程或函数 'Factory_FactoriesByCity' 需要参数 '@CityId',但未提供。 - Codeone
sp- `ALTER PROCEDURE [dbo].[Factory_FactoriesByCity] @CityId intAS BEGIN SELECT -1 as ID ,'选择工厂' as Name , 0 as RowNo UNION SELECT FactoryId AS ID
, FactoryName AS Name , 1 As RowNo FROM Factories WHERE CityId = @CityId AND statusId = 8 ORDER BY RowNo , Name; END `
- Codeone
看起来你正在执行的存储过程没有添加 CityId 参数。 - Lesmian
显示剩余5条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接