To JavaScript Prompts for Buttons in Asp::DataGrid for Delete Column(ZT)

look at the following code below. it has delete column, which we use to delete items in the grid. but delete is so careful action, we require the javascript alert to confirm the delete operation.

<asp:datagrid id="testgrid" 
    style="z-index: 102; left: 16px; position: absolute; top: 40px"
    runat="server" width="408px" height="160px" font-size="x-small"
    bordercolor="white" borderstyle="ridge" cellspacing="1" borderwidth="2px"
    backcolor="white" cellpadding="3" gridlines="none" autogeneratecolumns="false">

   <selecteditemstyle font-bold="true" forecolor="white" backcolor="#9471de">
   </selecteditemstyle>
 
   <itemstyle forecolor="black" backcolor="#dedfde">
   </itemstyle>
 
   <headerstyle font-bold="true" forecolor="#e7e7ff" backcolor="#4a3c8c">
   </headerstyle>
 
   <footerstyle forecolor="black" backcolor="#c6c3c6">
   </footerstyle>
 
   <columns>
      <asp:boundcolumn datafield="id"
            headertext="number"></asp:boundcolumn>
      <asp:boundcolumn datafield="name"
            headertext="name"></asp:boundcolumn>
      <asp:boundcolumn datafield="quantity"
            headertext="qty"></asp:boundcolumn>
      <asp:boundcolumn datafield="price"
            headertext="price"></asp:boundcolumn>
      <asp:buttoncolumn text="delete" buttontype="pushbutton" 
            commandname="delete"></asp:buttoncolumn>
   </columns>
 
   <pagerstyle horizontalalign="right" forecolor="black" backcolor="#c6c3c6">
   </pagerstyle>

</asp:datagrid>


to add javascript prompt command on click of delete button we can not directly modify the datagrid tag. and datagrid does not provide any facility to add alert code. however there is way to add an attribute to the controls. this way is very costly for server as far as performance is concerned. the old method is as shown below.

old slow method (not recommended):

private void initializecomponent()
{
   testgrid.itemcreated += new datagriditemeventhandler
                                  (testgrid_itemcreated);
}

void testgrid_itemcreated(object sender, datagriditemeventargs e)
{
   button btn = (button)e.item.cells[4].controls[0];
   btn.attributes.add("onclick",
     "return confirm('are you sure you want to delete this')");
}

the above code is recommended by various asp.net books which is very slow on performance since server delivers 100s of aspx files and executing this itemcreated
code executes for every rows in datagrid. just to add alert, i dont recommend to waste so much of cpu cycles of server.

why dont we apply some shortcut javascript code by having a bit of inside knowledge of how datagrid items are rendered.

datagrid items are rendered with table tag in html you can see the source of output in the browser. and delete button is replaced by input tag with type submit and it contains name as shown below.

  <td><input type="submit" name="testgrid:_ctl2:_ctl0" value="delete" /></td>

 

as you can see above the name attribute of button starts with "testgrid:".
so we can use this as guide and search all form elements and attach event of onclick in client browser and making server free from any code execution.

just add following script and see replace testgrid with name of grid. this prompts user for the alert and if user cancels nothing happens.

recommended:

<!-- client side javascript -->
<script language="javascript">
<!--
 
   function ondeleteclick()
   {
        return confirm("are you sure you want to delete this?")
   }
 
   for(i=0;i<document.all.length;i++)
   {
       var x = document.all.item(i)
       if(x!=null && x.name !=null &&  x.name.indexof("testgrid")==0)
       {
           if(x.value=="delete")
                x.onclick = ondeleteclick
           continue;
      }
 }
//-->
</script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值