ページ ロードメソッドで、HTML テーブルにデータを入力しています。
デフォルトでは、テーブルは空です。
<table runat="server" id="categoriesTable">
</table>
各行を動的に作成した後、それに 4 つのセルを追加し、後で使用する ID を各セルに付与します。
すべてのセルを作成した後、このテーブルを再度トラバースして、各セル内にリストを追加できるようにし<li></li>
ます。これらのリスト内のデータは、データベースから取得されます。これは、以前に各セルに与えられた ID に依存します。
ページを更新しても問題はありませんが、PostBack
(ASP ボタンをクリックした後、または DropDownList の選択されたインデックスを変更した後)、セルの数は同じままですが、各セル内のリストは 2 倍になります。
つまり、これがあれば:
Cell1
-Da
-Do
-Di
-Du
Cell2
-Ya
-Yo
Cell3
-Ka
-Ki
PostBackの後にこれがあります:
Cell1
-Da
-Do
-Di
-Du
-Da
-Do
-Di
-Du
Cell2
-Ya
-Yo
-Ya
-Yo
Cell3
-Ka
-Ki
-Ka
-Ki
コードは次のとおりです。cCategoryは私が作成したクラスであり、そのメソッドはList<cCategory>
.
//Load Categories Into the Table
List<cCategory> mainCategoriesList = cCategory.SubCategories(null);
if(mainCategoriesList.Count!=0)
{
//Categories Available
HtmlTableRow Row = new HtmlTableRow();
categoriesTable.Rows.Add(Row);
HtmlTableCell Cell;
int cellCounter = 0;
while(cellCounter<mainCategoriesList.Count)
{
if (cellCounter % 4 == 0 && cellCounter!=0)
{
//Add a New Row
Row = new HtmlTableRow();
categoriesTable.Rows.Add(Row);
}
Cell = new HtmlTableCell();
Cell.InnerHtml = "<a href=\"Category.aspx?id=" + mainCategoriesList.ElementAt(cellCounter).CategoryID() + "\">" + mainCategoriesList.ElementAt(cellCounter).Category()+ "</a>";
Cell.ID = mainCategoriesList.ElementAt(cellCounter).CategoryID();
Row.Cells.Add(Cell);
cellCounter++;
}
//Now we must add the sub categories
String subList = "";
String newContents;
int counter;
List<cCategory> subCategoriesList;
for (int i = 0; i < categoriesTable.Rows.Count; i++)
{
//For each Row, go through each Cell
for (int j = 0; j < categoriesTable.Rows[i].Cells.Count; j++)
{
//For Each CELL, get the subCategories
subCategoriesList = cCategory.SubCategories(categoriesTable.Rows[i].Cells[j].ID);
counter = 0;
while (counter < subCategoriesList.Count)
{
subList = subList + "<li><a href=\"Category.aspx?id=" + subCategoriesList.ElementAt(counter).CategoryID() + "\">" + subCategoriesList.ElementAt(counter).Category() + "</a></li>";
counter++;
}
newContents = "<div class=\"subCategoriesList\"><ul>" + subList + "</ul></div>";
subList = "";
categoriesTable.Rows[i].Cells[j].InnerHtml = categoriesTable.Rows[i].Cells[j].InnerHtml + newContents;
}
}
}
セル データが 2 倍になるのはなぜですか?