Posted by: R Manimaran | February 13, 2007

ClientSide Confirmation in DataGrid

<%@Page Language=”VB” Debug=”True” %> <%@Import Namespace=”System.Data” %> <%@Import Namespace=”System.Data.OleDb” %> <script runat=”server”>

 <%————————————————————-%>

 Sub Page_Load()

If Not Page.IsPostback Then

 BindDataGrid()

 End If

 End Sub

 <%————————————————————-%>

Sub BindDataGrid()

‘ get DataReader for rows from Northwind tables

 Dim sSelect As String _ = “SELECT OrderID, Products.ProductID, ProductName, Quantity, ” _ & “CONVERT(decimal(5,2),[Order Details].UnitPrice) AS Price, ” _ & “Discount FROM [Order Details] JOIN Products ” _ & “ON [Order Details].ProductID = Products.ProductID ” _ & “WHERE OrderID = 10979”

 Dim sConnect As String _ = ConfigurationSettings.AppSettings(“NorthwindOleDbConnectString”)

Dim oConnect As New OleDbConnection(sConnect)

Try

 oConnect.Open()

Dim oCommand As New OleDbCommand(sSelect, oConnect)

 dgr1.DataSource = oCommand.ExecuteReader(CommandBehavior.CloseConnection) dgr1.DataBind()

Catch oErr As Exception

‘ be sure to close connection if error occurs

 If oConnect.State <> ConnectionState.Closed Then

 oConnect.Close()

End If

‘ display error message in page

lblErr.Text = oErr.Message

 End Try

End Sub

<%————————————————————-%>

Sub WireUpDeleteButton(source As Object, e As DataGridItemEventArgs)

 ‘ make sure this is an Item or AlternatingItem row

Dim oType As ListItemType = CType(e.Item.ItemType, ListItemType)

If oType = ListItemType.Item _ Or oType = ListItemType.AlternatingItem Then

 ‘ get ProductName value from this row

 Dim sName As String = e.Item.Cells(3).Text

‘ escape any single quotes

 sName = sName.Replace(“‘”, “\'”)

 ‘ get a reference to the Delete Button in this row

Dim oCtrl As Button = CType(e.Item.FindControl(“blnDelete”), Button)

‘ attach the client-side onclick event handler

oCtrl.Attributes.Add(“onclick”, “return ConfirmDelete(‘” & sName & “‘);”)

End If

End Sub

<%————————————————————-%>

Sub DoItemCommand(source As Object, e As DataGridCommandEventArgs) ‘

 get a reference to the data key values in this row

 Dim sOrderID As String = e.Item.Cells(1).Text

 Dim sProductID As String = e.Item.Cells(2).Text

‘ create a suitable SQL statement and execute it

 Dim sSQL As String sSQL = “DELETE From [Order Details] WHERE OrderID = ” & sOrderID _ & ” AND ProductID = ” & sProductID

ExecuteSQLStatement(sSQL)

‘ bind grid to display remaining rows

BindDataGrid()

End Sub

<%————————————————————-%>

 Sub ExecuteSQLStatement(sSQL)

‘ execute SQL statement against the original data source

Dim sConnect As String _ = ConfigurationSettings.AppSettings(“NorthwindOleDbConnectString”)

 Dim oConnect As New OleDbConnection(sConnect)

 Try

‘ display SQL statement in page

 lblErr.Text &= “Executing: ” & sSQL & “<p mce_keep=”true” mce_keep=”true”> </p>” oConnect.Open()

 Dim oCommand As New OleDbCommand(sSQL, oConnect)

If oCommand.ExecuteNonQuery() <> 1 Then

 lblErr.Text &= “ERROR: Could not update the selected row<p mce_keep=”true” mce_keep=”true”> </p>”

End If

 oConnect.Close()

Catch oErr As Exception ‘

 be sure to close connection if error occurs

If oConnect.State <> ConnectionState.Closed Then

 oConnect.Close()

 End If

‘ display error message in page

 lblErr.Text &= “ERROR: ” & oErr.Message & “<p mce_keep=”true” mce_keep=”true”> </p>”

 End Try

 End Sub

</script>

<%————————————————————-%>

<html>

<head>

 <title>Using a Client-side Confirmation Dialog</title>

<script language=’javascript’>

<!– function ConfirmDelete(sName)

{

var sMsg = ‘Are you sure you want to delete “‘ + sName + ‘”?’;

return (confirm(sMsg)); }

//–>

</script>

</head>

<body>

<span class=”heading”>Using a Client-side Confirmation Dialog</span>

<hr> <form id=”frmMain” runat=”server”>

<asp:DataGrid id=”dgr1″ runat=”server” Font-Size=”10″ Font-Name=”Tahoma,Arial,Helvetica,sans-serif” BorderStyle=”None” BorderWidth=”1px” BorderColor=”#deba84″ BackColor=”#DEBA84″ CellPadding=”5″ CellSpacing=”1″ DataKeyField=”ProductID” OnItemCommand=”DoItemCommand” OnItemDataBound=”WireUpDeleteButton”> <HeaderStyle Font-Bold=”True” ForeColor=”#ffffff” BackColor=”#b50055″> <ItemStyle BackColor=”#FFF7E7″ VerticalAlign=”Top”></ItemStyle> <AlternatingItemStyle backcolor=”#ffffc0″></AlternatingItemStyle> <Columns> <asp:TemplateColumn> <ItemTemplate> <asp:Button id=”blnDelete” Text=”Delete” CommandName=”Delete” runat=”server”></asp> </ItemTemplate> </asp:TemplateColumn> </Columns> </asp:DataGrid> <asp:Label id=”lblErr” EnableViewState=”False” runat=”server”></asp><p mce_keep=”true” mce_keep=”true”> </p>

</form>

<hr>

</body>

 </html>

Client Side Confirmation

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: