我有一个包含三个元素的ASP.NET页面,每个元素都在一个面板中:
Model #
,Manufacturer
和Description
Model #
& Description
是文本字段。 Manufacturer
从数据库中获取其值,并通过下拉列表显示它们
用户输入Model #
,选择Manufacturer
,然后输入Description
,通过按钮保存记录,并将新模型添加到数据中。
问题是,无论从列表中选择Manufacturer
,记录只会与出现的第一个制造商一起保存(因此,如果我选择ZZZManufacturer,它将保存Model #
和Description
但是使用AAAManufacturer - ddl按字母顺序排序。
我花了最后几个小时的时间在SO&其他地方似乎是答案,或者至少看起来高度相关 - 元素必须在AutoPostBack,ViewState和后面的代码中的!IsPostBack检查之间正确。
或许是什么使得这个与众不同的是,有一个数据访问层组件 - 可能有什么影响这个? 我刚刚继承了这个项目,我还在学习它 - 这是最后的错误之一。
DDL代码:
<asp:Panel ID="Panel1" runat="server" CssClass="form-field-container">
<asp:Label ID="Label19" runat="server" CssClass="form-label"
AssociatedControlID="ddlManufacturer">
Manufacturer
<asp:Label runat="server" CssClass="form-field-required-indicator" Text="*" />
</asp:Label>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
runat="server" ControlToValidate="ddlManufacturer" InitialValue="-1"
Display="Dynamic" Text="Required" ErrorMessage="Manufacturer is Required"
CssClass="form-validator">
</asp:RequiredFieldValidator>
<asp:DropDownList ID="ddlManufacturer" runat="server" CssClass="form-data" AutoPostBack="True">
<asp:ListItem Selected="True" Text="-- Select --" Value="-1"></asp:ListItem>
</asp:DropDownList>
</asp:Panel>
<asp:Panel ID="Panel2" runat="server" CssClass="form-field-container">
<asp:Label ID="Label6" runat="server" CssClass="form-label"
AssociatedControlID="txtDescription">
Description
</asp:Label>
<br />
<asp:TextBox ID="txtDescription" runat="server" CssClass="form-data" />
</asp:Panel>
背后的代码:
- Page_Load组件
我不确定为什么最后一位开发人员对此进行了注释:
protected void Page_Load(object sender, EventArgs e) {
Instantiate();
//if (!IsPostBack) { // initial load
//} else { // post back
//}
}
- 参考的完整页码
public partial class ManufacturerModelFormUserControl : System.Web.UI.UserControl {
public delegate void FormActionEventHandler(string action);
public event FormActionEventHandler ActionPerformed;
public event PageNotificationEventHandler PageNotification;
private GuiUtility _guiUtil;
private User _currentUser {
get {
object o = Session["CurrentUser"];
return (o == null) ? new User() : (User) Session["CurrentUser"];
}
set {
Session["CurrentUser"] = value;
}
}
private ManufacturerModel _manufacturerModel {
get {
object o = ViewState["ManufacturerModel"];
return (o == null) ? new ManufacturerModel() : (ManufacturerModel)
ViewState["ManufacturerModel"];
}
set {
ViewState["ManufacturerModel"] = value;
}
}
public void NewManufacturerModel() {}
public void LoadManufacturerModel(int id) {
if (id <= 0) {
return;
}
_manufacturerModel = ManufacturerModelAdapter.GetById(id);
if (_manufacturerModel.ManufacturerModelId > 0) {
this.Visible = true;
}
}
public bool Save() {
bool bIsSaved = false;
if (!ValidateForm()) {
return false;
}
PopulateObjectsFromForm();
_manufacturerModel = ManufacturerModelAdapter.Save(_manufacturerModel, _currentUser);
if (_manufacturerModel.ManufacturerModelId != 0) {
bIsSaved = true;
} else {
//problem occurred
RaisePageNotificationEvent(new PageNotification(PageNotificationType.Error, "Model Number could not be saved"));
return false;
}
if (bIsSaved) {
RaiseActionEvent("Save");
RaisePageNotificationEvent(new PageNotification(PageNotificationType.Generic, String.Format("Model Number Saved: {0}", _manufacturerModel.ModelNumber)));
}
ClearForm();
return bIsSaved;
}
public void Cancel() {
ClearForm();
RaiseActionEvent("Cancel");
RaisePageNotificationEvent(new PageNotification(PageNotificationType.Generic, "Action Cancelled"));
}
protected void Page_Load(object sender, EventArgs e) {
Instantiate();
// if (!IsPostBack) {
// initial load
// } else {
// post back
// }
}
private void Instantiate() {
_guiUtil = new GuiUtility();
PopulateListControls();
this.txtModelNumber.Focus();
}
private void PopulateListControls() {
Dictionary<int, string> manufactuerers = ManufacturerAdapter.GetAllDictionary();
ddlManufacturer.DataSource = manufactuerers;
ddlManufacturer.DataTextField = "Value";
ddlManufacturer.DataValueField = "Key";
ddlManufacturer.DataBind();
}
private bool ValidateForm() {
IValidator v;
bool bIsValid = true;
int vCount;
Page.Validate();
vCount = Page.Validators.Count;
for (int i = 0; i < vCount; i++) {
v = Page.Validators[i];
if (!v.IsValid) {
RaisePageNotificationEvent(new PageNotification(PageNotificationType.Validation, v.ErrorMessage));
bIsValid = false;
}
}
/* *
* Additional validation checks
* */
return bIsValid;
}
private void ClearForm() {
_manufacturerModel = new ManufacturerModel();
this.txtModelNumber.Text = "";
this.txtDescription.Text = "";
this.ddlManufacturer.SelectedIndex = -1;
}
private void PopulateForm(ManufacturerModel m) {
_guiUtil.ListControlSelectValueIfExists((ListControl) this.ddlManufacturer, m.Manufacturer.ManufacturerId.ToString());
this.txtModelNumber.Text = m.ModelNumber;
this.txtDescription.Text = m.Description;
}
private void PopulateObjectsFromForm() {
ManufacturerModel m = new ManufacturerModel();
m.ManufacturerModelId = _manufacturerModel.ManufacturerModelId;
m.Manufacturer = ManufacturerAdapter.GetById(Convert.ToInt32(this.ddlManufacturer.SelectedValue));
m.ModelNumber = this.txtModelNumber.Text;
m.Description = this.txtDescription.Text;
_manufacturerModel = m;
}
private void RaiseActionEvent(string action) {
if (ActionPerformed != null) {
ActionPerformed(action);
}
}
private void RaisePageNotificationEvent(PageNotification pageNotification) {
if (PageNotification != null) {
PageNotification(pageNotification);
}
}
protected void btnSave_Click(object sender, EventArgs e) {
Save();
}
protected void btnCancel_Click(object sender, EventArgs e) {
Cancel();
}
}
}
答案 0 :(得分:1)
每次回发都会填充DropDownList,因此您将松开所选值。将实例化移动到if(!Page.IsPostBack)块内:
if (!IsPostBack) {
//initial load
Instantiate();
}
答案 1 :(得分:0)
只是为将来的观众提供一些额外的资源,这里是已解决的更新代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Instantiate();
}
}