1.实现功能
使用DataList进行数据绑定,并进行数据的修改、删除等操作。
2.步骤
1).添加DataList控件,并定制HeaderTemplate、ItemTemplate、AlternatingItemTemplate、SeparatorTemplate、SelectedItemTemplate、EditItemTemplate、FooterTemplate模板样式;
2).给各功能按钮设置CommandName属性,值的类型有edit、update、delete和cancel;
3).设置DataList控件各事件的处理程序,即在EditCommand、UpdateCommand、DeleteCommand、CancelCommand添加对应的处理函数名;
4).设置DataList的DataKeyField属性值,该值为某个字段名称,该属性可理解为每行的关键字段,在更新和删除时该属性值非常有用;
5).编写各事件处理程序;
3.示例代码
Default.aspx:
程序代码
<asp:DataList ID="DataList1" runat="server" OnCancelCommand="DataList1_Cancel" OnDeleteCommand="DataList1_Delete" OnEditCommand="DataList1_Edit" OnUpdateCommand="DataList1_Update" DataKeyField="ID">
<HeaderTemplate>
<!-- 头部 -->
</HeaderTemplate>
<ItemTemplate>
<div style="background-color:Red;"><%# Eval("LoginName")%>,<%# Eval("WebSite")%><asp:Button ID="Button1" CommandName="edit" runat="server"
Text="Edit" /><asp:Button ID="Button2" CommandName="delete" runat="server" Text="Delete" /></div>
</ItemTemplate>
<AlternatingItemTemplate>
<div style="background-color:Green;"><%# Eval("LoginName")%>,<%# Eval("WebSite")%><asp:Button ID="Button3" CommandName="edit" runat="server" Text="Edit" /><asp:Button ID="Button4" CommandName="delete" runat="server" Text="Delete" /></div>
</AlternatingItemTemplate>
<SeparatorTemplate>
<hr style="height:1px;" />
</SeparatorTemplate>
<SelectedItemTemplate>
<!-- 选中行样式 -->
</SelectedItemTemplate>
<EditItemTemplate>
<div><asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("LoginName") %>'></asp:TextBox><asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("WebSite") %>'></asp:TextBox>
<asp:Button
ID="Button7" CommandName="update" runat="server" Text="Update" /><asp:Button ID="Button8" CommandName="cancel" runat="server"
Text="Cancel" /></div>
</EditItemTemplate>
<FooterTemplate>
<!-- 底部 -->
</FooterTemplate>
</asp:DataList>
Default.aspx.cs:
程序代码
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
//private SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Sql"].ToString());
private SqlConnection conn = new SqlConnection("server=(local);uid=sa;pwd=sa;database=union_5k3g;");
protected void Page_Load(object sender, EventArgs e)
{
conn.Open();
if (!IsPostBack)
{
BindData();
}
}
protected void Page_UnLoad(object sender, EventArgs e)
{
conn = null;
}
private void BindData()
{
SqlDataAdapter sda = new SqlDataAdapter("Select Top 10 * From Union_User order By ID Desc", conn);
DataSet ds = new DataSet();
sda.Fill(ds, "Union_User");
DataList1.DataSource = ds.Tables["Union_User"].DefaultView;
DataList1.DataBind();
}
protected void DataList1_Cancel(object source, DataListCommandEventArgs e)
{
DataList1.EditItemIndex = -1;
BindData();
}
protected void DataList1_Delete(object source, DataListCommandEventArgs e)
{
string id = DataList1.DataKeys[e.Item.ItemIndex].ToString();
SqlCommand cmd = new SqlCommand("Delete From Union_User Where ID=" + id, conn);
cmd.ExecuteNonQuery();
DataList1.EditItemIndex = -1;
BindData();
}
protected void DataList1_Edit(object source, DataListCommandEventArgs e)
{
DataList1.EditItemIndex = e.Item.ItemIndex;
BindData();
}
protected void DataList1_Update(object source, DataListCommandEventArgs e)
{
string loginname = ((TextBox)e.Item.FindControl("TextBox1")).Text;
string website = ((TextBox)e.Item.FindControl("TextBox2")).Text;
string id = DataList1.DataKeys[e.Item.ItemIndex].ToString();
SqlCommand cmd = new SqlCommand("Update Union_User Set LoginName='" + loginname + "',WebSite='" + website + "' Where ID=" + id, conn);
cmd.ExecuteNonQuery();
DataList1.EditItemIndex = -1;
BindData();
}
}
4.注意事项
1).获取DataList模板中TextBox中的值必须使用"((TextBox)e.Item.FindControl("TextBox1")).Text",直接使用"TextBox1.Text"将出错提示:CS0103: 当前上下文中不存在名称“TextBox1”;