To JavaScript Prompts for Buttons in Asp :: DataGrid for delete column (zt)

xiaoxiao2021-03-06  18

Look at The Following Code Below. It has delete column, Which We Use to delete Items in The Grid. But Revete Is So Careful Action, We Require The JavaScript alert to confirm the delete operation.

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", "'Are you have 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 ItemCreatedcode 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 TD>

As you can see Above The name attribute of button starts with "testgrid:".

SOW We can use this as guide and search all form elements and attach events.

.

Recommended: