2011-02-11 111 views
0

我的代碼如下任何替代方案在asp.net菜單控制的XmlDataSource

DataSet ds = new DataSet(); 
     string connStr = "Data Source=PARITAS00024;Initial Catalog=MenuDb;Persist Security Info=True;User ID=sa;Password=paritas123"; 
     using (SqlConnection conn = new SqlConnection(connStr)) 
     { 
      string sql = "Select MenuId, MenuTitle, MenuDesc, MenuURL, ParentMenuId from tblMenus where Status=1 and RecordStatus=1 order by ParentMenuId, DisplayOrder"; 
      SqlDataAdapter da = new SqlDataAdapter(sql, conn); 
      da.Fill(ds); 
      da.Dispose(); 
     } 
     ds.DataSetName = "Menus"; 
     ds.Tables[0].TableName = "Menu"; 
     DataRelation relation = new DataRelation("ParentChild", ds.Tables["Menu"].Columns["MenuId"], ds.Tables["Menu"].Columns["ParentMenuId"], true); 

     relation.Nested = true; 
     ds.Relations.Add(relation); 

     System.Web.UI.WebControls.XmlDataSource xds = new System.Web.UI.WebControls.XmlDataSource(); 
     xds.TransformFile = "~/TransformXSLT.xsl"; 
     xds.XPath = "MenuItems/MenuItem"; 
     xds.Data = ds.GetXml(); 
     xds.ID = "xmlDataSourceMenu"; 

     Menu1.DataSource = xds; 
     Menu1.DataBind(); 

使用的XmlDataSource的這種正確的方法?

+0

依賴於XML和XSL文件的內容..你的問題到底是什麼? – 2011-02-11 08:48:48

+0

這裏我不使用xmldatasource控件,而是創建了我自己的xmldatasource對象,並使用它進行即時通訊。我想知道的是,這是正確的方式嗎? – Harsha 2011-02-11 09:02:05

回答

1

使用數據源的優勢與聲明性編程相關:將工作重點從工作必須完成的地方轉移到結果。如果您使用命令式數據源,則會失去所有優勢。

在這段代碼中,您將通過XSL轉換爲您的菜單提供一些XML數據,以轉換查詢返回的DataSet的XML表示形式:您是否真的需要執行所有這些工作?

爲什麼不以編程方式填充菜單?

foreach (DataRow parentItem in ds.Tables[0].Rows) 
    { 
    MenuItem item = new MenuItem((string)parentItem["Name"]); 
    menu.Items.Add(categoryItem); 

    ... 
    } 

,或者爲什麼沒有在ASPX使用的XmlDataSource:

<asp:XmlDataSource TransformFile="~/TransformXSLT.xsl" XPath="MenuItems/MenuItem" ID="xmlDataSourceMenu" runat="server" /> 

,並在後面的代碼:

... 
xmlDataSourceMenu.Data = ds.GetXml(); 
... 
+0

以及我的要求是使用XML文件來顯示菜單,而不使用aspx頁面中的任何數據控制。我想你是對的。 – Harsha 2011-02-21 17:16:01

0
try 
{ 
    XmlDocument xdoc = new XmlDocument();  
    SqlConnection cnn = null;  
    SqlCommand cmd = null;  

    // connection string from web.config 
    cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["____"].ConnectionString); 
    cnn.Open(); 

    // SP must return hierarchy of menu items. 
    string strUSP = "USP_XMLStoredProcedureName"; 
    cmd = new SqlCommand(strUSP, cnn); 

    XmlReader reader = cmd.ExecuteXmlReader(); 

    if (reader.Read()) { xdoc.Load(reader); } 

    // DRAG AND DROP XMLDataSource from toolbox , provide id as "DataSourceXML" 
    DataSourceXML.Data = xdoc.InnerXml.ToString(); 

    // To avoid root 
    DataSourceXML.XPath = "/ParentMenu/SubMenu"; 

    // :: Provide XMLDatasource ID to Menucontrol. 

    // OR 
    XmlDataSource XDS = new XmlDataSource(); 
    XDS.ID = "XMLDataSourceID"; 
    XDS.Data= xdoc.InnerXml; 

    // To avoid root 
    XDS.XPath = "/ParentMenu/SubMenu"; 

    MyMenu.DataSource = XDS; 
    MyMenu.DataBind(); 

} 
catch (Exception ex) 
{ 
    throw ex; 
} 
finally 
{ 
    cmd.Dispose(); 
    cnn.Close(); 
}