DataList
1、数据绑定(将数据写成一个方法,方便数据绑定)
public class db
{
protected string connstring;
private int pagesize;
public db()
{
connstring = ConfigurationManager.ConnectionStrings["pubconn"].ConnectionString;
pagesize = 4;
}
public DataTable dt(string query) //Datalist 演示2
{
SqlConnection con = new SqlConnection(connstring);
SqlDataAdapter sda = new SqlDataAdapter(query,con);
DataSet ds = new DataSet();
sda.Fill(ds, "name");
return ds.Tables["name"];
}
{
protected string connstring;
private int pagesize;
public db()
{
connstring = ConfigurationManager.ConnectionStrings["pubconn"].ConnectionString;
pagesize = 4;
}
public DataTable dt(string query) //Datalist 演示2
{
SqlConnection con = new SqlConnection(connstring);
SqlDataAdapter sda = new SqlDataAdapter(query,con);
DataSet ds = new DataSet();
sda.Fill(ds, "name");
return ds.Tables["name"];
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
private void bind()
{
db sdb = new db();
DataList1.DataSource = sdb.dt("select top 10 * from authors");
DataList1.DataKeyField = "au_id";
DataList1.DataBind();
}
{
if (!IsPostBack)
{
bind();
}
}
private void bind()
{
db sdb = new db();
DataList1.DataSource = sdb.dt("select top 10 * from authors");
DataList1.DataKeyField = "au_id";
DataList1.DataBind();
}
2、DataList总结
protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
{
DataList1.EditItemIndex = e.Item.ItemIndex;
bind();
}
protecte void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
{
string id = DataList1.DataKeys[e.Item.ItemIndex].ToString();
db sdb = new db();
string delete = "delete from authors where au_id='" + id + "'";
if (sdb.sql(delete) > 0)
{
Response.Write("<script>alert('删除成功')</script>");
DataList1.EditItemIndex = -1;
bind();
}
}
protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
{
DataList1.EditItemIndex = -1;
bind();
}
protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
{
string id = DataList1.DataKeys[e.Item.ItemIndex].ToString();
string fname = ((TextBox)e.Item.FindControl("textbox1")).Text;
string city = ((TextBox)e.Item.FindControl("textbox2")).Text;
db sdb = new db();
string update = "update authors set au_fname='" + fname + "',city='" + city + "'where au_id='" + id+"'";
if (sdb.sql(update) > 0)
{
DataList1.EditItemIndex = -1;
bind();
}
}
1、 DataList1.DataKeyField = "au_id"; 设置DataList1的主键为au_id{
DataList1.EditItemIndex = e.Item.ItemIndex;
bind();
}
protecte void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
{
string id = DataList1.DataKeys[e.Item.ItemIndex].ToString();
db sdb = new db();
string delete = "delete from authors where au_id='" + id + "'";
if (sdb.sql(delete) > 0)
{
Response.Write("<script>alert('删除成功')</script>");
DataList1.EditItemIndex = -1;
bind();
}
}
protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
{
DataList1.EditItemIndex = -1;
bind();
}
protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
{
string id = DataList1.DataKeys[e.Item.ItemIndex].ToString();
string fname = ((TextBox)e.Item.FindControl("textbox1")).Text;
string city = ((TextBox)e.Item.FindControl("textbox2")).Text;
db sdb = new db();
string update = "update authors set au_fname='" + fname + "',city='" + city + "'where au_id='" + id+"'";
if (sdb.sql(update) > 0)
{
DataList1.EditItemIndex = -1;
bind();
}
}
2、 DataList1.EditItemIndex = e.Item.ItemIndex; (编辑向索引设置为当前索引)e.Item.ItemIndex时间触发者当前集合的索引
3、 string id = DataList1.DataKeys[e.Item.ItemIndex].ToString(); DataKeys[]数据的主键索引[]中是int
4、 string fname = ((TextBox)e.Item.FindControl("textbox1")).Text; 寻找控件名称为“textbox1”的控件,转为textbox类型,获取其Text的内容
5、 e.Item.ItemType == ListItemType.Footer 验证集合类型是不是脚模板
6、 PlaceHolder ph = ((PlaceHolder)e.Item.FindControl("ph")); 寻找,并实例化名为ph的控件为ph
7、 hl.NavigateUrl = "?page=" + i;
Request.QueryString["Page"] 获取回发的page的值