JavaScript Prompts for Buttons in Asp:DataGrid for Delete Column

原创 2004年09月29日 12:41:00
Aim : To JavaScript Prompts for Buttons in Asp::DataGrid for Delete Column

    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">
   <ItemStyle ForeColor="Black" BackColor="#DEDFDE">
   <HeaderStyle Font-Bold="True" ForeColor="#E7E7FF" BackColor="#4A3C8C">
   <FooterStyle ForeColor="Black" BackColor="#C6C3C6">
      <asp:BoundColumn DataField="ID"
      <asp:BoundColumn DataField="Name"
      <asp:BoundColumn DataField="Quantity"
      <asp:BoundColumn DataField="Price"
      <asp:ButtonColumn Text="Delete" ButtonType="PushButton" 
   <PagerStyle HorizontalAlign="Right" ForeColor="Black" BackColor="#C6C3C6">


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

void TestGrid_ItemCreated(object sender, DataGridItemEventArgs e)
   Button btn = (Button)e.Item.Cells[4].Controls[0];
     "return confirm('are you sure you want to delete this')");

<SCRIPT type=text/javascript><!-- google_ad_client = "pub-2092403601439285"; google_ad_width = 300; google_ad_height = 250; google_ad_format = "300x250_as"; google_ad_channel =""; google_ad_type = "text_image"; google_color_border = "2F74C1"; google_color_bg = "FFFFFF"; google_color_link = "0000FF"; google_color_url = "008000"; google_color_text = "000000"; //--></SCRIPT> <SCRIPT src="" type=text/javascript> </SCRIPT> <IFRAME name=google_ads_frame marginWidth=0 marginHeight=0 src=";dt=1096487011669&amp;lmt=1096280860&amp;format=300x250_as&amp;output=html&amp;;color_bg=FFFFFF&amp;color_text=000000&amp;color_link=0000FF&amp;color_url=008000&amp;color_border=2F74C1&amp;ad_type=text_image&amp;;u_h=768&amp;u_w=1024&amp;u_ah=738&amp;u_aw=1024&amp;u_cd=32&amp;u_tz=-420&amp;u_his=5&amp;u_java=true" frameBorder=0 width=300 scrolling=no height=250 allowTransparency><img height="1" width="1" border="0" src="" /></IFRAME>

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.

<!-- Client Side JavaScript -->
<script language="javascript">
   function ondeleteclick()
        return confirm("Are you sure you want to delete this?")
       var x = document.all.item(i)
       if(x!=null && !=null &&"TestGrid")==0)
                x.onclick = ondeleteclick

You can add more if comparsion statements for multiple grids on the page.

The article is property of NeuroSpeech and its free, everyone has right to distribute this article without modifying it. Every distribution of this article must contain this copyright notice.

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. bu...
  • zhiguo2010
  • zhiguo2010
  • 2010年07月18日 00:54
  • 356

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. Bu...
  • Jinglecat
  • Jinglecat
  • 2005年03月29日 01:34
  • 1264


最近写了几个asp.net的项目。其中,Asp.Net的控件DataGrid使用的最多了,前几天需要一个功能,我研究了很长时间,发现一个方法,应该是最简单的实现。现表述如下。 DataGrid的htm...
  • TomMax
  • TomMax
  • 2004年12月16日 19:40
  • 3071


to: Inyoureyes(最深的蓝) 通过各位的指点,DataGrid必须加上AutoGenerateColumns=false属性才能隐藏列     AutoGenerateColumns="F...
  • hs123
  • hs123
  • 2007年07月11日 21:29
  • 1953


  • li_yunpeng
  • li_yunpeng
  • 2009年05月21日 09:27
  • 1282


1,column中formatter函数中参数的含义 示例: val:kk2(和field属性对应) row:Object {id: "", memberId: "", loginname: "...
  • u012031380
  • u012031380
  • 2016年12月31日 16:30
  • 1129


  • jelink
  • jelink
  • 2006年08月25日 15:49
  • 1100


 版本:.NET Framework 1.1 DataGrid是ASP.NET中用于显示table类型数据的一个功能强大的控件,使用DataGrid控件可以轻松实现数据的分页显示及修改,并且拥有多种显...
  • colin915gao
  • colin915gao
  • 2007年04月22日 14:41
  • 2538


 问题:因为DataGridColumns不是可视树的一部分,他们不能使用datagrid的datacontext。 解决方法1:代理类 public class Binding...
  • xiudou_123
  • xiudou_123
  • 2017年05月08日 08:46
  • 377


  • jly4758
  • jly4758
  • 2013年11月27日 16:32
  • 8450
您举报文章:JavaScript Prompts for Buttons in Asp:DataGrid for Delete Column