Web Technologies

BLOG for Web Technologies

Freewares, Free E-Books Download, SEO, Tips, Tricks, Tweaks, Latest News, .Net, PHP, ASP, ASP.Net, CSP, MS SQL Server, MySQL, Database
earnptr.com
Friday, October 31, 2008
GridView Delete, with Confirmation
Introduction
Everyone likes a confirmation that lets them know that a record is being deleted. In this article, I will show you how you can prompt confirmation boxes when you delete a record from the GridView control.
Implementing the Confirmation Feature
The first thing that you need to do is to attach the JavaScript confirmation code to the delete column of the GridView control. This can be done in the Row_DataBound event of the GridView control. The Row_DataBound event is fired whenever the row is attached to the GridView. Hence, this is fired when the GridView is building for the first time or even when the page is reloaded.
Let's see the HTML part of the GridView code:
<asp:GridView DataKeyNames="CategoryID" ID="GridView1"
runat="server" AutoGenerateColumns="False"
OnRowCommand="GridView1_RowCommand"
OnRowDataBound="GridView1_RowDataBound"
OnRowDeleted="GridView1_RowDeleted" OnRowDeleting="GridView1_RowDeleting">
<columns>
<asp:boundfield datafield="CategoryID" headertext="CategoryID">
<asp:boundfield datafield="CategoryName" headertext="CategoryName">
<asp:templatefield headertext="Select">
<itemtemplate>
<asp:LinkButton ID="LinkButton1"
CommandArgument='<%# Eval("CategoryID") %>'
CommandName="Delete" runat="server">
Delete</asp:LinkButton>
</itemtemplate>
</asp:TemplateField>
</columns>
</asp:GridView>
As you can see from the above code, I have three columns in the GridView. Columns CategoryID and CategoryName are the bound columns, and the column Delete is a template column. The command argument is set as the CategoryID which means that whenever the LinkButton is clicked, it will pass CategoryID as an argument. The CommandName is set to "Delete".
The CommandName property is very important. If you have a LinkButton or a Button control inside the template column of the GridView control and the CommandName property is set to "Delete", then apart from GridView_RowCommand event, the GridView_Row_Deleting event is also fired.
Now, let's see the GridView_RowBound event where I attach the JavaScript code to every LinkButton.
protected void GridView1_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton l = (LinkButton)e.Row.FindControl("LinkButton1");
l.Attributes.Add("onclick", "javascript:return " +
"confirm('Are you sure you want to delete this record " +
DataBinder.Eval(e.Row.DataItem, "CategoryID") + "')");
}
}
In the above code, I checked whether the GridView row is a DataRow, and if it is, I simply attach some JavaScript code using the Attributes.Add method.
Catching the Primary Key of the Clicked Row
Now that you have successfully attached the JavaScript code to the GridView control, all that is left is to catch the primary key of the row which you have clicked so that you can perform further operations (like deleting the row). Remember what I said about a LinkButton or a Button control with the CommandName set to "Delete"? If you don't, read the text in the box again.
The CommandName property is very important. If you have a LinkButton or a Button control inside the template column of the GridView control and the CommandName property is set to "Delete", then apart from the GridView_RowCommand event, the GridView_Row_Deleting event is also fired.
Now, since our LinkButton's CommandName is set to "Delete", it means we have two choices of getting the primary key from the GridView. We can do this in the RowCommand event, or we can do this in the Row_Deleting event. I am going to show you both of them.
Catching the primary key in the RowCommand event
This is pretty simple. All you need to do is to get the value from the CommandArgument property which you have already set to the CategoryID.
protected void GridView1_RowCommand(object sender,
GridViewCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
// get the categoryID of the clicked row
int categoryID = Convert.ToInt32(e.CommandArgument);
// Delete the record
DeleteRecordByID(categoryID);
// Implement this on your own :)
}
}
e.CommandArgument returns object so you need to convert it to int as I have done above.
Catching the primary key in the Row_Deleting event
Let's see how we can catch the primary key of the clicked row in the Row_Deleting event.
runat="server" AutoGenerateColumns="False"
OnRowCommand="GridView1_RowCommand"
OnRowDataBound="GridView1_RowDataBound"
OnRowDeleted="GridView1_RowDeleted"
OnRowDeleting="GridView1_RowDeleting">protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int categoryID = (int) GridView1.DataKeys[e.RowIndex].Value;
DeleteRecordByID(categoryID);
}

In the above technique, you must set the DataKeyNames property of the GridView to "CategoryID". The GridView1.DataKeys[e.RowIndex].Value property gets the CategoryID out of the row which is clicked.

Labels: , ,

posted by WebTeks @ 3:49 AM   0 comments
Wednesday, October 1, 2008
Silverlight Tutorials and eBooks

DOWNLOAD EBOOK - Silverlight and ASP.NET Revealed (Apress)

Silverlight 1.1 is a revolutionary browser plug-in that allows developers to create rich web pages. Like Adobe Flash, Silverlight supports event handling, two-dimensional drawing, video playback, and animations. Unlike Flash, Silverlight is tailored to .NET developers. Most impressively, Silverlight 1.1 applications can execute pure C# code.
The most exciting part of Silverlight is its cross-platform support. When Silverlight 1.1 is released, it will support a range of modern web browsers (such as Internet Explorer, Firefox, Opera, and Safari), and it will run on a variety of operating systems (including Windows, Mac OS X, and Linux). Essentially, Silverlight 1.1 will be a scaled-down, browser-hosted version of .NET. Although Silverlight 1.1 is still a long way from release, it's already generating more interest than any new Microsoft technology since .NET 1.0. Silverlight and ASP.NET Revealed provides a valuable preview that explores the alpha release of Silverlight 1.1. In it, you'll examine how you can integrate Silverlight content in an ASP.NET application, and you'll get a head start on Microsoft's next great innovation.
DOWNLOAD EBOOK


"Silverlight ‘WPF/E’ First Steps"
http://dotnetslackers.com/articles/silverlight/SilverlightFirstStepsAnalogClock.aspx
Tutorial: “Silverlight ‘WPF/E’ First Steps: Getting Started with Simple Analog Clock,” by Muhammad Mosa. Starts with an introduction to WPF/E, then discusses creating a WPF/E page, drawing clock elements, defining a canvas, drawing the clock frame and body using Ellipse; drawing clock bars, hours, minutes and seconds bars, using JavaScript to access Silverlight objects, and using JavaScript to create the Silverlight object.


"Getting Started with Silverlight"
http://www.oreilly.com/catalog/9780596510688/
eBook: "Getting Started with Silverlight," by Shawn Wildemuth. Topics include: why Silverlight?, what is Silverlight?, working with Silverlight XAML, comparing Silverlight and WPF, development model, using Silverlight with ASP.NET, using tools, and finding examples in the world.

Labels: , , , ,

posted by WebTeks @ 2:30 AM   0 comments
Previous Post
Archives
Links
Template by

Free Blogger Templates

BLOGGER

Subscribe in NewsGator Online Subscribe in Rojo Add to Google Add to netvibes Subscribe in Bloglines Web Developement Blogs - BlogCatalog Blog Directory Blogarama - The Blog Directory Blog Directory & Search engine Computers Blogs - Blog Top Sites Top Computers blogs