0

我进行的方式如下:

表名:tbltt

1.我从源代码中添加了一个表格

2.cs页面创建行和列

3.Data会在运行时被绑定

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”的方法。那就是我想要获取它的值。请给我一个适当的方法来得到这个。任何帮助将不胜感激。谢谢。

4

1 回答 1

0

如果为每个控件(ID 属性)分配唯一的 ID,则可以使用 FindControl 来获取所需的内容:http: //msdn.microsoft.com/it-it/library/486wc64h.aspx

FindControl 按 ID 名称查找与其嵌套级别无关,因此:

<control id="name1">
  <control id="name1_1"></control>
  <control id="name1_2"></control>
  <control id="name1_3">
    <control id="name1_3_1">value</control>
  </control>
</control>

使用以下命令查找“name1_3_1”:

name1.FindControl("name1_3_1"); //Assuming they are all server-side controls, so runat="server" marked.

会给你“name1_3_1”,如果存在,否则为空。

于 2012-11-11T01:04:34.453 回答