テーブルは、ドロップダウン リストの選択された値に基づくデータで構築されます。このテーブルには、各行のチェックボックスが含まれています (デフォルトでオンになっています)。ボタンのポストバックでは、チェックボックスがオンになっている行のみを取得したい。
初期ロード時およびボタンのポストバック後、これは意図したとおりに機能します。ただし、ドロップダウンリストの OnSelectedIndexChanged の後にボタンのポストバックを行うと、すべてのチェックボックスが再度チェックされます。
ページのライフサイクルを通じて機能させる必要があることはわかっていますが、適切に機能させることはできません。
私が使用するコードは次のとおりです: DynamicDropDownList.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DynamicDropDownList.aspx.cs" Inherits="LittleApplication.DynamicDropDownList" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList runat="server" ID="ddlControl" OnSelectedIndexChanged="DropDownChanged" AutoPostBack="True"></asp:DropDownList><br/><br/>
<asp:Table runat="server" ID="tblControl"></asp:Table><br/><br/>
<asp:Button runat="server" ID="btnControl" Text="Save"
onclick="btnControl_Click" /><br/><br/>
<asp:Label runat="server" ID="lblControl"></asp:Label>
</div>
</form>
</body>
</html>
DropDownList.aspx.cs
using System;
using System.Web.UI.WebControls;
namespace LittleApplication
{
public partial class DynamicDropDownList : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadDropDownList();
}
LoadTable();
}
private void LoadDropDownList()
{
ddlControl.Items.Add(new ListItem { Text = "First", Value = "1" });
ddlControl.Items.Add(new ListItem { Text = "Second", Value = "2" });
}
private void LoadTable()
{
tblControl.Rows.Clear();
if (ddlControl.SelectedValue == "1")
{
var firstCheck = new CheckBox {Checked = true};
var firstCellCheck = new TableCell();
firstCellCheck.Controls.Add(firstCheck);
var firstCellText = new TableCell();
firstCellText.Text = "First One";
var firstRow = new TableRow();
firstRow.Cells.Add(firstCellCheck);
firstRow.Cells.Add(firstCellText);
tblControl.Rows.Add(firstRow);
var secondCheck = new CheckBox {Checked = true};
var secondCellCheck = new TableCell();
secondCellCheck.Controls.Add(secondCheck);
var secondCellText = new TableCell();
secondCellText.Text = "First Two";
var secondRow = new TableRow();
secondRow.Cells.Add(secondCellCheck);
secondRow.Cells.Add(secondCellText);
tblControl.Rows.Add(secondRow);
}
else
{
var firstCheck = new CheckBox { Checked = true };
var firstCellCheck = new TableCell();
firstCellCheck.Controls.Add(firstCheck);
var firstCellText = new TableCell();
firstCellText.Text = "Second One";
var firstRow = new TableRow();
firstRow.Cells.Add(firstCellCheck);
firstRow.Cells.Add(firstCellText);
tblControl.Rows.Add(firstRow);
var secondCheck = new CheckBox { Checked = true };
var secondCellCheck = new TableCell();
secondCellCheck.Controls.Add(secondCheck);
var secondCellText = new TableCell();
secondCellText.Text = "Second Two";
var secondRow = new TableRow();
secondRow.Cells.Add(secondCellCheck);
secondRow.Cells.Add(secondCellText);
tblControl.Rows.Add(secondRow);
}
}
protected void DropDownChanged(object sender, EventArgs e)
{
lblControl.Text = String.Empty;
LoadTable();
}
protected void btnControl_Click(object sender, EventArgs e)
{
lblControl.Text = String.Empty;
foreach (TableRow row in tblControl.Rows)
{
if (((CheckBox)row.Cells[0].Controls[0]).Checked)
{
lblControl.Text += row.Cells[1].Text + "<br/>";
}
}
}
}
}
私が説明した動作を確認するには: ページ読み込み時に、[最初の 1 つ] のチェックを外し、[保存] をクリックします。「保存」ボタンの下に「最初の 2 つ」というテキストが表示されます (これは正しいです)。ドロップダウンリストの値を「Second. 「2 つ目」のチェックを外し、「保存」をクリックします。テキスト「Second One Second Two」が表示されますが、「Second Two」のみが予期されます。
アドバイスをありがとう。
編集 この例は、実際のアプリケーションで起こっていることの動作をシミュレートすることを追加する必要があります。また、実際の作業についてさらにフィードバックが必要になる場合があります。ドロップダウンリストには複数の値が含まれています。この選択に基づいて、異なるデータがデータベースから取得されます。このデータは、表示される列の数なども異なります。LoadTable は、この異なるデータをテーブルに動的に追加します。唯一の共通点は、チェックボックスの使用です。したがって、ソリューションでは、LoadDropDownList および LoadTable 関数への変更を最小限に抑える必要があります。