zoukankan      html  css  js  c++  java
  • ASP.NET动态生成控件

         前两天写了个模块,需要从数据库读出数据在根据数据动态生成WEB控件。因为以前没写过,所以遇到一个问题,调了很久才调通。原来动态生成控件每次刷新页面都得在Page_Load()方法里面重现(生成控件代码不能写在if(!PageIsPost){}里面),不然在其他地方拿不到生成控件的值。用FindControls方法取控件值会抛出控异常。

    下面是Page_Load()事件里面的部分代码:
     SqlConnection conn = new SqlConnection(txtSqlConn.Text);
                       SqlDataAdapter da 
    = new SqlDataAdapter("select top 1 * from (" + txtSelect.Text + ") t1", conn);
                       
    try
                       
    {
                           conn.Open();
                           DataSet ds 
    = new DataSet();
                           da.Fill(ds, 
    "InfoTable");
                           Table tb 
    = new Table();//创建一个表格
                           
                           tb.BorderWidth 
    = Unit.Parse("0");
                           tb.Width 
    = Unit.Parse("100%");
                           
    for (int i = 0; ds.Tables[0].Columns.Count > i; i++)
                           
    {
                               
    if (ds.Tables["InfoTable"].Columns[i].DataType.ToString() == "System.Int32")
                               
    {
                                   TableRow tr 
    = new TableRow();//创建一行
                                   TableCell cell1 = new TableCell();//创建单元格,也就是第一列
                                   Label lbl = new Label();
                                   lbl.ID 
    = "lblName" + (int.Parse(ViewState["columns"].ToString()) + 1);
                                   lbl.Text 
    = ds.Tables["InfoTable"].Columns[i].ColumnName;
                                   cell1.Controls.Add(lbl);
                                   tr.Cells.Add(cell1);
    //添加到行中
                                   TableCell cell2 = new TableCell();//创建第二列
                                   DropDownList List = new DropDownList();
                                   List.ID 
    = "List" + (int.Parse(ViewState["columns"].ToString()) + 1);
                                   
    //List.AutoPostBack = true;
                                   List.Items.Add("无    ");
                                   List.Items.Add(
    "求和");
                                   List.Items.Add(
    "最大值");
                                   List.Items.Add(
    "最小值");
                                   List.Items.Add(
    "平均数");
                                   cell2.Controls.Add(List);
                                   tr.Cells.Add(cell2);
                                   tb.Rows.Add(tr);
    //添加到表格中
                                   ViewState["columns"= int.Parse(ViewState["columns"].ToString()) + 1;
                               }

                           }

                           PlaceHolder1.Controls.Add(tb);
                       }

                       
    catch (Exception ex)
                       
    {
                           ErrorInfo.Text 
    = ex.ToString();
                       }

                       
    finally
                       
    {
                           conn.Close();
                       }

    下面是按钮事件里取控件值:
     string info = "";
                        
    for (int i = 0; i < int.Parse(ViewState["columns"].ToString()); i++)
                        
    {
                            Label lbl 
    = (Label)PlaceHolder1.FindControl("lblName" + (i + 1));
                            DropDownList list 
    = (DropDownList)PlaceHolder1.FindControl("List" + (i + 1));
                            info 
    += lbl.Text + "," + list.SelectedItem.Text + ";";
                        }
  • 相关阅读:
    idea快捷键
    idea抛异常方式
    scott登陆PLSQL时候出现insufficient privileges的解决方法
    Linux下磁盘实战操作命令
    Docker容器常用命令
    Docker启动守护式容器
    Docker命令总结
    Docker镜像常用命令
    Centos7系统Docker安装
    Docker组成三要素
  • 原文地址:https://www.cnblogs.com/Mercury/p/674670.html
Copyright © 2011-2022 走看看