C# Dropdownlist控件值未保存到数据库
我在添加/编辑表单页面上有两个dropdownlist控件,一个用于名为CountryCode的国家,另一个用于名为ProvinceCode的地区/州。该代码已设置为,当用户从国家/地区下拉列表中选择美国时,区域下拉列表将填充所有有效的美国州。如果用户从国家/地区下拉列表中选择加拿大,则区域下拉列表将填充有效的省份。此功能工作正常,但当出于某种原因选择加拿大时,区域dropdownlist的值永远不会保存到数据库中。我不知道为什么会这样,特别是因为我可以很好地拯救美国各州 这是用于填充国家和地区下拉列表的代码:C# Dropdownlist控件值未保存到数据库,c#,sql,drop-down-menu,C#,Sql,Drop Down Menu,我在添加/编辑表单页面上有两个dropdownlist控件,一个用于名为CountryCode的国家,另一个用于名为ProvinceCode的地区/州。该代码已设置为,当用户从国家/地区下拉列表中选择美国时,区域下拉列表将填充所有有效的美国州。如果用户从国家/地区下拉列表中选择加拿大,则区域下拉列表将填充有效的省份。此功能工作正常,但当出于某种原因选择加拿大时,区域dropdownlist的值永远不会保存到数据库中。我不知道为什么会这样,特别是因为我可以很好地拯救美国各州 这是用于填充国家和地区
protected void Page_Init(object sender, EventArgs e)
{
CountryCode.DataSource = CountryDataSource.LoadAll("Name");
CountryCode.DataBind();
InitCountryAndProvince();
}
private void InitCountryAndProvince()
{
//MAKE SURE THE CORRECT ADDRESS IS SELECTED
Address address = this.Address;
bool foundCountry = false;
if (!string.IsNullOrEmpty(address.CountryCode))
{
ListItem selectedCountry = CountryCode.Items.FindByValue(address.CountryCode);
if (selectedCountry != null)
{
CountryCode.SelectedIndex = CountryCode.Items.IndexOf(selectedCountry);
foundCountry = true;
}
}
if (!foundCountry)
{
Warehouse defaultWarehouse = AbleContext.Current.Store.DefaultWarehouse;
ListItem selectedCountry = CountryCode.Items.FindByValue(defaultWarehouse.CountryCode);
if (selectedCountry != null) CountryCode.SelectedIndex = CountryCode.Items.IndexOf(selectedCountry);
}
//MAKE SURE THE PROVINCE LIST IS CORRECT FOR THE COUNTRY
UpdateCountry();
//NOW LOOK FOR THE PROVINCE TO SET
ListItem selectedProvince = ProvinceCode.Items.FindByValue(address.Province);
if (selectedProvince != null) ProvinceCode.SelectedIndex = ProvinceCode.Items.IndexOf(selectedProvince);
}
private void UpdateCountry()
{
//SEE WHETHER POSTAL CODE IS REQUIRED
string[] countries = AbleContext.Current.Store.Settings.PostalCodeCountries.Split(",".ToCharArray());
//PostalCodeRequired.Enabled = (Array.IndexOf(countries, Country.SelectedValue) > -1);
//SEE WHETHER PROVINCE LIST IS DEFINED
IList<Province> provinces = ProvinceDataSource.LoadForCountry(CountryCode.SelectedValue);
if (provinces.Count > 0)
{
IEnumerable<Province> sortedProvinces = provinces.OrderBy(f => f.Name);
provinces = sortedProvinces.ToList();
ProvinceCode.Visible = false;
ProvinceCode.Visible = true;
ProvinceCode.Items.Clear();
ProvinceCode.Items.Add(string.Empty);
foreach (Province province in provinces)
{
string provinceValue = (!string.IsNullOrEmpty(province.ProvinceCode) ? province.ProvinceCode : province.Name);
ProvinceCode.Items.Add(new ListItem(province.Name, provinceValue));
}
ListItem selectedProvince = FindSelectedProvince();
if (selectedProvince != null) selectedProvince.Selected = true;
ProvinceCode.Enabled = true;
ProvinceCode.Text = string.Empty;
}
else
{
ProvinceCode.Visible = true;
ProvinceCode.Visible = false;
ProvinceCode.Items.Clear();
//Province2Required.Enabled = false;
}
}
private ListItem FindSelectedProvince()
{
string defaultValue = ProvinceCode.Text;
if (string.IsNullOrEmpty(defaultValue)) defaultValue = Request.Form[ProvinceCode.UniqueID];
if (string.IsNullOrEmpty(defaultValue)) return null;
defaultValue = defaultValue.ToUpperInvariant();
foreach (ListItem item in ProvinceCode.Items)
{
string itemText = item.Text.ToUpperInvariant();
string itemValue = item.Value.ToUpperInvariant();
if (itemText == defaultValue || itemValue == defaultValue) return item;
}
return null;
}
protected void Country_Changed(object sender, EventArgs e)
{
//UPDATE THE FORM FOR THE NEW COUNTRY
UpdateCountry();
}
如果用户正在编辑现有条目,则当页面加载以使用数据库中的值填充字段时,会调用以下代码:
protected void Page_Load(object sender, EventArgs e)
{
_CustomerID = AlwaysConvert.ToInt(Request.QueryString["CustomerID"]);
int.TryParse(Request.QueryString["CustomerID"], out _CustomerID);
if (_CustomerID == 0)
{
AddBtn.Visible = true;
EditBtn.Visible = false;
}
else
{
custIDHidden.Value = _CustomerID.ToString();
AddBtn.Visible = false;
EditBtn.Visible = true;
}
if (!Page.IsPostBack)
{
if (_CustomerID != 0)
{
string selectQuery = "BillToRegion, BillToCountry FROM Customers WHERE CustomerID = @CustomerID";
try
{
using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["AbleCommerce"].ToString()))
{
cn.Open();
SqlCommand cmd = new SqlCommand(selectQuery, cn);
cmd.Parameters.Add(new SqlParameter("@CustomerID", custIDHidden.Value));
using (IDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
ProvinceCode.SelectedValue = reader["BillToRegion"].ToString();
CountryCode.SelectedValue = reader["BillToCountry"].ToString();
}
}
cn.Close();
}
}
catch (Exception x)
{
Logger.Warn("Admin\\People\\Customers\\EditCustomer.aspx - Page_Load", x);
}
}
}
}
SqlCommand cmd=新的SqlCommand;在SaveCustomerInfo中?这里没有使用两个查询字符串中的任何一个。C&P错误?哦,对不起,我排除了检查需要哪个查询的位。有更多的信息从表单保存到数据库,但是有很多,我的意思是很多字段。为了简单起见,我从INSERT、UPDATE和SELECT方法中删除了对其他字段的引用,只保留了国家和地区的引用。在Page_Init上运行的代码是否可能只在初始加载时运行?通常,您会检查isPostBack,并且仅在这些情况下或在某些内容发生更改时重新绑定控件。美国条目被保存的事实表明,这不是问题所在,因此将其添加为注释而不是答案,但可能值得检查,因为每次发回时重新绑定控件通常会导致不稳定的结果。
protected void Page_Load(object sender, EventArgs e)
{
_CustomerID = AlwaysConvert.ToInt(Request.QueryString["CustomerID"]);
int.TryParse(Request.QueryString["CustomerID"], out _CustomerID);
if (_CustomerID == 0)
{
AddBtn.Visible = true;
EditBtn.Visible = false;
}
else
{
custIDHidden.Value = _CustomerID.ToString();
AddBtn.Visible = false;
EditBtn.Visible = true;
}
if (!Page.IsPostBack)
{
if (_CustomerID != 0)
{
string selectQuery = "BillToRegion, BillToCountry FROM Customers WHERE CustomerID = @CustomerID";
try
{
using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["AbleCommerce"].ToString()))
{
cn.Open();
SqlCommand cmd = new SqlCommand(selectQuery, cn);
cmd.Parameters.Add(new SqlParameter("@CustomerID", custIDHidden.Value));
using (IDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
ProvinceCode.SelectedValue = reader["BillToRegion"].ToString();
CountryCode.SelectedValue = reader["BillToCountry"].ToString();
}
}
cn.Close();
}
}
catch (Exception x)
{
Logger.Warn("Admin\\People\\Customers\\EditCustomer.aspx - Page_Load", x);
}
}
}
}