开发者

any alternative to xmldatasource in asp.net menu control

开发者 https://www.devze.com 2023-02-10 02:21 出处:网络
my code is as follows DataSet ds = new DataSet(); string connStr = \"Data Source=PARITAS00024;Initial Catalog=MenuDb;Persist Security Info=True;User ID=sa;Password=paritas123\";

my code is as follows

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, DisplayO开发者_如何学Pythonrder";
            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();

is this correct way of using the xmldatasource ?


The advantages of using of data sources are related to declarative programming: move the focus from how work must be done to the results. If you are using a datasource in imperative way, you lose all the advantages.

In that code you are giving your menu some XML data got transforming the XML representation of a DataSet returned by a query through an XSL transformation: do you really need to do all that work?

Why don't populate the menu programmatically?

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

    ...
  }

or, why don't use the XmlDataSource in the aspx:

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

and, in code behind:

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


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();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消