导航

动态生成页面返回数据的问题--十万火急!

Posted on 2006-05-16 16:03  山姆蜀黍  阅读(1447)  评论(4编辑  收藏  举报
  可能很初级的问题,把我难倒了,各位一定要帮我!dudu我把问题发到提问区没人理所以只好到首页放一放

  我作了一个层次结构的类,然后想用这个层次结构动态生成页面,页面是生成了,可作了一些操作以后点按钮却什么也没有了
  具体问题如下:

  首先,因为数据现在是不定的,所以我将数据封装到一个带有结构的类中:
    public class Privileges
    
{
        
public Dictionary<int,Panel> Panels;
    
private DataSet PriDs;
    
private Classid;
        
public Privileges(string obj, string id)
        
{
            init(obj,id);
        }

        
private void init(string obj,string id)
        
{
        
//dosome thing for init ,for example PriDs
        
            MakePanels();
        }


        
private void MakePanels()
        
{
            Panels 
= new Dictionary<int,Panel>();
            
string name, id;
            DataTable dt;
            
for (int i = 0; i < PriDs.Tables.Count; i++)
            
{
                dt 
= PriDs.Tables[i];
                name 
= dt.Rows[0]["className"].ToString();
                id 
= dt.Rows[0]["classID"].ToString();
                Panels.Add(i, 
new Panel(dt, name, id));
            }

        }

     
public class Panel
        
{
            
public string Name;
            
public string ID;
            
public Dictionary<int, Group> Groups;

            
public Panel(DataTable dt,string name,string id)
            
{
                
this.Name = name;
                
this.ID = id;
                MakeGroups(dt);
            }


            
private void MakeGroups(DataTable dt)
            
{
                Groups 
= new Dictionary<int,Group>();
                
string name, id, elementstruct, value, type,cnname;
                
for (int i = 0; i < dt.Rows.Count; i++)
                
{
                    name 
= dt.Rows[i]["name"].ToString();
                    cnname 
= dt.Rows[i]["cnname"].ToString();
                    id 
= dt.Rows[i]["privilegeid"].ToString();
                    elementstruct 
= dt.Rows[i]["code"].ToString();
                    type 
= dt.Rows[i]["controltype"].ToString();
                    value 
= dt.Rows[i]["value"].ToString();
                    Groups.Add(i, 
new Group(name,cnname, id, elementstruct, value, type));
                }

            }

        }

        
public class Group
        
{
            
public string Name;
            
public string CnName;
            
public string ID;
            
public string Value;
            
public string Type
            
{
                
get
                
{
                    
return type;
                }

                
set
                
{
                    
if (value != "DropDownList" && value != "CheckBoxList" && value != "RadioButtonList")
                    
{
                        type 
= "CheckBoxList";
                    }

                    
else
                    
{
                        type 
= value;
                    }

                }

            }

            
public Dictionary<int, Element> Elements;
            
private string type;
            
private string ElementStruct;

            
public Group(string name,string cnname,string id,string elementStruct,string value,string type)
            
{
                
this.Name = name;
                
this.CnName = cnname;
                
this.ID = id;
                
this.ElementStruct = elementStruct;
                
this.Value = value;
                
this.Type = type;
                Elements 
= new Dictionary<int,Element>();
                MakeLeafs();
                
            }


            
public void MakeLeafs()
            
{
                
string[] structure = ElementStruct.Split(';');
                ListDictionary valueList
=new ListDictionary();
                
string[] valueStr = Value.Split(',');
                
for (int i = 0; i < valueStr.Length; i++)
                
{
                    valueList.Add(valueStr[i],i);
                }

                
for (int i=0; i < structure.Length; i++)
                
{
                    
string[] NameID = structure[i].Split(',');
                    
string leafName = NameID[0];
                    
string leafID = NameID[1];
                    
if(valueList[leafID]!=null)
                    
{
                        Elements.Add(i,
new Element(leafName,leafID,true));
                    }
else
                    
{
                        Elements.Add(i, 
new Element(leafName, leafID, false));
                    }

                }

            }

        }

        
public class Element
        
{
            
public string Name;
            
public string ID;
            
public bool State;
            
public Element(string name, string id, bool state)
            
{
                
this.Name = name;
                
this.ID = id;
                
this.State = state;
            }

        }

    }
    

然后,我将这个类传递给页面,页面通过遍历将其格式化显示出来

public partial class testOrg_testDB : System.Web.UI.Page
{
    DataTable dt 
= new DataTable();
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!IsPostBack)
        
{
            
this.PReport.Visible = false;
            initDDL();       
        }

    }


    
private void initDDL()
    
{
        
for (int i = 0; i < 4; i++)
        
{
            
this.DropDownList1.Items.Add(Privileges.objStruct[i]);
        }

    }

    
protected void Button1_Click(object sender, EventArgs e)
    
{
        
string id = this.TextBoxCode.Text;
        
string obj = this.DropDownList1.SelectedValue;
        ViewState[
"id"= id;
        ViewState[
"obj"= obj;
        
if (id == "")
        
{
            
this.PReport.Visible = false;
        }

        
else
        
{
            
this.PReport.Visible = true;
            Privileges Prl 
= new Privileges(obj, id);
            initOrg(Prl);
        }

    }

    
private void initOrg(Privileges p)
    
{
        
for (int i = 0; i < p.Panels.Count; i++)
        
{
            
//@panel
            PReport.ID = "Panel"+p.Panels[i].ID;
            PReport.Header.Text
=p.Panels[i].Name;
            
for (int j = 0; j < p.Panels[i].Groups.Count; j++)//@group
            {
                WebGroupBox pp 
= new WebGroupBox();
                pp.Text 
= p.Panels[i].Groups[j].CnName;
                pp.ID 
= "Wgb"+p.Panels[i].Groups[j].ID;//@groupid
                pp.Controls.Add(makeElements(p.Panels[i].Groups[j]));//@makeElements
                PReport.Controls.Add(pp);
            }

        }

        
    }

    
private ListControl makeElements(Privileges.Group g)//@element
    {
        
string type = g.Type.ToLower();
        ListControl lc;
        
if (type == "dropdownlist")
        
{
            lc 
= new DropDownList();
        }

        
else if (type == "radiobuttonlist")
        
{
            lc 
= new RadioButtonList();
        }

        
else if (type == "checkboxlist")
        
{
            lc 
= new CheckBoxList();
        }

        
else
        
{
            
throw new Exception("invalid data");
        }

        
for (int i = 0; i < g.Elements.Count; i++)
        
{
            ListItem li 
= new ListItem(g.Elements[i].Name, g.Elements[i].ID);
            
if (g.Elements[i].State == true)
            
{
                li.Selected 
= true;
            }

            lc.Items.Add(li);
        }

       
return lc;


    }

        
    
protected void ButtonUpdata_Click(object sender, EventArgs e)
    
{
        
//    for (int j = 0; j < PReport.Controls.Count; j++)//@groups
        
//    {
        
//        Control group = PReport.Controls[j];
        
//        string groupid = group.ID;
        
//        string value = string.Empty;                
        
//        for (int m = 0; m < group.Controls.Count; m++)//@elementlist
        
//        {
        
//            Control lc = (ListControl)group.Controls[m];
                    
        
//        }
        
//    }

        
//}


    }

    
}


问题:当我点更新返回的时候,发现我所new出来的control全都没有了!(可能代码写的很烂,为了避免大家头疼,我把代码简化了一点)
解决:我通过msdn发现原来由于dom本身就是扁平的(呵呵,我自己这么说,提法有点业余)所以需要通过一个NamingContainer属性来访问下级控件,可是俺找了半天也没有找到这样的方法,msdn中的示例是空的,郁闷...
如果改用Repeater我以前写的好多业务类(是我考虑了很久才写出来的)还得重新写