您的位置:首页 > 其它

分类管理模块-新增新的新闻分类(简单版本)

2012-05-22 23:40 337 查看
这个管理分类模块式是为了做新闻模块的一个前奏,给新闻模块提供必要的新闻分类。

设计界面:



数据库:



三个麻烦点:

1. 页面加载的时候,要做一个树形结构,把所有的分类显示在左侧。

2. 点击树的时候,分类的名称会自动显示到其父类。

3. 提交添加新的新闻分类。

HTML代码

<form id="form1" runat="server">
<table cellpadding="0" cellspacing="0" width="100%">
<tr>
<td style="width: 164px">
添加新闻分类</td>
<td colspan="2">
 </td>
</tr>
<tr>
<td rowspan="6" style="width: 164px">
<asp:TreeView ID="TreeView1" runat="server" ImageSet="Arrows"
onselectednodechanged="TreeView1_SelectedNodeChanged">
<ParentNodeStyle Font-Bold="False" />
<HoverNodeStyle Font-Underline="True" ForeColor="#5555DD" />
<SelectedNodeStyle Font-Underline="True" ForeColor="#5555DD"
HorizontalPadding="0px" VerticalPadding="0px" />
<NodeStyle Font-Names="Tahoma" Font-Size="10pt" ForeColor="Black"
HorizontalPadding="5px" NodeSpacing="0px" VerticalPadding="0px" />
</asp:TreeView>
</td>
<td colspan="2" style="text-align: center">
<asp:Label ID="lblMessage" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td style="width: 166px">
所属父类:</td>
<td>
<asp:TextBox ID="txtClassID" runat="server" Enabled="False"
ToolTip="请点击左边的导航树,选择需要的编辑">新闻</asp:TextBox>
<input type="hidden" id="hidClassID" value="1" runat="server" style="width: 60px" />
</td>
</tr>
<tr>
<td style="width: 166px">
分类名称:</td>
<td>
<asp:TextBox ID="txtClassName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td style="width: 166px">
是否有子类:</td>
<td>
<asp:RadioButtonList ID="rbl" runat="server" Height="20px"
RepeatDirection="Horizontal" Width="228px">
<asp:ListItem Value="1">是</asp:ListItem>
<asp:ListItem Selected="True" Value="0">否</asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
<tr>
<td style="width: 166px">
备注</td>
<td>
<asp:TextBox ID="txtExample" runat="server" Height="126px" TextMode="MultiLine"
Width="183px"></asp:TextBox>
</td>
</tr>
<tr>
<td style="width: 166px">
 </td>
<td>
<asp:Button ID="btnSubmit" runat="server" onclick="btnSubmit_Click" Text="提交" />
</td>
</tr>
</table>
<div>

</div>
</form>


CS文件:

protected void Page_Load(object sender, EventArgs e)
{
this.TreeView1.Nodes.Clear();
BindTree(this.TreeView1.Nodes, 0);

}
private void BindTree(TreeNodeCollection treecol, int parentID)   //解决第一个问题
{
CategoryBLL classsystem = new CategoryBLL();//调动业务层获取分类方法
List<Category> arrayList = classsystem.GetClassDataID(parentID);
TreeNode node;//创建新的节点
foreach (Category classData in arrayList)//遍历所有分类,并添加到树的节点下
{
node = new TreeNode();
node.Text = classData.ClassName + "(" + classData.Child + ")";//树的名称赋值
node.Value = classData.ClassId.ToString();//树的值赋值
treecol.Add(node);//新增节点
BindTree(node.ChildNodes, int.Parse(node.Value));//递归子节点
}
}
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)  //解决第二个问题
{
int id = int.Parse(this.TreeView1.SelectedNode.Value);
Category classData = new Category();
classData = (new CategoryBLL()).GetclassID(id);
this.txtClassID.Text = classData.ClassName;//获取分类的父类名称
this.hidClassID.Value = classData.ClassId.ToString();//获取分类父类ID,隐藏域
}
protected void btnSubmit_Click(object sender, EventArgs e)   //解决第三个问题
{
Category classdata = new Category();//创建一个新的实体
classdata.ClassName = this.txtClassName.Text.Trim();//为实体的各个属性赋值
classdata.ParentId = int.Parse(this.hidClassID.Value.ToString().Trim());
classdata.LastNode = int.Parse(this.rbl.SelectedValue.ToString());
classdata.Example = this.txtExample.Text.ToString();
CategoryDAL classSystem = new CategoryDAL();//调用业务层的添加方法
classSystem.AddClass(classdata);
this.ClientScript.RegisterStartupScript(this.GetType(), "succeed", "<script>alert('添加成功!')</script>");
this.txtClassName.Text = "";
this.rbl.Items[0].Selected = true;
this.txtExample.Text = "";//重新绑定数据到tree中
this.TreeView1.Nodes.Clear();
BindTree(this.TreeView1.Nodes, 0);
Response.Redirect("Admin_ClassAdd.aspx");
}


BLL层:

/// <summary>
/// 新增分类
/// </summary>
/// <param name="classData"></param>
public void AddClass(Category classData)
{
CategoryDAL cdal = new CategoryDAL();
cdal.AddClass(classData);
}
/// <summary>
/// 通过ID号获取信息
/// </summary>
/// <param name="parentID"></param>
/// <returns></returns>
public List<Category> GetClassDataID(int parentID)
{
return (new CategoryDAL()).GetClassData(parentID);
}
//根据classID来返回信息
//需要返回参数
public Category GetclassID(int classID)
{
return (new CategoryDAL()).GetClassID(classID);
}


DAL层:

public void AddClass(Category classData)
{
DataClassesDataContext db = new DataClassesDataContext();//创建数据上下文
db.Category.InsertOnSubmit(classData); //添加新数据到数据实体
db.SubmitChanges();//提交数据更改
}
/// <summary>
/// 根据parentID号获取一组信息
/// </summary>
/// <param name="parentID"></param>
/// <returns></returns>
public List<Category> GetClassData(int parentID)
{
DataClassesDataContext db = new DataClassesDataContext();//创建数据上下文
List<Category> mylist = db.Category.Where(c => c.ParentId == parentID).ToList();//返回所有的分类
return mylist;
}
/// <summary>
/// 通过classID来获取信息
/// </summary>
/// <param name="classID"></param>
/// <returns></returns>
public Category GetClassID(int classID)
{
DataClassesDataContext db = new DataClassesDataContext();
Category classresult = new Category();
try
{
classresult = db.Category.Where(c => c.ClassId == classID).First();
}
catch { }
return classresult;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐