autopostback = trueの動的ドロップダウンリストを含む1つの列を持つaspテーブルを含む編集ページがあり、データソースはデータベースから入力されたデータテーブルです。それぞれの列のデータを追加および削除するための動的な画像ボタンがあるが、ドロップダウンリストの列とはまったく関係がないか、影響を受けない別の2つの列があります。
そのテーブルに最初にデータを入力する前にポストバックを発生させずにドロップダウンリストの選択を変更すると、動的に作成されたイメージボタンの1つのクリックイベントハンドラーが発生し、ドロップダウンリストの選択値は変更されません。ただし、その後は正常に動作するか、ドロップダウンリストの選択を変更する前にこれらの画像ボタンをクリックすると正常に動作します。
これが起こっている理由は何ですか?
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);