数据绑定控件
数据绑定
DataBinder.Eval(Container.DataItem, “rowname”);
Repeater
ItemTemplate
AlternatingItemTemplate
HeaderTemplate
FooterTemplate
SeparatorTemplate
DataList
ItemTemplate
AlternatingItemTemplate
HeaderTemplate
FooterTemplate
SeparatorTemplate
EditItemTemplate edit事件触发
(objDataList.EditItemIndex=e.Item.ItemIndex)
SelectedItemTemplate select事件触发
(objDataList.SelectIndex=e.Item.ItemIndex)
This.objDataList.SelectedIndex =
e.Item.ItemIndex;
按钮CommandName
值select触发选中(该值随意但需e.CommandName
= “select”之类进行判断)
注:以下值为关键字不可自定义
值edit触发编辑事件
值update触发保存修改事件 常用于配合EditItemTemplate
值cancel取消 常用于配合EditItemTemplate
值delete触发删除
值cancel取消
值insert增加
Repeater DataList分页依赖 PageDataSoure对象
///
///Page_Load事件
///
if (!Page.IsPostBack)
{
this.Lable1.Text = "1";
this.dataBind2DataList();
}
private void dataBind2DataList()
{
int currPage = (int)this.Lable1.Text();
SqlConnection conn = DbHelp.CreateConn();
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = new SqlCommand("SELECT * FROM employes", conn);
DataSet ds = new DataSet();
sda.Fill(ds, "emp");
PageDataSource ps = new PAgeDataSource();
ps.DataSource = ds.Table["emp"].DefaultView;
ps.AllowPaging = true;
ps.PageSzie = 10;
//开始索引
ps.CurrentPageIndex = currPage - 1;
//按钮默认为可用
this.btnPre.Enbled = true;
this.btnNext.Enbled = true;
//根据当前页码判断按钮状态
if (currPage == 1)
{
this.btnPre.Enabled = false;
}
if (currPage == ps.PageCount)
{
this.btnNext.Enabled = false;
}
this.DataList1.DataSource = ps;
this.DataBind();
}
///
///按钮单击事件
///
this.Lable1.Text = ((int)this.Lable1.Text) - 1;
//务必重新绑定
this.dataBind2DataList();
GridView常用知识及函数
在增删改中需要利用主键
设定绑定时:
this.GridView1.DataKeyNames = new
String[] {“主键名”};
This.GridView1.DataSource=objDataDet.Table[“表名”];
This.GridView1.DataBind();
取用时:
this.GridView1.DataKeys[e.RowIndex].Value;
删除
(在RowDataBound中加入javascript弹出窗口确认是否删除)
方法1
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs
e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton objlink = (LinkButton)(e.Row.Cells[3].Controls[0]);
objlink.Attributes.Add("onclick", "return
confirm('确定删除?');");
}
}
方法2
protected void gvMain_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton lb = e.Row.FindControl("linb_Delete") as LinkButton;
lb.Attributes.Add("onclick", "return confirm('确认删除吗?')");
}
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs
e)
{
//该处仅仅为获得主键,删除方法直接调用该主键即可
Response.Write(this.GridView1.DataKeys[e.RowIndex].Value);
}
编辑(为不允许编辑的列在设计时选中
ReadOnly)
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs
e)
{
this.GridView1.EditIndex = e.NewEditIndex;
//该处调用数据绑定方法
}
取消
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs
e)
{
this.GridView1.EditIndex = -1;
//该处调用数据绑定方法
}
更新
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs
e)
{
//获取主键
String keyStr = this.GridView1.DataKeys[e.RowIndex].Value.ToString();
//获取当前行
GridViewRow gvr = this.GridView1.Rows[e.RowIndex];
//一个个获取更新值
String newName = ((TextBox)gvr.Cells[1].Controls[0]).Text;
String newAge = ((TextBox)gvr.Cells[1].Controls[0]).Text;
…
//调用sql
GridView1.EditIndex = -1;
//该处调用数据绑定方法
}
获取选中行中的控件状态或值(单选框 标签框等)
string sb =String.Empty;
for (int i = 0; i <
this.gvBooks.Rows.Count; i++)
{
CheckBox cb = (gvBooks.Rows[i].FindControl("chbSelect")) as CheckBox;
if (cb.Checked == true)
{
sb += (gvBooks.Rows[i].FindControl("lblId") as
Label).Text+",";
}
}
string catagory = this.ddlCatagory.SelectedItem.Value;
ChangeCatagory(sb, catagory); //实现更新图书分类的方法
多选
function GetAllCheckBox(CheckAll)
{
var
items = document.getElementsByTagName("input");
for(i=0;
i<items.length;i++)
{
if(items[i].type=="checkbox")
{
items[i].checked
= CheckAll.checked;
}
}
}
高亮(该事件通常做为表添加脚本用,比如为删除列加弹出窗口确认)
protected void gvDemo_RowDataBound(object
sender,GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover","currentcolor=this.style.backgroundColor;this.style.backgroundColor='#6699ff'");
e.Row.Attributes.Add("onmouseout","this.style.backgroundColor=currentcolor");
}
}
排序
protected void GridView1_Sorting(object
sender, GridViewSortEventArgs e)
{
if (ViewState["Order"] == null)
{
ViewState["Order"] = "ASC";
}
else
{
if (ViewState["Order"].ToString()
== "ASC")
{
ViewState["Order"] = "DESC";
}
else
{
ViewState["Order"] = "ASC";
}
}
SqlConnection conn = DB.createConn();
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand("SELECT
* FROM Student", conn);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(ds, "stu");
ds.Tables["stu"].DefaultView.Sort = e.SortExpression + "
" + ViewState["Order"].ToString();
this.GridView1.DataSource = ds.Tables["stu"];
this.GridView1.DataBind();
}