DataList 提供相關(guān)的編輯模板,但和DataGrid不一樣的是,DataList沒(méi)有編輯按鈕。要在DataList中使用編輯功能,可在項模板中增加一個(gè)按 鈕,Linkbutton和Button都可以。在CommandName中設置為Edit就可以把此按鈕和DataList的編輯事件聯(lián)系起來(lái)了。
如:
編輯按鈕可以使用CommandName="Edit"
更新按鈕可以使用CommandName="Update"
取消按鈕可以使用CommandName="Cancel"
刪除按鈕可以使用CommandName="Delete"
來(lái)實(shí)現。
-------------------------------------------------------------------------------------------------------------------------
控件設置:
<asp:DataList ID="UserList" DataKeyField="Uid" OnItemCreated="UserList_ItemCreated" OnUpdateCommand="UserList_OnUpdateCommand" OnDeleteCommand="UserList_OnDeleteCommand" runat="server" Width="100%" RepeatColumns="6" OnEditCommand="UserList_OnEditCommand" OnCancelCommand="UserList_OnCancelCommand">
<HeaderTemplate>
<table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#E2EEF5" >
<tr>
<td width="17%" height="25" align="center" bgcolor="#E8F0F7">登陸名稱(chēng)</td>
<td width="15%" align="center" bgcolor="#E8F0F7">真實(shí)姓名</td>
<td width="17%" align="center" bgcolor="#E8F0F7">所屬用戶(hù)組</td>
<td width="25%" align="center" bgcolor="#E8F0F7">擁有權限</td>
<td width="14%" align="center" bgcolor="#E8F0F7">創(chuàng )建時(shí)間</td>
<td width="12%" align="center" bgcolor="#E8F0F7">操作</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td height="25" bgcolor="#FFFFFF"><img src="images/FriendICO.gif" width="15" height="15" style="margin-left:5px;" /> [<%#Eval("Uname")%>]<asp:Image ID="StateICO" ImageUrl='<%#GetUserState(Eval("uState").ToString()) %>' width="12" height="12" runat="server" /></td>
<td align="center" bgcolor="#FFFFFF"><%#Eval("Rname")%></td>
<td align="center" bgcolor="#FFFFFF"><%#Eval("UserGroup")%></td>
<td align="center" bgcolor="#FFFFFF"><%#Eval("Purview")%></td>
<td align="center" bgcolor="#FFFFFF"><%#Eval("Ctimes","{0:d}")%></td>
<td align="center" bgcolor="#FFFFFF"><img src="images/Edit_ICOS.gif" width="14" height="15" border="0" align="absmiddle" /><asp:LinkButton CommandName="Edit"
ID="Edit_But" ForeColor="#003366" runat="server">編輯</asp:LinkButton> <img src="images/DEL_ICOS.gif" width="14" height="15" border="0" align="absmiddle" /><asp:LinkButton CommandName="Delete"
ID="Del_But" ForeColor="#003366" runat="server" >刪除</asp:LinkButton></td>
</tr>
</ItemTemplate>
<EditItemTemplate>
<tr>
<td height="25" bgcolor="#E8F0F7"><img src="images/FriendICO.gif" width="15" height="15" style="margin-left:5px;" /> <%#Eval("Uname")%><asp:CheckBox ID="User_State" Checked='<%#GetBoxState(Eval("uState").ToString().Trim()) %>' runat="server" Text="啟用賬戶(hù)" />
</td>
<td align="center" bgcolor="#E8F0F7"><asp:TextBox ID="User_Rname" runat="server" CssClass="inputX"
Text='<%#Eval("Rname")%>' Width="80px"></asp:TextBox>
</td>
<td align="center" bgcolor="#E8F0F7">
<asp:DropDownList ID="User_Group" runat="server" CssClass="FontBlue12">
<asp:ListItem Value="Guest">來(lái)賓組</asp:ListItem>
<asp:ListItem Value="Editor">編輯組</asp:ListItem>
<asp:ListItem Value="System">系統組</asp:ListItem>
</asp:DropDownList>
</td>
<td align="center" bgcolor="#E8F0F7">
<asp:CheckBoxList ID="User_Priv" runat="server" CssClass="FontBlue12"
RepeatDirection="Horizontal">
<asp:ListItem Value="r">讀</asp:ListItem>
<asp:ListItem Value="w">寫(xiě)</asp:ListItem>
<asp:ListItem Value="e">編輯</asp:ListItem>
<asp:ListItem Value="d">刪除</asp:ListItem>
</asp:CheckBoxList>
</td>
<td align="center" bgcolor="#E8F0F7"><%#Eval("Ctimes","{0:d}")%></td>
<td align="center" bgcolor="#E8F0F7">
<img align="absmiddle" border="0" height="15" src="images/Update_ICOS.gif"
width="14" /><asp:LinkButton ID="Update_But" runat="server"
CommandName="Update" ForeColor="#003366">更新</asp:LinkButton> <img align="absmiddle" border="0" height="15" src="images/Cancel_ICOS.gif"
width="14" /><asp:LinkButton ID="Cancel_But" runat="server"
CommandName="Cancel" ForeColor="#003366">取消</asp:LinkButton>
</td>
</tr>
</EditItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:DataList>
后臺代碼:
//取消編輯
protected void UserList_OnCancelCommand(object sender, DataListCommandEventArgs e)
{
this.UserList.EditItemIndex = -1;
GetUserList();
}
//DataList編輯
protected void UserList_OnEditCommand(object sender, DataListCommandEventArgs e)
{
this.UserList.EditItemIndex = e.Item.ItemIndex;
GetUserList();
}
//DataList更新
protected void UserList_OnUpdateCommand(object sender, DataListCommandEventArgs e)
{
string id = this.UserList.DataKeys[e.Item.ItemIndex].ToString(); //使用前需先設置DataList的DataKeyField="Uid"
string Rname = ((TextBox)e.Item.FindControl("User_Rname")).Text.Trim();//取得DataList中ID為"User_Rname"的TextBox中的值
string UserGroup = ((DropDownList)e.Item.FindControl("User_Group")).SelectedValue.Trim();
string UserPurview = "";
for (int i = 0; i < ((CheckBoxList)e.Item.FindControl("User_Priv")).Items.Count; i++)
{
if (((CheckBoxList)e.Item.FindControl("User_Priv")).Items[i].Selected == true)
{
UserPurview += ((CheckBoxList)e.Item.FindControl("User_Priv")).Items[i].Value + ",";
}
}
if (UserPurview != "")
{
UserPurview = UserPurview.Substring(0, UserPurview.Length - 1);
}
int usstate = 0;
if (((CheckBox)e.Item.FindControl("User_State")).Checked == true)
{
usstate = 1;
}
string sql = "update SystemUser set uState =" + usstate + ",UserGroup='" + UserGroup + "',Rname='" + Rname + "',Purview='" + UserPurview + "' where uid=" + id + "";
ConnDB db = new ConnDB();
if (db.EditDatabase(sql))
{
msgBox.Alert("編輯成功!", "SysUser_Admin.aspx");
}
else
{
msgBox.Alert("編輯失??!" + ((CheckBox)e.Item.FindControl("User_State")).ToString());
}
}
//DataList刪除對話(huà)框
protected void UserList_ItemCreated(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
LinkButton lbDelete = (LinkButton)e.Item.FindControl("Del_But"); lbDelete.Attributes.Add("onclick", "return confirm(""確定刪除這個(gè)用戶(hù)嗎?"");");
}
}
//DataList刪除
protected void UserList_OnDeleteCommand(object sender, DataListCommandEventArgs e)
{
string id = this.UserList.DataKeys[e.Item.ItemIndex].ToString(); //使用前需先設置DataList的DataKeyField="Uid"
string sql = "delete from SystemUser where Uid=" + id + "";
ConnDB db = new ConnDB();
if (db.EditDatabase(sql))
{
msgBox.Alert("已刪除!", "SysUser_Admin.aspx");
}
else
{
msgBox.Alert("刪除失??!", "SysUser_Admin.aspx");
}
GetUserList();
}
//DataList數據綁定
private void DataListDataBind()
{
SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["DataBaseCon"].ToString());
SqlDataAdapter da=new SqlDataAdapter("select * from Employees",conn);
DataSet ds=new DataSet();
try
{
da.Fill(ds,"testTable");
dlEditItem.DataSource=ds.Tables["testTable"];
dlEditItem.DataBind();
}
catch(Exception error)
{
Response.Write(error.ToString());
}
}
聯(lián)系客服