DataGrid小技巧

//添加删除确认对话框:
private void DataGrid1_ ItemDataBound(   )
{
   switch(e.Item.ItemType)
   {
    case ListItemType.Item:
    case ListItemType.AlternatingItem:
    case ListItemType.EditItem:
     ImageButton btn = (ImageButton)e.Item.FindControl("btnDelete");
     btn.Attributes.Add("onclick", "return confirm('你是否确定删除这条记录?');");
     break;
   
   }
  }
处理操作:
private void DataGrid1_ItemCommand()
  {
   string c=e.CommandName;
    int PermissionsID =(int)this.DataGrid1.DataKeys[e.Item.ItemIndex];//DataKeyField值
   switch(c)
   {
    case "Delete":
     //执行删除操作
     break;
 
   }
 
  }
 
还有个好方法:
datagrid-》属性生成器-》列-》添加按钮列-》删除-》文本(T)->在文本框里加上:
<div id="de" οnclick="JavaScript:return confirm('确定删除吗?')">删除</div>

就可以了/
OK!
 
//重新取值赋值:

private void DataGrid1_ItemDataBound(   )
{
   if((e.Item.ItemType==ListItemType.Item)||(e.Item.ItemType==ListItemType.AlternatingItem))
   {
    //e.Item代表一行
    
    string ImgUrl = ( string)DataBinder.Eval(e.Item.DataItem, "ImageUrl");
    //注意这一行,"ImageUrl"字段是什么类型前面就转成什么类型,否则报错
  
    //e.Item.Cells[0].Style.Add("font-weight", "bold");
    //e.Item.Cells[0].ForeColor = System.Drawing.Color.Red;        
    //e.Item.BackColor = System.Drawing.Color.AliceBlue; 

    string s=e.Item.DataItem.ToString();
    string ss=e.Item.Cells[0].Text;
    int sss=e.Item.Cells.Count;
 
   }
}
 
 
//绑定是重新计算页数与当前页
 
public void dataShowBind(DataSet ds)
{
   this.DataGrid1.DataSource=ds;
   if(ds.Tables.Count>0)
   {
        int rowscount=ds.Tables[0].Rows.Count;
        double p=double.Parse(rowscount.ToString())/5.0;
        //注意:只有double/double结果才是double,int/int=int;
    
        int newPageCount=int.Parse(Math.Ceiling(p).ToString());
  
        if(this.DataGrid1.CurrentPageIndex>newPageCount-1)
        {
             this.DataGrid1.CurrentPageIndex--;
        }
 
        this.DataGrid1.DataBind();
   }
   
 
}
 
列中的控件的事件首先反升到 DataGrid1_ItemCommand 事件,但要判断CommandName
private void TopicsGrid_ ItemCommand(e)
  {
   string btn=e.CommandName;
   int i=0;  
   switch(btn)
   {
    case "edit":
     i=(int)this.TopicsGrid.DataKeys[e.Item.ItemIndex];     
     break;
    case "delete":
     i=(int)this.TopicsGrid.DataKeys[e.Item.ItemIndex];
     
     break;
   }
  }
 
 
点击链接列弹出窗口
<Columns>
 <asp:TemplateColumn>
 <ItemTemplate>

     <a href="javascript:var aa=window.open('<%# "Form2.aspx?id=" + DataBinder.Eval(Container.DataItem,"持股名称")%>','','width=310,height=280,top=160,left=350');aa.focus();">
     <%#DataBinder.Eval(Container.DataItem,"持股名称")%>
 </a>

 </ItemTemplate>
 </asp:TemplateColumn>
</Columns>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值