站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Javascript 专题To JavaScript Prompts for Buttons in Asp::DataGrid for Delete Column(ZT)

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

摘要: To JavaScript Prompts for Buttons in Asp::DataGrid for Delete Column(ZT)
<textarea readonly style="border:none;font-family:Courier New;line-height:150%;width:760px;overflow-y:visible">

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. <table cellSpacing=1 cellPadding=5 width="100%" bgColor=#000000 border=0> <tr> <td bgColor=#fffff0> <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>
</td></tr></table>
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):
<table cellSpacing=1 cellPadding=5 width="100%" bgColor=#000000 border=0> <tr> <td bgColor=#fffff0>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')");
}
</td></tr></table>

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.
<table cellSpacing=1 cellPadding=5 width="100%" bgColor=#000000 border=0> <tr> <td bgColor=#fffff0> <td><input"TestGrid:_ctl2:_ctl0" value="Delete" /></td> </td></tr></table>
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:
<table cellSpacing=1 cellPadding=5 width="100%" bgColor=#000000 border=0> <tr> <td bgColor=#fffff0> function ondeleteclick()
{
return confirm("Are you sure you want to delete this?")
}

for(i=0;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>
</td></tr></table>

Reference Website: http://www.akashkava.com/articles/asp.net/datagrid/javascript-prompts.html


</textarea>
↑返回目录
前一篇: 在ASP.Net中应用Javascript
后一篇: 从 XSLT 样式表调用 JavaScript (转载 作者Nicholas Chase )