动态生成dropdownlist

                    <td colspan="4" id="td_ddl" runat="server"> 
                    </td>
后台代码:

            #region 动态拼出未知个数的下拉框列表,每个分别绑定成组的三级指标
            if (dt != null && dt.Rows.Count > 0)
            {
                string tempIndi = "";
                int count = 0;
                //循环拼出由各条指标的评价标准组成的下拉框html,默认认为同一条二级指标下的三级指标绑定为一个下拉框,否则为不同的二级指标创建下一个下拉框
               for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (tempIndi != dt.Rows[i]["ST_LEVEL2_PLAN_TITLE"].ToString().Trim())
                    {
                        if (tempIndi.Length == 0)
                        {
                            td_ddl.InnerHtml = "<table  border=\"0\" cellpadding=\"0\" cellspacing=\"0\" style=\" width:100%;\"><tr><td>";
                        }
                        else
                        {
                            td_ddl.InnerHtml = "</asp:DropDownList>";
                        }
                        td_ddl.InnerHtml += "<asp:DropDownList runat=\"server\" ID=\"ddl" + count + dt.Rows[i]["IN_LEVEL3_PLAN_ID"].ToString().Trim() + "\" AutoPostBack=\"true\" >";
                        td_ddl.InnerHtml += "<asp:ListItem Value=\"" + dt.Rows[i]["IN_LEVEL3_PLAN_ID"].ToString().Trim() + "\">" + dt.Rows[i]["ST_LEVEL3_PLAN_TITLE"].ToString().Trim() + "</asp:ListItem>";
                        tempIndi = dt.Rows[i]["ST_LEVEL2_PLAN_TITLE"].ToString().Trim();
                        count++;
                    }
                    else
                    {
                        td_ddl.InnerHtml += "<asp:ListItem Value=\"" + dt.Rows[i]["DE_LEVEL3_PLAN_SCORE"].ToString().Trim() + "\">" + dt.Rows[i]["ST_LEVEL3_PLAN_TITLE"].ToString().Trim() + "</asp:ListItem>";
                    }
                }
                td_ddl.InnerHtml = "</asp:DropDownList>";
                hidDdlCount.Value = count.ToString();
            #endregion

            //循环查找出之前动态生成的dropdownlist
            for (int j = 0; j < int.Parse(hidDdlCount.Value); j++)
            {
                DropDownList ddl1 = (DropDownList)(td_ddl.FindControl("ddl" + j + dt2.Rows[0]["ST_DAILY_ID"].ToString()));
                if (ddl1 != null)
                {
                    ddl1.SelectedIndex = ddl1.Items.IndexOf(ddl1.Items.FindByValue(dt2.Rows[0]["ST_DAILY_ID"].ToString()));
                    break;
                }

                DropDownList ddl2 = (DropDownList)(td_ddl.FindControl("ddl" + j + dt2.Rows[0]["ST_WORDNUM_ID"].ToString()));
                if (ddl2 != null)
                {
                    ddl2.SelectedIndex = ddl2.Items.IndexOf(ddl2.Items.FindByValue(dt2.Rows[0]["ST_WORDNUM_ID"].ToString()));
                    break;
                }
            } 

        }

 

你可能感兴趣的:(list)