GridView中删除前提示框与编辑框长度设置的实现

最近这几天遇到了两个问题:在ASP.NET的gridview中,如何实现删除数据前弹出询问提示框与修改编辑框的长度?

  实现删除数据前弹出询问提示框主要有四中方法:

  一、在.aspx或.ascx文件中增加客户端JS脚本:

  把下面的代码直接加到.aspx或.ascx文件中即可,注意其中的“删除”二字根据自己的需要进行修改:

<script   language="JavaScript">  
function   delete_confirm(e){  
 if(event.srcElement.outerText=="删除"){
   event.returnValue=confirm("确定要删除该记录吗?");
 }  
}  
document.οnclick=delete_confirm;
</script>


  二、设置Text属性。

  删除按钮对应的Text属性为DeleteText,把该属性设置为“ <div οnclick="return confirm('确定要删除该记录吗?')">删除</div>”即可。

  三、编写事件代码实现。

  把下面的代码加到DataGrid的ItemCreated事件中(或者其他相关的事件,如ItemDataBound):

(一)VB.NET:
       Dim btnDel As System.Web.UI.WebControls.LinkButton
       If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
           btnDel = e.Item.Cells(1).Controls(0)
           btnDel.Attributes.Add("onclick", "return confirm('确定要删除该记录吗?')")
       End If

(二)C#:
       System.Web.UI.WebControls.LinkButton btnDel;
       If(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
       {
           btnDel = (System.Web.UI.WebControls.LinkButton)e.Item.Cells[1].Controls[0];
           btnDel.Attributes.Add("onclick", "return confirm('确定要删除该记录吗?')");
       }

(三)注意事项:
       其中btnDel的类型要注意,要与你的删除按钮类型一致。可分别设置为Button、LinkButton、ImageButton。
       Cells要设置以0开始的删除按钮所在列的索引。


  四、使用模板列(只限ASP.NET 2.0中的GridView):

  在ASP.NET 2.0中,GridView增加模板列也能实现这效果。我在ASP.NET 1.1的DataGrid中测试尚未成功,或许是我技术不行吧,希望有测试成功的人可以来分享下!

  先在GridView中增加一个模板列,如下:

引用
<asp:TemplateField HeaderText="Delete" ShowHeader="False">
<ItemStyle ForeColor="Red" />
<ItemTemplate>
 <asp:LinkButton ID="BtnDelete" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>


  然后给它加上客户端事件OnClientClick,事件执行代码为“ return confirm('确认要删除此行信息吗?')”。增加后的代码如下:

引用
<asp:TemplateField HeaderText="Delete" ShowHeader="False">
<ItemStyle ForeColor="Red" />
<ItemTemplate>
 <asp:LinkButton ID="BtnDelete" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete" OnClientClick="return confirm('确认要删除此行信息吗?')"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>


  DataGrid与GridView在编辑状态的时候,文本框的长度过长这是人所皆知的了,下面给出两个自定长度的方法:

  一、编写事件代码:

  在ItemDataBound事件中加入以下代码即可(只适合DataGrid,GridView的方法还没想出来):

(一)VB.NET
       Dim txtEdit As System.Web.UI.WebControls.TextBox;
       Dim i As Integer
       If e.Item.ItemType = ListItemType.EditItem Then
           For i = 0 To e.Item.Cells.Count - 1
               txtEdit = e.Item.Cells(i).Controls(0)
               txtEdit.Width = System.Web.UI.WebControls.Unit.Pixel(100)
           Next
       End If

(二)C#
       System.Web.UI.WebControls.TextBox txtEdit;
       Int i;
       If(e.Item.ItemType == ListItemType.EditItem)
       {
           for(i=0;i<e.Item.Cells.Count;i++)
           {
               txtEdit=e.Item.Cells[i].Controls[0];
               txtEdit.Width=System.Web.UI.WebControls.Unit.Pixel(100);
           }
       }

(三)注意事项
       以上只是示范代码,请根据自己的需要进行修改。有任何疑问的可以给我留言。


  二、使用模板列(只适合GridView)

  先在GridView中添加一个模板列,然后编辑该模板。里面有一个EditItemTemplate的模板,就是编辑状态的模板。
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值