|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThis article describes adding a confirmation message to your server controls (even those inside your BackgroundI participate quite a bit on Experts-Exchange (ASP.NET), and always come across the question of how to attach a confirmation message to a Delete button, such as "Are you sure you want to delete this item?". While this is not a difficult task, it is a somewhat mundane one, and having to add an Using the codeThe first step is create the JavaScript method that will handle all of this. function confirmDelete(e) {
var targ;
if (!e) var e = window.event;
targ = (e.target) ? e.target : e.srcElement;
if (targ.nodeType == 3) targ = targ.parentNode;
if (targ.id.toLowerCase().indexOf("delete") >= 0) {
return confirm("Do you want to delete this item?");
}
routeEvent(e);
}
document.onclick = confirmDelete;
The The next step is to create a server control that gets affected by the logic above. <asp:Button RunAt="server" ID="MyDeleteButton1" Text="Delete Record" />
This delete button will pop-up the confirmation box simply because we have included the word "Delete" in the ID. No code was required, amazing! We're done. Now, imagine this with your <!-- ASP.NET 1.x -->
<asp:DataGrid RunAt="server" ID="GridView1">
<Columns>
... your other columns ...
<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton RunAt="server"
ID="DeleteMe" Text="Remove Row" />
</ItemTemplate>s
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
<!-- ASP.NET 2.0 -->
<asp:GridView RunAt="server" ID="GridView1">
<Columns>
... your other fields ...
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton RunAt="server"
ID="DeleteMe" Text="Remove Row" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
You could place the <script type="text/javascript">
... confirmDelete function ...
</script>
Or even better, would be to place it in your JavaScript external script file. Then, you could throw it in your Master Page setup and have this feature on every page. Another Method to ConsiderIf we didn't want to base everything on the ID of the server control, we could customize it to be assigned to the CSS function getElementsByClassName(ClassName,tagName,parentElement){
var elements=new Array();
var d=parentElement ? parentElement : document;
var allElements;
if(tagName)
allElements=d.all && d.all.tags(tagName)
|| d.getElementsByTagName && d.getElementsByTagName(tagName);
else allElements=d.all || d.getElementsByTagName("*");
for(var i=0,len=allElements.length; i<len; i++)
if(allElements[i].className.indexOf(ClassName)>=0)
elements[elements.length]=allElements[i];
return elements;
}
function assignConfirm(className) {
var elements = getElementsByClassName(className);
if (elements && elements.length > 0) {
for (i=0; i<elements.length; i++)
elements[i].onclick = confirmDelete;
}
}
function confirmDelete() {
return confirm("Are you sure you want to delete this item?");
}
Then <body onload="assignConfirm('delete');">
Finally, an example server control that utilizes this alternative method... <asp:LinkButton RunAt="server" ID="lbRemove" CssClass="delete" Text="Remove Item" />
That's it. Now we're able to assign the same functionality as before, but using CSS classes to control which controls get this feature. It'd be very easy to tweak this method to toggle multiple messages by simply changing the
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||