📄 defaultcs.aspx.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Telerik.WebControls;
namespace Telerik.MenuExamplesCSharp.Menu.Examples.Design.DataBinding
{
/// <summary>
/// Summary description for DefaultCS.
/// </summary>
public class DefaultCS : Telerik.QuickStart.XhtmlPage
{
protected System.Web.UI.WebControls.RadioButtonList RadioButtonList1;
protected RadMenu Menu1;
protected System.Web.UI.WebControls.Label Label1;
protected string DefaultDSN = String.Empty;
private DataSet GetMenuDataSource(string inputDSN)
{
OleDbConnection OldDbCon = new OleDbConnection(inputDSN);
OldDbCon.Open();
OleDbDataAdapter MenuDataAdaptor = new OleDbDataAdapter("SELECT Text,idtext,parentIdtext FROM Links", OldDbCon);
DataSet MenuDataLoader = new DataSet();
MenuDataAdaptor.Fill(MenuDataLoader);
OldDbCon.Close();
return MenuDataLoader;
}
// The old DataBind method uses the GenerateMenu, which sets
// the data Source, and then cycles trough the dataset calling
// RecursivelyPopulate which sets recursively the control trees
private void GenerateMenu(string inputDSN)
{
OleDbConnection OldDbCon = new OleDbConnection (inputDSN);
OldDbCon.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT id,parentId,Text,idtext,parentIdtext FROM Links WHERE status=1", OldDbCon);
DataSet ds = new DataSet();
adapter.Fill(ds);
ds.Relations.Add("NodeRelation", ds.Tables[0].Columns["id"], ds.Tables[0].Columns["parentId"],false);
MenuGroup MainGroup = new MenuGroup();
MainGroup.Flow = PresentationDirection.Horizontal;
Menu1.RootGroup = MainGroup;
Menu1.ImagesBaseDir = "~/Menu/Examples/Programming/DataBinding/Images/";
foreach(DataRow dbRow in ds.Tables[0].Rows)
{
if(dbRow.IsNull("parentId"))
{
Telerik.WebControls.MenuItem item = new Telerik.WebControls.MenuItem();
item.Text = dbRow["Text"].ToString();
MainGroup.AddItem(item);
RecursivelyPopulate(dbRow, item);
}
}
OldDbCon.Close();
}
private void RecursivelyPopulate(DataRow dbRow, Telerik.WebControls.MenuItem item)
{
MenuGroup CurrentGroup = new MenuGroup();
foreach (DataRow childRow in dbRow.GetChildRows("NodeRelation"))
{
Telerik.WebControls.MenuItem childItem = new Telerik.WebControls.MenuItem();
childItem.Text = childRow["Text"].ToString();
CurrentGroup.AddItem(childItem);
RecursivelyPopulate(childRow, childItem);
}
if (CurrentGroup.Items.Count > 0)
{
item.RightLogo = "arrow_right.gif";
item.ChildGroup = CurrentGroup;
}
}
private void processMainItems()
{
if (Menu1.RootGroup != null && Menu1.RootGroup.Items.Count > 0)
{
for (int i=0;i<Menu1.RootGroup.Items.Count;i++)
{
Menu1.RootGroup.Items[i].CssClass = "MainItem";
Menu1.RootGroup.Items[i].CssClassOver = "MainItemOver";
}
}
}
private void MenuRealBind()
{
// here we set the Data source string (DSN) required by ADO.NET classes
DefaultDSN = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("menudata.mdb");
switch(Convert.ToInt32(RadioButtonList1.Items[RadioButtonList1.SelectedIndex].Value))
{
case 1:
// here we use the new Asp.NET DataBinding mechanism
Menu1.EnableGroupChildrenImage = true;
Menu1.DataFieldID = "idtext";//idtext
Menu1.DataFieldParentID = "parentIdtext";//parentIdtext
Menu1.DataSource = GetMenuDataSource(DefaultDSN).Tables[0].DefaultView;
Menu1.DataBind();
Menu1.RootGroup.Flow = PresentationDirection.Horizontal;
Label1.Text = "Menu is data-bound using Asp.NET DataBind() method";
break;
case 2:
GenerateMenu(DefaultDSN);
Label1.Text = "Menu is data-bound using RecursivelyPopulate() method";
break;
default:
GenerateMenu(DefaultDSN);
Label1.Text = "Menu is data-bound using RecursivelyPopulate() method";
break;
}
}
private void RadioSwitch_Clicked(object sender, System.EventArgs e)
{
MenuRealBind();
processMainItems();
}
private void Evnt_Test(object sender, MenuEntityInstantiatedEventArgs e)
{
if (e.Item != null)
{
//e.Item.Text = "test_change";
}
if (e.DataItem != null)
{
}
}
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
MenuRealBind();
processMainItems();
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Menu1.ItemDataBound += new Telerik.WebControls.RadMenu.RadMenuItemInstantiatedEventHandler(this.Evnt_Test);
this.RadioButtonList1.SelectedIndexChanged += new System.EventHandler(this.RadioSwitch_Clicked);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -