web开发,自动给页面控件复值

这个是本人在做b/s开发的时候用的一段代码,使用的时候需要控件的id和数据库的列字段值对应,希望能够对需要的人有点帮助欢迎大家拍砖。
/// <summary>
        
/// 给页面数据复值
        
/// </summary>
        
/// <param name="PageContral">要复值的页面</param>
        
/// <param name="table">数据源DataTable</param>
        public static void SetDataToForm(System.Web.UI.Page PageContral,DataTable table)
        {
            
int ColCount=table.Columns.Count;
            
object ColText;
            
foreach(Control ct in PageContral.Controls)
            {
                
if(ct.HasControls())
                {
                    SetDataToForm(PageContral,table,ct.Controls,ColCount);
                }
                
else
                {
                    
if(ct.ID!=null)
                    {
                        
for(int i=0;i<ColCount;i++)
                        {
                            
if(ct.ID==table.Columns[i].ColumnName)
                            {
                                ColText
=table.Rows[0][i];
                                
if(ct is jw.Calendar)
                                {
                                    
if(Microsoft.VisualBasic.Information.IsDate(ColText))
                                    {
                                        DateTime Time
=Convert.ToDateTime(ColText);
                                        
if(Time.Year<=1900)
                                            ((Label)ct).Text
="";
                                        
else
                                            ((Label)ct).Text
=Time.ToShortDateString();
                                    }
                                    
else
                                        ((Label)ct).Text
=ColText.ToString();
                                }
                                
else if(ct is TextBox)
                                {
                                    ((TextBox)ct).Text
=ColText.ToString();
                                }
                                
else if(ct is DropDownList)
                                {
                                    ((DropDownList)ct).SelectedValue
=ColText.ToString();
                                }
                                
else if(ct is Label)
                                {
                                    
if(Microsoft.VisualBasic.Information.IsDate(ColText))
                                    {
                                        DateTime Time
=Convert.ToDateTime(ColText);
                                        
if(Time.Year<=1900)
                                            ((Label)ct).Text
="";
                                        
else
                                            ((Label)ct).Text
=Time.ToShortDateString();
                                    }
                                    
else
                                        ((Label)ct).Text
=ColText.ToString();
                                }
                                
else if(ct is CheckBox)
                                {
                                    ((CheckBox)ct).Checked
=(bool)ColText;
                                }
                            }
                        }
                    }
                }
            }
        }
        
private static void SetDataToForm(System.Web.UI.Page PageContral,DataTable table,ControlCollection ct,int ColCount)
        {
            
object ColText;
            
foreach(Control _ct in ct)
            {
                
if(_ct.HasControls())
                {
                    SetDataToForm(PageContral,table,_ct.Controls,ColCount);
                }
                
else
                {
                    
if(_ct.ID!=null)
                    {
                        
for(int i=0;i<ColCount;i++)
                        {
                            
if(_ct.ID==table.Columns[i].ColumnName)
                            {
                                ColText
=table.Rows[0][i];
                                
if(_ct is jw.Calendar)
                                {
                                    
if(Microsoft.VisualBasic.Information.IsDate(ColText))
                                    {
                                        DateTime Time
=Convert.ToDateTime(ColText);
                                        
if(Time.Year<=1900)
                                            ((TextBox)_ct).Text
="";
                                        
else
                                            ((TextBox)_ct).Text
=Time.ToShortDateString();
                                    }
                                    
else
                                        ((TextBox)_ct).Text
=ColText.ToString();
                                }
                                
else if(_ct is TextBox)
                                {
                                    ((TextBox)_ct).Text
=ColText.ToString();
                                }
                                
else if(_ct is DropDownList)
                                {
                                    ((DropDownList)_ct).SelectedValue
=ColText.ToString();
                                }
                                
else if(_ct is Label)
                                {
                                    
if(Microsoft.VisualBasic.Information.IsDate(ColText))
                                    {
                                        DateTime Time
=Convert.ToDateTime(ColText);
                                        
if(Time.Year<=1900)
                                            ((Label)_ct).Text
="";
                                        
else
                                            ((Label)_ct).Text
=Time.ToShortDateString();
                                    }
                                    
else
                                        ((Label)_ct).Text
=ColText.ToString();
                                }
                                
else if(_ct is CheckBox)
                                {
                                    ((CheckBox)_ct).Checked
=(bool)ColText;
                                }
                            }
                        }
                    }
                }
            }
        }
        
#endregion
posted @ 2008-05-25 16:36  wangwang0522  阅读(1947)  评论(17)    收藏  举报