技术开发 频道

ASP.NET常用语句33条代码

  29.DataGrid使用:

添加删除确认:

private void DataGrid1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
 foreach(DataGridItem di in this.DataGrid1.Items)
 {
  
if(di.ItemType==ListItemType.Item||di.ItemType==ListItemType.AlternatingItem)
  {
   ((LinkButton)di.Cells[
8].Controls[0]).Attributes.Add("onclick","return confirm(’确认删除此项吗?’);");
  }
 }
}
  样式交替:

ListItemType itemType
= e.Item.ItemType;

if (itemType == ListItemType.Item )
{
 e.Item.Attributes[
"onmouseout"] = "javascript:this.style.backgroundColor=’#FFFFFF’;";
 e.Item.Attributes[
"onmouseover"] = "javascript:this.style.backgroundColor=’#d9ece1’;cursor=’hand’;" ;
}
else if( itemType == ListItemType.AlternatingItem)
{
 e.Item.Attributes[
"onmouseout"] = "javascript:this.style.backgroundColor=’#a0d7c4’;";
 e.Item.Attributes[
"onmouseover"] = "javascript:this.style.backgroundColor=’#d9ece1’;cursor=’hand’;" ;
}
  添加一个编号列:

DataTable dt
= c.ExecuteRtnTableForAccess(sqltxt); //执行sql返回的DataTable
DataColumn dc
=dt.Columns.Add("number",System.Type.GetType("System.String"));

for(int i=0;i<dt.Rows.Count;i++)
{
 dt.Rows[i][
"number"]=(i+1).ToString();
}

DataGrid1.DataSource
=dt;
DataGrid1.DataBind();
  DataGrid1中添加一个CheckBox,页面中添加一个全选框

private void CheckBox2_CheckedChanged(object sender, System.EventArgs e)
{
 foreach(DataGridItem thisitem in DataGrid1.Items)
 {
  ((CheckBox)thisitem.Cells[
0].Controls[1]).Checked=CheckBox2.Checked;
 }
}
  将当前页面中DataGrid1显示的数据全部删除

foreach(DataGridItem thisitem in DataGrid1.Items)
{
 
if(((CheckBox)thisitem.Cells[0].Controls[1]).Checked)
 {
  
string strloginid= DataGrid1.DataKeys[thisitem.ItemIndex].ToString();
  Del (strloginid);
//删除函数
 }
}
0
相关文章