<ASP:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnSorting="GridView1_Sorting" OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDataBound="GridView1_RowDataBound" OnPageIndexChanging="GridVIEw1_PageIndexChanging" CellPadding="4" AllowSorting="True" AllowPaging="True" PageSize="18">
<Columns>
<ASP:BoundField DataFIEld="class_id" HeaderText="編號" ReadOnly="True" SortExpression="class_id">
<ItemStyle Width="60px" />
</ASP:BoundFIEld>
<ASP:BoundField DataFIEld="class_name" HeaderText="類別名稱" SortExpression="class_name">
<ItemStyle Width="200px" />
</ASP:BoundFIEld>
<ASP:TemplateFIEld HeaderText="父類別" SortExpression="class_parent">
<ItemTemplate>
<%#bindParent_Name(Eval("class_parent").ToString())%>
</ItemTemplate>
<EditItemTemplate>
<ASP:HiddenFIEld ID="HDFXueli" runat="server" Value=''<%# Eval("class_parent") %>'' />
<ASP:DropDownList ID="DDLXueli" runat="server" Width="90px" />
</EditItemTemplate>
<ItemStyle Width="100px" />
</ASP:TemplateFIEld>
<ASP:CommandFIEld EditText="修改" HeaderText="修改" ShowEditButton="True" >
<ItemStyle Width="60px" />
</ASP:CommandFIEld>
<ASP:CommandFIEld HeaderText="刪除" ShowDeleteButton="True" >
<ItemStyle Width="60px" />
</ASP:CommandFIEld>
</Columns>
<PagerStyle HorizontalAlign="Center" Font-Bold="True" />
<HeaderStyle HorizontalAlign="Left" />
<SelectedRowStyle Font-Bold="True" />
</ASP:GridVIEw>
private void Bind()
{
string key = "";
string parent_id = "";
try
{
key = Request.QueryString["key"];
parent_id = Request.QueryString["parent_id"];
}
catch
{
key = "";
parent_id = "";
}
DataTable dt;
if (parent_id != "" && parent_id != null)
{
Data d;
string cond = "";
if (key == "" || key == null)
{
if (parent_id == "0")
cond = "class_parent!=0";
else
cond = "class_parent=" + parent_id;
}
else
{
if (parent_id == "0")
cond = "class_name like ''%" + key + "%'' and class_parent!=0";
else
cond = "class_name like ''%" + key + "%'' and class_parent=" + parent_id;
}
d = new Data("bookClass", cond, "class_id desc", 1);
dt = d.getDataTable();
}
else
{
dt = Dol.dtClassTwo();
}
string sort = (string)ViewState["SortOrder"] + " " + (string)VIEwState["OrderDire"];
bind.bindGridView(this.GridVIEw1, dt, "class_id", sort);
}
//根據父目錄編號返回名稱
public string bindParent_Name(string class_id)
{
string parent_name = class_id;
for (int i = 0; i < dt.Rows.Count; i++)
{
if (dt.Rows[i][0].ToString().Trim() == class_id.Trim())
{
parent_name = dt.Rows[i][1].ToString();
break;
}
}
return parent_name;
}
protected void GridView1_RowDataBound(object sender, GridVIEwRowEventArgs e)
if (((DropDownList)e.Row.FindControl("DDLXueli")) != null)
{
DropDownList ddlxueli = (DropDownList)e.Row.FindControl("DDLXueli");
ddlxueli.DataSource = dt;
ddlxueli.DataTextFIEld = "class_name";
ddlxueli.DataValueFIEld = "class_id";
ddlxueli.DataBind();
// 選中 DropDownList
ddlxueli.SelectedValue = ((HiddenFIEld)e.Row.FindControl("HDFXueli")).Value;
}
//如果是綁定數據行
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
{
((LinkButton)e.Row.Cells[4].Controls[0]).Attributes.Add("onclick", "Javascript:return confirm(''你確認要刪除:[" + e.Row.Cells[1].Text + "]嗎?\\n刪除類別[" + e.Row.Cells[1].Text + "]後該類別下的所有圖書、子目錄和子目錄下的圖書都將被刪除!'')");
}
}
}
//排序
protected void GridView1_Sorting(object sender, GridVIEwSortEventArgs e)
{
string sPage = e.SortExpression;
if (VIEwState["SortOrder"].ToString() == sPage)
{
if (VIEwState["OrderDire"].ToString() == "Desc")
VIEwState["OrderDire"] = "ASC";
else
VIEwState["OrderDire"] = "Desc";
}
else
{
VIEwState["SortOrder"] = e.SortExpression;
Bind();
}
//編輯
protected void GridView1_RowEditing(object sender, GridVIEwEditEventArgs e)
{
GridVIEw1.EditIndex = e.NewEditIndex;
Bind();
}
//刪除
protected void GridView1_RowDeleting(object sender, GridVIEwDeleteEventArgs e)
{
string class_id = GridVIEw1.DataKeys[e.RowIndex].Value.ToString();
//刪除該 目錄下的圖書
DataDel d1 = new DataDel("book", "class_id=" + class_id);
d1.ExceDel();
//刪除該目錄下的子目錄
DataDel d3 = new DataDel("bookClass", "class_parent=" + class_id);
d3.ExceDel();
//刪除該目錄
DataDel d = new DataDel("bookClass", "class_id=" + class_id);
d.ExceDel();
Bind();
}
//更新
protected void GridView1_RowUpdating(object sender, GridVIEwUpdateEventArgs e)
{
string class_name = ((TextBox)(GridVIEw1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim();
string class_id = GridVIEw1.DataKeys[e.RowIndex].Value.ToString();
string class_parent = ((DropDownList)GridVIEw1.Rows[e.RowIndex].FindControl("DDLXueli")).SelectedValue;
Data d1 = new Data();
if (d1.count("bookClass", "class_name=''" + class_name + "'' and class_parent=" + class_parent + " and class_id!=" + class_id) == 0)
{
DataUpdate d = new DataUpdate("bookClass", "class_name=''" + class_name + "'',class_parent=" + class_parent, "class_id=" + class_id);
d.ExceUpdate();
GridVIEw1.EditIndex = -1;
Bind();
}
else
{
Alert a = new Alert("[" + class_name + "]名稱重復!");
a.show();
}
}
//取消
protected void GridView1_RowCancelingEdit(object sender, GridVIEwCancelEditEventArgs e)
{
GridVIEw1.EditIndex = -1;
Bind();
}
//分頁
protected void GridView1_PageIndexChanging(object sender, GridVIEwPageEventArgs e)
{
GridVIEw1.PageIndex = e.NewPageIndex;
Bind();
}