テーブルを表示する Web ページを作成しました。テーブルのほとんどのセルは既に入力されていますが、ユーザー入力が必要なセルもあります。これらのテーブル セルの一部にドロップ ダウン メニューを配置して、セルにメニューからの選択が取り込まれ、ドロップ ダウン リストが消えるようにしたいと思います。
これを単一のセルで動作させることができました。しかし、2 番目のセルで動作させようとすると、すべてが壊れてしまいました。私は問題を見つけようとしてハッキングしすぎたので、最初の問題をどのように機能させたかを覚えているかどうかさえわかりません。:-)
関連するコードは次のとおりです。
protected void Page_Load(object sender, EventArgs e)
{
// Create a DropDownList control.
DropDownList DropList = new DropDownList();
// Set the properties for the DropDownList control.
DropList.ID = "TrendList";
DropList.AutoPostBack = true;
// Manually register the event-handling method for the
// SelectedIndexChanged event.
DropList.SelectedIndexChanged += new EventHandler(this.Selection_Change);
// Because the DropDownList control is created dynamically each
// time the page is loaded, the data must be bound to the
// control each time the page is refreshed.
// Specify the data source and field names for the Text and
// Value properties of the items (ListItem objects) in the
// DropDownList control.
DropList.DataSource = CreateDataSource();
DropList.DataTextField = "ColorTextField";
DropList.DataValueField = "ColorValueField";
// Bind the data to the control.
DropList.DataBind();
// Set the default selected item when the page is first loaded.
if (!IsPostBack)
{
DropList.SelectedIndex = 0;
}
// Add the DropDownList control to the Controls collection of
// the PlaceHolder control.
p1.Controls.Add(DropList);
p2.Controls.Add(DropList);
}
ICollection CreateDataSource()
{
// Create a table to store data for the DropDownList control.
DataTable dt = new DataTable();
// Define the columns of the table.
dt.Columns.Add(new DataColumn("ColorTextField", typeof(String)));
dt.Columns.Add(new DataColumn("ColorValueField", typeof(String)));
// Populate the table with sample values.
dt.Rows.Add(CreateRow("GreenUp", "GreenUp", dt));
dt.Rows.Add(CreateRow("GreenFlat", "GreenFlat", dt));
dt.Rows.Add(CreateRow("GreenDown", "GreenDown", dt));
dt.Rows.Add(CreateRow("YellowUp", "YellowUp", dt));
dt.Rows.Add(CreateRow("YellowFlat", "YellowFlat", dt));
dt.Rows.Add(CreateRow("YellowDown", "YellowDown", dt));
dt.Rows.Add(CreateRow("RedUp", "RedUp", dt));
dt.Rows.Add(CreateRow("RedFlat", "RedFlat", dt));
dt.Rows.Add(CreateRow("RedDown", "RedDown", dt));
dt.Rows.Add(CreateRow("ClearUp", "ClearUp", dt));
dt.Rows.Add(CreateRow("ClearFlat", "ClearFlat", dt));
dt.Rows.Add(CreateRow("ClearDown", "ClearDown", dt));
// Create a DataView from the DataTable to act as the data source
// for the DropDownList control.
DataView dv = new DataView(dt);
return dv;
}
DataRow CreateRow(String Text, String Value, DataTable dt)
{
// Create a DataRow using the DataTable defined in the
// CreateDataSource method.
DataRow dr = dt.NewRow();
// This DataRow contains the ColorTextField and ColorValueField
// fields, as defined in the CreateDataSource method. Set the
// fields with the appropriate value. Remember that column 0
// is defined as ColorTextField, and column 1 is defined as
// ColorValueField.
dr[0] = Text;
dr[1] = Value;
return dr;
}
void Selection_Change(Object sender, EventArgs e)
{
// Retrieve the DropDownList control from the Controls
// collection of the PlaceHolder control.
DropDownList DropList1 = (DropDownList)p1.FindControl("TrendList");
DropDownList DropList2 = (DropDownList)p2.FindControl("TrendList");
switch (sender.ToString())
{
case "p1":
s1.InnerHtml = DropList1.SelectedItem.Value;
break;
case "p2":
s2.InnerHtml = DropList2.SelectedItem.Value;
break;
}
}
そして、ここに表からの関連するスニペットがあります:
<td><span id="s1" runat="server"><asp:PlaceHolder ID="p1" runat="server"></asp:PlaceHolder></span>
<td><span id="s2" runat="server"><asp:PlaceHolder ID="p2" runat="server"></asp:PlaceHolder></span>
送信者は発信者のIDを表していないため、スイッチ制御がすべて間違っていることがわかりました。しかし、どのドロップダウンメニューが呼び出し元であるかを区別する方法が必要なので、どの HTML を置き換えるかがわかります。また、一度に表示できるドロップダウン メニューは 1 つだけです。
アドバイスをいただければ幸いです。
よろしく。