|
|
|
มีวิธีการทำ เมนูรายการสินค้าและซัพเมนูแบบเพิ่มได้ตลอดไหมครับ ASP.NET C# |
|
|
|
|
|
|
|
ผมเคยใช้พวก Repeater ในการสร้าง Menu ครับ
ลองดูครับ
|
|
|
|
|
Date :
2012-12-11 17:32:38 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ลองเอาไปประยุกต์ดูครับ น่าจะประมาณนี้นะ
Default.ASPX
Code (ASP)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<div>
<asp:Label ID="lblErrorMessage" runat="server" ForeColor="Red" Text="Label"></asp:Label>
</div>
<div>
<span>
<asp:Label ID="lblCode" runat="server" Text="รหัส : "></asp:Label>
<asp:TextBox ID="txtCode" runat="server"></asp:TextBox>
</span><span>
<asp:Label ID="lblMenuName" runat="server" Text=" ชื่อเมนู : "></asp:Label>
<asp:TextBox ID="txtMenuName" runat="server"></asp:TextBox>
</span><span>
<asp:Label ID="lblMainMenuCode" runat="server" Text=" เป็นเมนูลูกของ : "></asp:Label>
<asp:DropDownList ID="cboMainMenuCode" runat="server" Height="19px">
</asp:DropDownList>
</span>
</div>
<asp:Button ID="btnOk" runat="server" Text="ตกลง" />
<asp:TreeView ID="tvMenu" runat="server" ShowLines="true">
</asp:TreeView>
</div>
</form>
</body>
</html>
Default.ASPX.CS
Code (C#)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public class MenuItem {
public string P_Code { get; set; }
public string P_Name { get; set; }
public string P_MainMenuCode { get; set; }
}
public partial class _Default : System.Web.UI.Page {
public List<MenuItem> P_ListMenuItem {
set {
Session["listMenuItem"] = value;
}
get {
if (Session["listMenuItem"] == null) {
Session["listMenuItem"] = new List<MenuItem>();
}
return (List<MenuItem>)Session["listMenuItem"];
}
}
protected void Page_Load(object sender, EventArgs e) {
if (!this.IsPostBack) {
this.P_ListMenuItem = null;
fillDataToCboMainMenuCode();
}
btnOk.Click += new EventHandler(btnOk_Click);
}
void btnOk_Click(object sender, EventArgs e) {
if (isDuplicateMenuCode(txtCode.Text)) {
lblErrorMessage.Text = "รหัสมีค่าซ้ำ";
return;
}
MenuItem mi = new MenuItem() {
P_Code = txtCode.Text,
P_Name = txtMenuName.Text,
P_MainMenuCode = cboMainMenuCode.SelectedValue
};
this.P_ListMenuItem.Add(mi);
fillDataToCboMainMenuCode();
tvMenu.Nodes.Clear();
getTreeNodeList(string.Empty).ForEach(tn => tvMenu.Nodes.Add(tn));
tvMenu.ExpandAll();
txtCode.Text = string.Empty;
txtMenuName.Text = string.Empty;
lblErrorMessage.Text = string.Empty;
}
private bool isDuplicateMenuCode(string pMenuCode) {
return (
from m in P_ListMenuItem
where m.P_Code == pMenuCode
select m
).Count() > 0;
}
private void fillDataToCboMainMenuCode() {
cboMainMenuCode.DataSource =
(new[] { new { value = string.Empty, text = "( ไม่มี )" } })
.Union
(from m in this.P_ListMenuItem
select new { value = m.P_Code, text = "( " + m.P_Code + " ) " + m.P_Name });
cboMainMenuCode.DataTextField = "text";
cboMainMenuCode.DataValueField = "value";
cboMainMenuCode.DataBind();
}
public List<TreeNode> getTreeNodeList(string pMenuCode) {
List<TreeNode> result = (
from m in P_ListMenuItem
where m.P_MainMenuCode == pMenuCode
select new TreeNode(m.P_Name, m.P_Code)).ToList();
result.ForEach(
tn => getTreeNodeList(tn.Value).ForEach(
tn2 => tn.ChildNodes.Add(tn2)
)
);
return result;
}
}
|
|
|
|
|
Date :
2012-12-15 17:33:14 |
By :
SandKing |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขอบคุณครับ จะลองดูครับ ^^
|
|
|
|
|
Date :
2012-12-18 13:16:19 |
By :
Lightning |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 02
|