最後一步顯示分類(只是一條select語句)
CREATE PROCEDURE sp_Column_List
AS
SELECT Column_ID,Column_Name,Parent_ID,Column_Path,Column_Depth,
Column_Order,Column_Intro
FROM Tb_Column
ORDER BY Column_Order
GO
接下來就是在界面呈現了,顯示的時候用的是DataGrid
Html代碼如下:
<asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False" Width="520px" DataKeyField="Column_Id">
<Columns>
<asp:TemplateColumn HeaderText="分類信息">
<ItemTemplate>
<asp:Label id="lbname" runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="tbdgname" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"Column_Name")%>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="添加子節點">
<ItemTemplate>
<a href="#" onclick="Open('AddSonCate.aspx?fid=<%#DataBinder.Eval(Container.DataItem,"Column_Id")%>','son',280,80)">添加子分類</a>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="編輯">
<ItemTemplate>
<asp:LinkButton runat="server" Text="編輯" CommandName="Edit" CausesValidation="false"></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton runat="server" Text="更新" CommandName="Update" CausesValidation="False"></asp:LinkButton>
<asp:LinkButton runat="server" Text="取消" CommandName="Cancel" CausesValidation="false"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="刪除">
<ItemTemplate>
<asp:LinkButton runat="server" ID="lbdelete" Text="刪除" CommandName="Delete" CausesValidation="false" CommandArgument='<%#DataBinder.Eval(Container.DataItem,"Column_Id")%>'>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
最關鍵的地方在DataGrid_ItemDataBind事件中
private void DataGrid1_ItemDataBound(object sender,System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
//得到Column_Name字段的值
string columnName = (string)DataBinder.Eval(e.Item.DataItem,"Column_Name");
string columnTemp = "";
//得到深度值Column_Depth
int columnDepth = Int32.Parse(DataBinder.Eval(e.Item.DataItem,"Column_Depth").ToString());
if(columnDepth>1)
{
for(int i = 1;i<columnDepth;i++)
{
columnTemp +=" ";
}
columnTemp+="┝";
}
Label lbname = (Label)e.Item.FindControl("lbname");
lbname.Text = columnTemp+columnName ;
LinkButton lbdelete = (LinkButton)e.Item.FindControl("lbdelete");
lbdelete.Attributes.Add("OnClick","JavaScript:return confirm('確實要刪掉此分類嗎?');");
}
}
Over!