To JavaScript Prompts for Buttons in Asp::DataGrid for Delete Column(ZT)
2005-03-29 01:34
567 查看
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> |
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')"); } |
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> |
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> |
相关文章推荐
- To JavaScript Prompts for Buttons in Asp::DataGrid for Delete Column(ZT)
- JavaScript Prompts for Buttons in Asp:DataGrid for Delete Column
- One tip for javascript to invoke variables in asp.net web page
- How to Confirm a Delete in an ASP.NET Datagrid...
- (转)JavaScript: in, hasOwnProperty, delete, for/in
- The service cannot be activated because it does not support ASP.NET compatibility. ASP.NET compatibility is enabled for this application. Turn off ASP.NET compatibility mode in the web.config or add the AspNetCompatibilityRequirements attribute to the ser
- Cannot insert explicit value for identity column in table 'settings' when IDENTITY_INSERT is set to OFF.
- Using JavaScript and CSS to Enhance Your ASP.NET Datagrid
- How to use jquery or ajax to update razor partial view in c#/asp.net for a MVC project
- Cannot insert explicit value for identity column in table 'settings' when IDENTITY_INSERT is set to OFF.
- To set the div position(used for DataGrid Imeplate Column with DropDownList item)
- How to get Hidden Column Value in GridView(ASP.net GridView如何获取隐藏列的值)
- System.Data.SqlClient.SqlException: Cannot insert explicit value for identity column in table 'Food' when IDENTITY_INSERT is set to OFF
- how to use javascript to control the usercotrol in the asp.net
- JS:Trim() in javascript, how to define a function of checkinput for a WebControl(ascx)
- Multiple Column Dropdownlist for the ASP.NET DataGrid
- Add an onclick event in the DataGrid for any Column
- Cannot insert explicit value for identity column in table ‘table’ when IDENTITY_INSERT is set to OFF
- (转)JavaScript: in, hasOwnProperty, delete, for/in
- Yii 1.1: Using CButtonColumn to customize buttons in CGridView Follow @yiiframework