动态生成表格

是以前完成一段程序,当初想了很久参考了老师的代码,完成了自己要求的功能.主要是代码片段

 List<TextBox> textlist = new List<TextBox>();     //...

  buildtableshow(TableShow, 100000);
  buildIlist(TableShow);

ContractedBlock.gif ExpandedBlockStart.gif Code
protected bool setPageinfoToClass()
    {
        
try
        {
            
return pkhead.p_storePageinfoToClass(hfpkhead_id.Value,kpDDLEditTeacher.seleValue,kpDDLEditLesson.seleValue,txtEditMembers.Text
                ,txtEditStartweek.Text,txtEditEndweek.Text,txtEditPrelection.Text
                ,kpDDLEditWeektype.seleValue,txtEditWeekhour.Text,kpDDLEditClaroomtye.seleValue
                ,kpDDLOtherSucc.seleValue,kpDDLGroupcode.seleValue,txtEditGroupwith.Text,txtEditTeacherlist.Text
                ,textlist[
0].Text,textlist[1].Text,textlist[2].Text,textlist[3].Text,textlist[4].Text
                ,textlist[
5].Text,textlist[6].Text,textlist[7].Text,textlist[8].Text,textlist[9].Text
                ,textlist[
10].Text,textlist[11].Text,textlist[12].Text,textlist[13].Text,textlist[14].Text
                ,textlist[
15].Text,textlist[16].Text,textlist[17].Text,textlist[18].Text,textlist[19].Text
                ,textlist[
20].Text,textlist[21].Text,textlist[22].Text,textlist[23].Text,textlist[24].Text
                ,textlist[
25].Text,textlist[26].Text,textlist[27].Text,textlist[28].Text,textlist[29].Text
                ,textlist[
30].Text,textlist[31].Text,textlist[32].Text,textlist[33].Text,textlist[34].Text
                ,kpDDLDeptEdit.seleValue);

        }
        
catch (Exception ex) { labelErrinfo.Text = "所提交的数据无法保存。请检查数据格式" + ex.Message; return false; }
    }

 

ContractedBlock.gif ExpandedBlockStart.gif Code
protected void buildtableshow(Table table, int id)
    {
        dbb 
= new DBBase();
        
string sql = string.Format("select * from pkhead where pkhead_id={0}", id);
        DataSet ds 
= dbb.readData(sql);
        TableRow tr 
= new TableRow();
        
foreach (DataRow dr in ds.Tables[0].Rows)
        {
            table.Rows.Clear();
            tr.Cells.Add(newcell(
"时  间"));
            tr.Cells.Add(newcell(
"星期一"));
            tr.Cells.Add(newcell(
"星期二"));
            tr.Cells.Add(newcell(
"星期三"));
            tr.Cells.Add(newcell(
"星期四"));
            tr.Cells.Add(newcell(
"星期五"));
            tr.Cells.Add(newcell(
"星期六"));
            tr.Cells.Add(newcell(
"星期天"));
            table.Rows.Add(tr);
            tr 
= new TableRow();
            
int ss = 1;
            
for (int i = 1; i <= 5; i++)
            {
                tr.Cells.Add(newcell(
string.Format("{0}、{1}节",ss,ss+1)));
                ss
=ss+2;
                
for (int j = 0; j <= 30; j = j + 5)
                {
                    
string name = "t" + pubfunc.NumToStrPrefix0(i + j, 2);
                    
string content = pubfunc.ObjectToString(dr[name]);
                    tr.Cells.Add(newcell(CreateTextbox(name,content)));
                }
                tr.EnableViewState 
= true;
                table.Rows.Add(tr);
                tr 
= new TableRow();
            }
            tr.EnableViewState 
= true;
            table.Rows.Add(tr);
            table.EnableViewState 
= true;
        }
        table.Visible 
= true;
        table.BackColor 
= System.Drawing.Color.AntiqueWhite;
        
if (ds != null) ds.Dispose();
    }
    
protected void buildtableshow(Table table)
    {
        TableRow tr 
= new TableRow();
        table.Rows.Clear();
        tr.Cells.Add(newcell(
"时  间"));
        tr.Cells.Add(newcell(
"星期一"));
        tr.Cells.Add(newcell(
"星期二"));
        tr.Cells.Add(newcell(
"星期三"));
        tr.Cells.Add(newcell(
"星期四"));
        tr.Cells.Add(newcell(
"星期五"));
        tr.Cells.Add(newcell(
"星期六"));
        tr.Cells.Add(newcell(
"星期天"));
        table.Rows.Add(tr);
        tr 
= new TableRow();
        
int ss = 1;
        
for (int i = 1; i <= 5; i++)
        {
            tr.Cells.Add(newcell(
string.Format("{0}、{1}节", ss, ss + 1)));
            ss 
= ss + 2;
            
for (int j = 0; j <= 30; j = j + 5)
            {
                
string name = "t" + pubfunc.NumToStrPrefix0(i + j, 2);
                tr.Cells.Add(newcell(CreateTextbox(name,
"")));
            }
            tr.EnableViewState 
= true;
            table.Rows.Add(tr);
            tr 
= new TableRow();
        }
        tr.EnableViewState 
= true;
        table.Rows.Add(tr);
        table.EnableViewState 
= true;
        table.Visible 
= true;
        table.BackColor 
= System.Drawing.Color.AntiqueWhite;
 
    }
    
private TableCell newcell(TextBox tb)
    {
        TableCell tc 
= new TableCell();
        tc.Controls.Add(tb);
        tc.Font.Size 
= FontUnit.Point(10);
        tc.VerticalAlign 
= VerticalAlign.Middle;
        
return tc;
    }
    
private TableCell newcell(string cellinfo)
    {
        TableCell tc 
= new TableCell();
        
//tc.BackColor = System.Drawing.Color.White;
        tc.Text = cellinfo;
        tc.VerticalAlign 
= VerticalAlign.Middle;
        
return tc;
    }
    
private TextBox CreateTextbox(string ContainerID,string text)
    {
        
string texboxid = string.Format("txt{0}", ContainerID);
        TextBox tb 
= new TextBox();
        tb.ID 
= texboxid;
        tb.Text 
= text;
        tb.Width 
= 50;
        tb.Height 
= 20;
        
return tb;
    }
    
private void buildIlist(Table table)
    {
        textlist.Clear();
        
for (int i = 1; i <= 35; i++)
        {
            
bool flag = false;
            
foreach (TableRow tr in table.Rows)
            {
                
foreach (TableCell tc in tr.Cells)
                {
                    
string id = "txtt" + pubfunc.NumToStrPrefix0(i, 2);
                    TextBox tb 
= (TextBox)tc.FindControl(id);
                    
if (tb != null)
                    {
                        textlist.Add(tb);
                        flag 
= true;
                    }
                    
if (flag)
                        
break;
                }
                
if (flag)
                    
break;
            }
        }
    }

转载于:https://www.cnblogs.com/wellma/archive/2009/04/02/1427711.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值