我有一个带有asp表的编辑页面,其中一列包含动态下拉列表,其中autopostback = true,数据源是从数据库填充的数据表。还有另外两列具有动态图像按钮,用于在各自的列中添加和删除数据,但与下拉列表中的列完全没有关联或影响。
如果我在最初填充该表之前更改了下拉列表中的选择而没有引起任何回发,则会触发其中一个动态创建的图像按钮的click事件处理程序,并且下拉列表选择的值不会更改。然而,之后它表现正常或者如果我在更改下拉列表中的选择之前单击这些图像按钮,它的行为正常。
为什么会发生这种情况的原因?
protected void Page_Load(object sender,EventArgs e) {
if (pnl_Edit.Visible)
{
Client ClientToEdit = ClientController.ViewClient((int)Session["SelectedClient"]);
PopulateEditClientPanel(ClientToEdit);
//Combo boxes need to be databinded on every postback
BindOperatingNameComboBox(rcb_OperatingName);
BindParentCompanyComboBox(rcb_ParentCompany);
}
}
protected void btn_Edit_Command(object sender,CommandEventArgs e) { Client ClientToEdit = ClientController.ViewClient(int.Parse(e.CommandArgument.ToString()));
//Populate Client fields
txt_ClientName.Text = ClientToEdit.ClientName;
rcb_OperatingName.Text = ClientToEdit.OperatingName;
rcb_ParentCompany.Text = ClientToEdit.ParentCompany;
txt_Address1.Text = ClientToEdit.Address1;
txt_Address2.Text = ClientToEdit.Address2;
txt_Country.Text = ClientToEdit.Country;
txt_Region.Text = ClientToEdit.Region;
txt_City.Text = ClientToEdit.City;
txt_PostalCode.Text = ClientToEdit.PostalCode;
PopulateEditClientPanel(ClientToEdit);
btn_SaveChanges.CommandArgument = e.CommandArgument.ToString();
}
protected void PopulateEditClientPanel(Client ClientToEdit)
{
//Populate the Services table
TableHeaderRow HeaderRow = new TableHeaderRow();
HeaderRow.BackColor = System.Drawing.Color.Blue;
HeaderRow.ForeColor = System.Drawing.Color.White;
TableHeaderCell HeaderCell = new TableHeaderCell();
HeaderCell.Text = "Service";
HeaderRow.Cells.Add(HeaderCell);
HeaderCell = new TableHeaderCell();
HeaderCell.Text = "Status";
HeaderRow.Cells.Add(HeaderCell);
HeaderCell = new TableHeaderCell();
HeaderCell.Text = "Source(s)";
HeaderRow.Cells.Add(HeaderCell);
HeaderCell = new TableHeaderCell();
HeaderCell.Text = "Contact(s)";
HeaderRow.Cells.Add(HeaderCell);
tbl_EditServices.Rows.Add(HeaderRow);
TableRow Row;
TableCell Cell;
DataTable Statuses = PageController.GetStatuses();
DataTable Sources = PageController.GetSourceList("");
//Create a row for each service
foreach (Service CurrentService in ClientToEdit.Services)
{
Row = new TableRow();
Row.ID = CurrentService.ServiceDescription;
Cell = new TableCell(); //Service
Cell.ID = CurrentService.ServiceDescription + "Service";
Cell.Text = CurrentService.ServiceDescription;
Cell.VerticalAlign = VerticalAlign.Top;
Row.Cells.Add(Cell);
Cell = new TableCell(); //Status
Cell.ID = CurrentService.ServiceDescription + "Status";
DropDownList Status = new DropDownList();
Status.AutoPostBack = true;
Status.DataTextField = "StatusDescription";
Status.DataValueField = "StatusID";
Status.DataSource = Statuses;
Status.DataBind();
Cell.Controls.Add(Status);
//Select the current status that is currently known for this service
foreach (ListItem CurrentStatus in Status.Items)
{
if (CurrentStatus.Text == CurrentService.Status)
CurrentStatus.Selected = true;
else
CurrentStatus.Selected = false;
}
Cell.VerticalAlign = VerticalAlign.Top;
Row.Cells.Add(Cell);
Cell = new TableCell(); //Sources
Cell.ID = CurrentService.ServiceDescription + "Source";
//Create a button to add another source
ImageButton AddSourceButton = new ImageButton();
AddSourceButton.ID = CurrentService.ServiceDescription + "$Source";
AddSourceButton.ImageUrl = @"~/Images/add_icon.jpg";
AddSourceButton.Width = 20;
AddSourceButton.Height = 20;
Cell.Controls.Add(AddSourceButton);
Cell.VerticalAlign = VerticalAlign.Top;
for (int i = 0; i < CurrentService.Sources.Count; ++i)
{
Cell.Controls.Add(new LiteralControl("<br/>"));
//Create a remove button for each entry
ImageButton RemoveSourceButton = new ImageButton();
RemoveSourceButton.ImageUrl = @"~/Images/Delete.png";
RemoveSourceButton.Height = 20;
RemoveSourceButton.Width = 20;
RemoveSourceButton.CommandArgument = Session["SelectedClient"].ToString() + "," + CurrentService.ServiceDescription + "," + CurrentService.Sources[i];
RemoveSourceButton.Click += new ImageClickEventHandler(RemoveSourceButton_Click);
Cell.Controls.Add(RemoveSourceButton);
Cell.Controls.Add(new LiteralControl(" " + CurrentService.Sources[i]));
}
Row.Cells.Add(Cell);
Cell = new TableCell(); //Contact Name
Cell.ID = CurrentService.ServiceDescription + "Contact";
//Create a button to add another contact
ImageButton AddContactButton = new ImageButton();
AddContactButton.ID = CurrentService.ServiceDescription + "$Contact";
AddContactButton.ImageUrl = @"~/Images/add_icon.jpg";
AddContactButton.Width = 20;
AddContactButton.Height = 20;
Cell.Controls.Add(AddContactButton);
for (int i = 0; i < CurrentService.Contacts.Count; ++i)
{
Cell.Controls.Add(new LiteralControl("<br/>"));
//Create a remove button for each entry
ImageButton RemoveContactButton = new ImageButton();
RemoveContactButton.ImageUrl = @"~/Images/Delete.png";
RemoveContactButton.Height = 20;
RemoveContactButton.Width = 20;
RemoveContactButton.CommandArgument = Session["SelectedClient"].ToString() + "," + CurrentService.ServiceDescription + "," + CurrentService.Contacts[i].ContactID.ToString();
RemoveContactButton.Click += new ImageClickEventHandler(RemoveSourceButton_Click);
Cell.Controls.Add(RemoveContactButton);
//Create a link button for each contact which can be clicked to edit
Cell.Controls.Add(new LiteralControl(" "));
LinkButton Link = new LinkButton();
Link.ID = CurrentService.ServiceDescription + CurrentService.Contacts[i].ContactID.ToString();
Link.Click += new EventHandler(Link_Click);
Link.CommandArgument = CurrentService.Contacts[i].ContactID.ToString();
Link.Text = CurrentService.Contacts[i].ContactName;
Cell.Controls.Add(Link);
}
Cell.VerticalAlign = VerticalAlign.Top;
Row.Cells.Add(Cell);
tbl_EditServices.Rows.Add(Row);