惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
人人都是产品经理
人人都是产品经理
爱范儿
爱范儿
aimingoo的专栏
aimingoo的专栏
博客园 - 叶小钗
H
Help Net Security
Microsoft Security Blog
Microsoft Security Blog
The Cloudflare Blog
S
SegmentFault 最新的问题
小众软件
小众软件
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 司徒正美
The GitHub Blog
The GitHub Blog
量子位
H
Hackread – Cybersecurity News, Data Breaches, AI and More
V
V2EX
Martin Fowler
Martin Fowler
博客园 - 【当耐特】
J
Java Code Geeks
D
DataBreaches.Net
云风的 BLOG
云风的 BLOG
F
Fortinet All Blogs
Blog — PlanetScale
Blog — PlanetScale
Last Week in AI
Last Week in AI

博客园 - Niko

SQLServer使用注意规范 . windows2008 导出Excel 配置。 Web.config 数据库连接字符串加密 NHibernate.AdoNet.TooManyRowsAffectedException: Unexpected row count: 19; expected: 1 安装sql server 2008 注意事项 - Niko 添加 程序映射 链接服务器"(null)"的 OLE DB 访问接口 Microsoft Office Excel 不能访问文件 Win2008 检索 COM 类工厂中 CLSID 为 {000209FF-0000-0000-C000-000000000046} 的组件时失败解决方法 excel 汇入, SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问 - Niko 卸载 vs 2008 ASP.net 打印页面内容 - Niko - 博客园 asp.net 异步调用 MSSQL2005 建立分区 Asp.net2.0 defaultButton 不支持firefox解决方法 Rational Rose 安装破解, 和常见问题解决 Gridview 选中checkbox 时, 展开子节点, 同时选中父节点 - Niko Grid view 级联选择 - Niko Database diagram support objects cannot be installed because this database does not have a valid owner.
ASP.NET动态生成控件问题。 - Niko - 博客园
Niko · 2008-11-05 · via 博客园 - Niko

今天有用到动态生成控件:

  protected void Page_PreRender(object sender, EventArgs e)
        {
            BoundControl();
        }

 {
            List<OfferQuestion> offerQuestion = new List<OfferQuestion>();

            if (Request.QueryString["OfferId"] != null && Request.QueryString["UserId"] != null)
            {
                offerQuestion = BLLOffer.GetQuestionForInfluencer(Request.QueryString["OfferId"].ToString(), Request.QueryString["UserId"].ToString(), -1); ;
            }

            if (Request.QueryString["OfferId"] != null && Request.QueryString["LanguageId"] != null)
            {
                offerQuestion = BLLOffer.GetQuestionForInfluencer(Request.QueryString["OfferId"].ToString(), "", Convert.ToInt32(Request.QueryString["LanguageId"]));
            }

            if (offerQuestion != null)
            {
                //Create a table for question.
                Table table = new Table();
               
                table.CellPadding = 4;
                table.CellSpacing = 0;
                table.Width = Unit.Percentage(98);
                table.Attributes.Add("align", "center");

                for (int i = 0; i < offerQuestion.Count; i++)
                {
                    //Create 2 rows, one is the title, another is the context
                    TableRow tableRow1=new TableRow();
                    TableRow tableRow2 = new TableRow();

                    //Every row will has two column.
                    TableCell tableCell=new TableCell();
                    TableCell tableCell2 = new TableCell();
                    tableCell.VerticalAlign=VerticalAlign.Top;
                    tableCell.Width = 250;

                    //Create the label for title.
                    Label lb = new Label();
                    lb.ID = "_label" + offerQuestion[i].Question;
                    lb.Text = offerQuestion[i].Question + ":";
                    tableCell.Controls.Add(lb);

                    if (offerQuestion[i].Required)
                    {
                        Label lbValidation = new Label();
                        lbValidation.ID = "_validation" + i.ToString();
                        lbValidation.Text = "*";
                        lbValidation.ForeColor = System.Drawing.Color.Red;
                        tableCell.Controls.Add(lbValidation);
                    }

                    if (offerQuestion[i].QuestionType.Trim() == "TextBox")
                    {
                        TextBox tb = new TextBox();
                        tb.ID = "_" + offerQuestion[i].Question;
                        tb.Width = 150;
                        tb.Text = offerQuestion[i].PreDefinedAnswers;
                        //tb.Enabled = false;
                        tableCell2.Controls.Add(tb);
                    }
                    else
                    {
                        DropDownList ddl = new DropDownList();
                        ddl.ID = "_" + offerQuestion[i].Question;
                        ddl.Width = 150;

                        if (offerQuestion[i].Question == "Country")
                        {
                            List<Country> Country = BLLMetadata.GetCountiesBySubRegion(null);       //Bind drlCountry for country
                            ddl.DataSource = Country;
                            ddl.DataTextField = "CountryName";
                            ddl.DataValueField = "CountryID";
                            ddl.DataBind();
                            ddl.SelectedIndex = 0;
                            ddl.Items.Insert(0, new ListItem("<Select>", "-1"));
                            //ddl.Enabled = false;
                            tableCell2.Attributes.Add("style", "padding-left:18px");
                        }
                       
                        tableCell2.Controls.Add(ddl);
                    }

                    tableRow1.Cells.Add(tableCell);
                    tableRow2.Cells.Add(tableCell2);

                    tableCell = new TableCell();
                    tableCell2 = new TableCell();

                    if (i < offerQuestion.Count - 1)
                    {
                        tableCell = new TableCell();
                        lb = new Label();
                        lb.ID = "_label" + offerQuestion[i++].Question;
                        lb.Text = offerQuestion[i].Question + ":";
                        tableCell.Width = 200;
                        tableCell.Controls.Add(lb);

                        if (offerQuestion[i].Required)
                        {
                            Label lbValidation = new Label();
                            lbValidation.ID = "_validation" + i.ToString();
                            lbValidation.Text = "*";
                            lbValidation.ForeColor = System.Drawing.Color.Red;
                            tableCell.Controls.Add(lbValidation);
                        }
                     
                        if (offerQuestion[i].QuestionType.Trim() == "TextBox")
                        {
                            TextBox tb = new TextBox();
                            tb.ID = "_" + offerQuestion[i].Question;
                            tb.Width = 150;
                            tb.Text = offerQuestion[i].PreDefinedAnswers;
                            //tb.Enabled = false;
                            tableCell2.Controls.Add(tb);
                        }
                        else
                        {
                            DropDownList ddl = new DropDownList();
                            ddl.ID = "_" + offerQuestion[i].Question;
                            ddl.Width = 150;                          

                            if (offerQuestion[i].Question == "Country")
                            {
                                List<Country> Country = BLLMetadata.GetCountiesBySubRegion(null);       //Bind drlCountry for country
                                ddl.DataSource = Country;
                                ddl.DataTextField = "CountryName";
                                ddl.DataValueField = "CountryID";
                                ddl.DataBind();
                                ddl.Items.Insert(0, new ListItem("<Select>", "-1"));
                                ddl.SelectedIndex = 0;
                                tableCell2.Attributes.Add("style", "padding-left:18px");
                            }
                                                      
                            tableCell2.Controls.Add(ddl);
                        }

                        tableRow1.Cells.Add(tableCell);
                        tableRow2.Cells.Add(tableCell2);
                    }

                    table.Rows.Add(tableRow1);
                    table.Rows.Add(tableRow2);
                }

                this.PlaceHolder1.Controls.Add(table);
            }
        }