私が進めた方法は次のとおりです。
テーブル名:tbltt
1.ソースコードからテーブルを追加しました
2.csページに行と列を作成しました
3.データは実行時にバインドされます
4.テーブル値を保存したいので、その中にコントロールを見つけたい(各行と列にドロップダウンがあります)
以下の表
public void createdynamictable()
{
string[] p = { "flag", "campusid", "ttid" };
string[] v = { "3", campusID.ToString(), ddlTimetable.SelectedValue };
DataTable dtt2 = bl.SelectDataTableUsingSp("SP_timetablefinal", p, v);
if (dtt2.Rows.Count > 0)
{
DataTable dtTimeTable = new DataTable();
dTable1 = new DataTable();
ArrayList freeHours = new ArrayList();
ArrayList PeriodCount = new ArrayList();
ArrayList PeriodCount1 = new ArrayList();
ArrayList PeriodDate = new ArrayList();
int period = 0;
period = dtt2.Rows.Count;
HtmlTableRow tableRowhead = new HtmlTableRow();
HtmlTableCell tableCellhead = new HtmlTableCell();
// tableCellhead.ColSpan = period + 2;
Label lblHead = new Label();
lblHead.ForeColor = System.Drawing.Color.Red;
tableCellhead.Controls.Add(lblHead);
tableRowhead.Controls.Add(tableCellhead);
tbltt.Controls.Add(tableRowhead);
HtmlTableRow tableRow = new HtmlTableRow();
tableRow.BgColor = "#0272A8";
tableRow.Height = "32";
HtmlTableCell tableCell1 = new HtmlTableCell();
tableCell1.Width = "75px";
Label lbl = new Label();
lbl.Text = " Date";
lbl.Font.Size = 12;
lbl.Font.Bold = true;
lbl.ForeColor = System.Drawing.Color.White;
tableCell1.Controls.Add(lbl);
tableRow.Controls.Add(tableCell1);
dtTimeTable.Columns.Add();
dtTimeTable.Rows.Add();
for (int k = 0; k <= dtt2.Rows.Count; k++)
{
HtmlTableCell tableCell = new HtmlTableCell();
Label lblPeriodHead = new Label();
if (k < dtt2.Rows.Count)
lblPeriodHead.Text = dtt2.Rows[k][0].ToString();
lblPeriodHead.Font.Size = 12;
tableCell.Width = "85px";
lblPeriodHead.ForeColor = System.Drawing.Color.White;
lblPeriodHead.Font.Bold = true;
tableCell.Controls.Add(lblPeriodHead);
tableRow.Controls.Add(tableCell);
dtTimeTable.Columns.Add();
dtTimeTable.Rows[0][k] = lblPeriodHead.Text;
}
//done
string[] p2 = { "flag", "campusid", "ttid" };
string[] v2 = { "2", campusID.ToString(), ddlTimetable.SelectedValue };
dt = bl.SelectDataTableUsingSp("SP_timetablefinal", p2, v2);
if (dt.Rows.Count > 0)
{
tbltt.Controls.Add(tableRow);
for (int m = 0; m < dt.Rows.Count; m++)
{
HtmlTableRow tableRowday = new HtmlTableRow();
HtmlTableCell tableCellday = new HtmlTableCell();
//----------------------------------
HtmlTableCell tableCelldate = new HtmlTableCell();
Label lbldate = new Label();
lbldate.Text = dt.Rows[m][0].ToString();
lbldate.Font.Size = 9;
lbldate.Height = Unit.Pixel(20);
tableCelldate.Width = "85px";
lbldate.Height = Unit.Pixel(20);
tableCelldate.Controls.Add(lbldate);
dtTimeTable.Rows.Add();
dtTimeTable.Rows[m + 1][0] = lbldate.Text;
tableCelldate.BorderColor = "Grey";
tableRowday.Controls.Add(tableCelldate);
int pds = 0;
for (int j = 1; j <= period; j++)
{
HtmlTableCell tableCells = new HtmlTableCell();
string datet = lbldate.Text;
string[] p22 = { "flag", "ttid", "campusid" };
string[] v22 = { "3", ddlTimetable.SelectedValue, campusID.ToString() };
DataTable dtss = bl.SelectDataTableUsingSp("SP_timetablefinal", p22, v22);
if (dtss.Rows.Count > 0)
{
DataTable ddt = new DataTable();
string[] pp = { "flag", "classid", "campusid" };
string[] vv = { "4", ddlclass.SelectedValue, campusID.ToString() };
ddt = bl.SelectDataTableUsingSp("SP_timetablefinal", pp, vv);
int count = 0;
if (ddt.Rows.Count > 0)//fill subjects
{
pds = pds + 1;
DateTime dat = Convert.ToDateTime(bl.SplitDate(datet));
DropDownList ddlsubjects = new DropDownList();
//ddlsubjects.CssClass = "dropdownMedium";
//ddlsubjects.Attributes.Add("style", "font-style:italic;color: #FF3399; font-size: 10px; border: thin groove #000000;width:300px");
//ddlsubjects.Style = "color: #FF3399; font-size: 10px; border: thin groove #000000 ";
tableCells.BorderColor = "Grey";
//lbtnPeriodHead1.Text = "";
ddlsubjects.Font.Size = 8;
ddlsubjects.ForeColor = System.Drawing.Color.SlateGray;
ddlsubjects.Height = Unit.Pixel(20);
tableCells.Controls.Add(ddlsubjects);
tableRowday.Controls.Add(tableCells);
// DropDownList ddlsubjects = (DropDownList)childItem.FindControl("ddlsubjects");
if (count < ddt.Rows.Count)
{
string[] p5 = { "flag", "classid", "pdsno", "date", "campusid" };
string[] v5 = { "1", ddlclass.SelectedValue, pds.ToString(), dat.ToString(), campusID.ToString() };
DataTable dtb = bl.SelectDataTableUsingSp("[SP_timetablefinalsub]", p5, v5);
if (dtb.Rows.Count > 0)
{
ddlsubjects.DataValueField = "SubjectClassid";
ddlsubjects.DataTextField = "papername";
ddlsubjects.DataSource = dtb;
ddlsubjects.DataBind();
ddlsubjects.Items.Insert(0, new ListItem("Select One", "0"));
count++;
//fill datas in DB
DateTime datt = dat;
string[] p3 = { "flag", "ttid", "date", "campusid", "pdnum" };
string[] v3 = { "6", ddlTimetable.SelectedValue, datt.ToString(), campusID.ToString(), pds.ToString() };
DataTable d3 = bl.SelectDataTableUsingSp("SP_timetablefinal", p3, v3);
if (d3.Rows.Count > 0)
{
if (d3.Rows[0][0].ToString()!= "0")
{
ddlsubjects.SelectedValue = d3.Rows[0][0].ToString();
}
}
//same group in any other class
string[] p1 = { "flag", "classid", "fromdate", "todate" };
string[] v1 = { "10", ddlclass.SelectedValue, bl.SplitDate(txtfrmdate.Text), bl.SplitDate(txttodate.Text) };
DataTable dti = bl.SelectDataTableUsingSp("SP_timetablefinal", p1, v1);
if (dti.Rows.Count > 0)
{
for (int s = 0; s < dti.Rows.Count; s++)
{
if (Convert.ToDateTime(dti.Rows[s]["date"]) == dat)
{
if (Convert.ToInt16(dti.Rows[s]["periodnum"]) == pds)
{
int sss = Convert.ToInt16(dti.Rows[s]["opgroupid"]);
ddlsubjects.SelectedValue = sss.ToString();
ddlsubjects.Enabled = false;
}
}
}
}
}
}
}
}
}
tbltt.Controls.Add(tableRowday);
}
}
}
}
コントロール「ddlsubjects」を動的に取得するメソッドが必要です。つまり、その値を取得したいのです。これを取得するための適切な方法を教えてください。助けていただければ幸いです。ありがとう。