a complete sample: detailview callbacks editing and paging

Detailview.aspx:

<%@ Page Language=”C#” AutoEventWireup=”true” CodeBehind=”DetailView.aspx.cs” Inherits=”testweb2.DDD” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt;

<html xmlns=”http://www.w3.org/1999/xhtml&#8221; >
<head runat=”server”>
<title>Detailsview with callback</title>

<script language=”javascript”>

function Pager(page)
{
<%= ClientScript.GetCallbackEventReference(this, “page”, “ShowResult”, null) %>;
}

function ShowResult(val, ctx)
{
document.getElementById(‘dv’).innerHTML = val;
}
</script>

<script type=”text/javascript”>
function CallServer(str)
{
var product = str;
<%= ClientScript.GetCallbackEventReference(this, “product”, “ReceiveServerData”,null)%>;
}

function ReceiveServerData(rValue)
{
document.getElementById(‘dv’).innerHTML = rValue;
}

</script>
</head>
<body>
<form id=”form1″ runat=”server”>
<div id=”dv”>
<asp:DetailsView ID=”DetailsView1″ runat=”server” Height=”50px” Width=”288px” BackColor=”White” BorderColor=”#E7E7FF” BorderStyle=”None” BorderWidth=”1px” CellPadding=”3″ DataSourceID=”SqlDataSource1″ AllowPaging=”True” AutoGenerateRows=”False” DataKeyNames=”countryid” OnDataBound=”DetailsView1_DataBound”>
<FooterStyle BackColor=”#B5C7DE” ForeColor=”#4A3C8C” />
<RowStyle BackColor=”#E7E7FF” ForeColor=”#4A3C8C” />
<PagerStyle BackColor=”#E7E7FF” ForeColor=”#4A3C8C” HorizontalAlign=”Right” />
<HeaderStyle BackColor=”#4A3C8C” Font-Bold=”True” ForeColor=”#F7F7F7″ />
<EditRowStyle BackColor=”#738A9C” Font-Bold=”True” ForeColor=”#F7F7F7″ />
<AlternatingRowStyle BackColor=”#F7F7F7″ />
<Fields>
<asp:BoundField DataField=”countryid” HeaderText=”countryid” InsertVisible=”False”
ReadOnly=”True” SortExpression=”countryid” />
<asp:BoundField DataField=”countryname” HeaderText=”countryname” SortExpression=”countryname” />
<asp:TemplateField ShowHeader=”False”>
<EditItemTemplate>
<asp:LinkButton ID=”LinkButtonUpdate” runat=”server” CausesValidation=”True” CommandName=”Update”
Text=”Update”></asp:LinkButton>
<asp:LinkButton ID=”LinkButtonCancel” runat=”server” CausesValidation=”False” CommandName=”Cancel”
Text=”Cancel”></asp:LinkButton>
</EditItemTemplate>
<InsertItemTemplate>
<asp:LinkButton ID=”LinkButtonInsert” runat=”server” CausesValidation=”True” CommandName=”Insert”
Text=”Insert”></asp:LinkButton>
<asp:LinkButton ID=”LinkButtonCancel” runat=”server” CausesValidation=”False” CommandName=”Cancel”
Text=”Cancel”></asp:LinkButton>
</InsertItemTemplate>
<ItemTemplate>
<asp:LinkButton ID=”LinkButtonEdit” runat=”server” CausesValidation=”False” CommandName=”Edit”
Text=”Edit”></asp:LinkButton>
<asp:LinkButton ID=”LinkButtonNew” runat=”server” CausesValidation=”False” CommandName=”New”
Text=”New”></asp:LinkButton>
<asp:LinkButton ID=”LinkButtonDelete” runat=”server” CausesValidation=”False” CommandName=”Delete”
Text=”Delete”></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>

</div>

<asp:SqlDataSource ID=”SqlDataSource1″ runat=”server”
ConnectionString=”<%$ ConnectionStrings:tttConnectionString %>”
DeleteCommand=”DELETE FROM [country] WHERE [countryid] = @original_countryid AND [countryname] = @original_countryname”
InsertCommand=”INSERT INTO [country] ([countryname]) VALUES (@countryname)”
SelectCommand=”SELECT * FROM [country]”
UpdateCommand=”UPDATE [country] SET [countryname] = @countryname WHERE [countryid] = @original_countryid AND [countryname] = @original_countryname” OldValuesParameterFormatString=”original_{0}” ProviderName=”System.Data.SqlClient”>
<DeleteParameters>
<asp:ControlParameter ControlID=”DetailsView1″ Name=”original_countryid” PropertyName=”SelectedValue”
Type=”Int32″ />
<asp:ControlParameter ControlID=”DetailsView1″ Name=”original_countryname” PropertyName=”SelectedValue”
Type=”String” />
</DeleteParameters>
<UpdateParameters>
<asp:ControlParameter ControlID=”DetailsView1″ Name=”countryname” PropertyName=”SelectedValue”
Type=”String” />
<asp:ControlParameter ControlID=”DetailsView1″ Name=”original_countryid” PropertyName=”SelectedValue”
Type=”Int32″ />
<asp:ControlParameter ControlID=”DetailsView1″ Name=”original_countryname” PropertyName=”SelectedValue”
Type=”String” />
</UpdateParameters>
<InsertParameters>
<asp:ControlParameter ControlID=”DetailsView1″ Name=”countryname” PropertyName=”SelectedValue”
Type=”String” />
</InsertParameters>
</asp:SqlDataSource>

</form>
</body>
</html>
CodeBehind:DetailView.aspx.cs:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

using System.Data.SqlClient;
using System.Data.Common;

namespace testweb2
{
public partial class DDD : System.Web.UI.Page, ICallbackEventHandler
{

#region ICallbackEventHandler
public string GetCallbackResult()
{
StringWriter sr = new StringWriter();
HtmlTextWriter htm = new HtmlTextWriter(sr);
this.DetailsView1.RenderControl(htm);
htm.Close();
sr.Close();
return sr.ToString();

}

public void RaiseCallbackEvent(string eventArgument)
{
string[] command = eventArgument.Split(new char[] { ‘,’ });
string work = command[0];
if (work == “Edit”)
{
this.DetailsView1.ChangeMode(DetailsViewMode.Edit);
}

if (work == “Delete”)
{
this.DetailsView1.DeleteItem();
this.SqlDataSource1.Delete();
}
else if (work == “New”)
{
this.DetailsView1.ChangeMode(DetailsViewMode.Insert);
}
else if (work == “Insert”)
{
this.DetailsView1.InsertItem(true);
}
else if (work == “Update”)
{
this.DetailsView1.ChangeMode(DetailsViewMode.Edit);
this.DetailsView1.UpdateItem(true);
}
else
{
if (work.Split(new char[] { ‘|’ })[0] == “page”)
{
this.DetailsView1.PageIndex = int.Parse((work.Split(new char[] { ‘|’ })[1])) – 1;
}
}
this.DetailsView1.DataBind();
}

#endregion

protected void DetailsView1_DataBound(object sender, EventArgs e)
{

object my = this.DetailsView1;

foreach (TableRow row in this.DetailsView1.Rows)
{

LinkButton btnEdit = row.FindControl(“LinkButtonEdit”) as LinkButton;
if (btnEdit != null) btnEdit.Attributes.Add(“onclick”, “CallServer(‘Edit’); return false;”);

LinkButton btnNew = row.FindControl(“LinkButtonNew”) as LinkButton;
if (btnNew != null) btnNew.Attributes.Add(“onclick”, “CallServer(‘New’); return false;”);

LinkButton btnDelete = row.FindControl(“LinkButtonDelete”) as LinkButton;
if (btnDelete != null) btnDelete.Attributes.Add(“onclick”, “CallServer(‘Delete’); return false;”);

LinkButton btnInsert = row.FindControl(“LinkButtonInsert”) as LinkButton;
if (btnInsert != null) btnInsert.Attributes.Add(“onclick”, “CallServer(‘Insert’); return false;”);

LinkButton btnUpdate = row.FindControl(“LinkButtonUpdate”) as LinkButton;
if (btnUpdate != null) btnUpdate.Attributes.Add(“onclick”, “CallServer(‘Update’); return false;”);

LinkButton btnCancel = row.FindControl(“LinkButtonCancel”) as LinkButton;
if (btnCancel != null) btnCancel.Attributes.Add(“onclick”, “CallServer(‘Cancel’); return false;”);

}

if (this.DetailsView1.BottomPagerRow.Controls[0].HasControls())
{
TableRow myrow = (TableRow)this.DetailsView1.BottomPagerRow.Controls[0].Controls[0].Controls[0];

foreach (TableCell cell in myrow.Cells)
{
if (cell.HasControls())
{
LinkButton button = cell.Controls[0] as LinkButton;
if (button != null)
{
button.Attributes.Add(“onclick”, “Pager(‘page|” + button.Text + “‘); return false;”);
}
}
}
}
}
}
}