gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 14:26:14 25 4
gpt4 key购买 nike

我做了一个 3 下拉菜单 1) 国家 2) 城市 3)工厂

CountryCity 下拉菜单正常工作(Country 选择显示的国家/地区以包含城市的)但city 未选择要包含的显示城市 >工厂的我该如何解决?你能给出解决方案吗?

     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;
}


}


}

最佳答案

您需要将 ddlCity_SelectedIndexChanged 添加到 asp 标记和您的

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”

关于c# - Asp.Net 下拉菜单无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36003947/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com