ASP.NET动态生成控件

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

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

ExpandedSubBlockEnd.gif                       }

InBlock.gif                       PlaceHolder1.Controls.Add(tb);
ExpandedBlockEnd.gif                   }

None.gif                   
catch  (Exception ex)
ExpandedBlockStart.gifContractedBlock.gif                   
dot.gif {
InBlock.gif                       ErrorInfo.Text 
= ex.ToString();
ExpandedBlockEnd.gif                   }

None.gif                   
finally
ExpandedBlockStart.gifContractedBlock.gif                   
dot.gif {
InBlock.gif                       conn.Close();
ExpandedBlockEnd.gif                   }

下面是按钮事件里取控件值:
None.gif   string  info  =   "" ;
None.gif                    
for  ( int  i  =   0 ; i  <   int .Parse(ViewState[ " columns " ].ToString()); i ++ )
ExpandedBlockStart.gifContractedBlock.gif                    
dot.gif {
InBlock.gif                        Label lbl 
= (Label)PlaceHolder1.FindControl("lblName" + (i + 1));
InBlock.gif                        DropDownList list 
= (DropDownList)PlaceHolder1.FindControl("List" + (i + 1));
InBlock.gif                        info 
+= lbl.Text + "," + list.SelectedItem.Text + ";";
ExpandedBlockEnd.gif                    }

转载于:https://www.cnblogs.com/Mercury/archive/2007/03/14/674670.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值