界面代码:
注:因为用的是第三方控件《Telerik》所以有些和原本的自带的控件用法不一样,比如:在用第三方控件Telerik的时候界面必须要有<telerik:RadScriptManager ID="RadScriptManager1" runat="server"> </telerik:RadScriptManager>这个控件才能运行。
<telerik:RadScriptManager ID="RadScriptManager1" runat="server">
</telerik:RadScriptManager>
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
<AjaxSettings>
<telerik:AjaxSetting AjaxControlID="VocationPage">
<UpdatedControls>
<telerik:AjaxUpdatedControl ControlID="VocationPage" />
<telerik:AjaxUpdatedControl ControlID="Vocation" LoadingPanelID="RadAjaxLoadingPanel1" />
</UpdatedControls>
</telerik:AjaxSetting>
<telerik:AjaxSetting AjaxControlID="Grid_Vocation">
<UpdatedControls>
<telerik:AjaxUpdatedControl ControlID="Grid_VocationPage" LoadingPanelID="RadAjaxLoadingPanel1" />
</UpdatedControls>
</telerik:AjaxSetting>
</AjaxSettings>
</telerik:RadAjaxManager>
<telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" Skin="Default" MinDisplayTime="500">
</telerik:RadAjaxLoadingPanel>
<telerik:RadSplitter ID="RadSplitter" runat="server" Width="100%">
<telerik:RadPane ID="RadPane1" runat="server" Width="200px">
<telerik:RadTreeView ID="Tree_VocationPage" runat="server" Width="100%" OnNodeExpand="Grid_VocationPage_NodeExpand">
</telerik:RadTreeView>
</telerik:RadPane>
<telerik:RadSplitBar ID="RadSplitBar1" runat="server" Width="100px">
</telerik:RadSplitBar>
<telerik:RadPane ID="RadPane3" runat="server">
<telerik:RadGrid ID="Grid_Vocation" runat="server" AutoGenerateColumns="false" AllowAutomaticInserts="true"
AllowAutomaticUpdates="true" ShowStatusBar="true"
OnDeleteCommand="Grid_Vocation_DeleteCommand"
OnNeedDataSource="Grid_Vocation_NeedDataSource"
OnUpdateCommand="Grid_Vocation_UpdateCommand"
OnInsertCommand="Grid_Vocation_InsertCommand">
<MasterTableView EditMode="InPlace" DataKeyNames="Code" TableLayout="Fixed" CommandItemDisplay="Top">
<Columns>
<telerik:GridTemplateColumn HeaderText="编码">
<ItemTemplate>
<%#Eval("Code")%>
</ItemTemplate>
<EditItemTemplate>
<telerik:RadTextBox ID="Code" Text='<%#Eval("Code") %>' runat="server">
</telerik:RadTextBox>
<asp:RequiredFieldValidator ID="rtxtCodeValid" runat="server" Display="Dynamic"
ControlToValidate="Code" ErrorMessage="*" ForeColor="Red" />
</EditItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn HeaderText="名称">
<ItemTemplate>
<%#Eval("Name") %>
</ItemTemplate>
<EditItemTemplate>
<telerik:RadTextBox ID="Name" Text='<%#Eval("Name") %>' runat="server">
</telerik:RadTextBox>
<asp:RequiredFieldValidator ID="rtxtNameValid" runat="server" Display="Dynamic"
ControlToValidate="Name" ErrorMessage="*" ForeColor="Red" />
</EditItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn HeaderText="上级编码">
<ItemTemplate>
<%#Eval("ParentCode")%>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="ParentCode" runat="server" Text="上级编码不需要手动操作"></asp:Label>
<%--<telerik:RadTextBox ID="ParentCode" Text='<%#Eval("ParentCode") %>' runat="server">
</telerik:RadTextBox>--%>
</EditItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridEditCommandColumn ButtonType="LinkButton" InsertText="添加" UpdateText="更新"
CancelText="取消" EditText="编辑" UniqueName="EditCommandColumn">
<HeaderStyle Width="85px"></HeaderStyle>
</telerik:GridEditCommandColumn>
<telerik:GridButtonColumn CommandName="delete" ConfirmText="请确认是否删除?" ButtonType="LinkButton"
Text="删除">
</telerik:GridButtonColumn>
</Columns>
<CommandItemTemplate>
<div style="padding: 5px 5px;">
<asp:LinkButton ID="btnInitInsert" runat="server" CommandName="InitInsert" Text="新增"></asp:LinkButton>
</div>
</CommandItemTemplate>
</MasterTableView>
<ClientSettings EnableRowHoverStyle="true" AllowDragToGroup="false">
<Selecting AllowRowSelect="True" />
<Scrolling AllowScroll="false" UseStaticHeaders="true" />
</ClientSettings>
<SelectedItemStyle CssClass="SelectedItem" />
</telerik:RadGrid>
</telerik:RadPane>
</telerik:RadSplitter>
CS代码:
VocationBLL vocationBLL = new VocationBLL();//BLL层
List<Vocation> vlist = null;//树形的集合
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["vocations"] = "0";//设置父节点
GetVocation();
BindingVocation();
}
}
public void GetVocation()
{
Grid_Vocation.DataSource = vocationBLL.GetVocationAll();
}
/// <summary>
/// 树形
/// </summary>
///
public void BindingVocation()
{
vlist = vocationBLL.GetVocationAll();
Tree_VocationPage.Nodes.Clear();//清除
foreach (Vocation model in vlist.Where(i => i.ParentCode == "0"))
{
RadTreeNode node = new RadTreeNode();
node.Text = "[" + model.Code + "]" + model.Name;
node.Value = model.Code;
node.ExpandMode = TreeNodeExpandMode.ServerSide;
if (vocationBLL.SelectSonInfo(model.Code).Count > 0)
{
node.Nodes.Add(PopulateNodeOnDemand(node, vocationBLL.SelectSonInfo(model.Code)));
}
Tree_VocationPage.Nodes.Add(node);
}
}
/// <summary>
/// 递归调用子集
/// </summary>
/// <param name="e"></param>
/// <param name="expandMode"></param>
private RadTreeNode PopulateNodeOnDemand(RadTreeNode KNode, List<Vocation> list)
{
RadTreeNode node = null;
foreach (Vocation model in list)
{
node = new RadTreeNode();
node.Text = "[" + model.Code + "]" + model.Name;
node.Value = model.Code;
node.ExpandMode = TreeNodeExpandMode.ServerSide;
if (vocationBLL.SelectSonInfo(model.Code).Count > 0)
{
PopulateNodeOnDemand(node, vocationBLL.SelectSonInfo(model.Code));
}
KNode.Nodes.Add(node);
}
return node;
}
/// <summary>
/// 查询下级科目
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void RadTreeViewSub_NodeExpand(object sender, RadTreeNodeEventArgs e)
{
if (!string.IsNullOrEmpty(e.Node.Value))
{
ViewState["vocations"] = e.Node.Value;
//绑定下级信息
this.Grid_Vocation.DataSource = vocationBLL.SelectSonInfo(ViewState["vocations"] as string);
this.Grid_Vocation.DataBind();
}
}
/// <summary>
/// 删除事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Grid_Vocation_DeleteCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
{
string code = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Code"].ToString();
try
{
vocationBLL.DeleteVocationByCode(code);
GetVocation();
BindingVocation();
}
catch (Exception ex)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "focus", "alert('删除失败," + ex.Message + "!');", true);
}
}
protected void Grid_Vocation_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
this.Grid_Vocation.DataSource = vocationBLL.SelectSonInfo(ViewState["vocations"] as string);
}
/// <summary>
/// 更新
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Grid_Vocation_UpdateCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
{
try
{
GridEditableItem editedItem = e.Item as GridEditableItem;
string Code = (editedItem.FindControl("Code") as RadTextBox).Text;
Vocation model = new Vocation();
model.Code = Code;
model.Name = (editedItem.FindControl("Name") as RadTextBox).Text;
model.ParentCode = ViewState["vocations"] as string;
vocationBLL.UodateVocation(model);
Grid_Vocation.MasterTableView.Items[e.Item.ItemIndex].Edit = false;//关闭当前修改窗口
BindingVocation();
}
catch (Exception ex)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "focus", "alert('请检查上一级表," + ex.Message + "!');", true);
}
}
/// <summary>
/// 添加 事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Grid_Vocation_InsertCommand(object sender, GridCommandEventArgs e)
{
try
{
GridEditableItem editedItem = e.Item as GridEditableItem;
Vocation model = new Vocation();
model.Code = (editedItem.FindControl("Code") as RadTextBox).Text;
if (vocationBLL.CheckCode(model.Code))
{
model.Name = (editedItem.FindControl("Name") as RadTextBox).Text;
model.ParentCode = ViewState["vocations"] as string;
vocationBLL.AddVocation(model);
this.Grid_Vocation.DataSource = vocationBLL.SelectSonInfo(ViewState["vocations"] as string);
Grid_Vocation.Rebind();
BindingVocation();
}
else
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "focus", "alert('不能添加重复的主键值');", true);
return;
}
}
catch (Exception )
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "focus", "alert('请检查添加数据');", true);
}
}
protected void Grid_VocationPage_NodeExpand(object sender, RadTreeNodeEventArgs e)
{
if (!string.IsNullOrEmpty(e.Node.Value))
{
ViewState["vocations"] = e.Node.Value;
//绑定下级信息
this.Grid_Vocation.DataSource = vocationBLL.SelectSonInfo(ViewState["vocations"] as string);
this.Grid_Vocation.DataBind();
}
}
}