Tuesday, March 1, 2011

Gridview/Detailsview edit browse working sample c#

<%

@ Page Language="C#" %>

<!

DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<

script runat="server">

void CustomerDetail_ItemInserted(object sender,

DetailsViewInsertedEventArgs e)

{

CustomersView.DataBind();

}

void CustomerDetail_ItemInserting(object sender,

DetailsViewInsertEventArgs e)

{

for (int i = 0; i < e.Values.Count; i++)

{

if (e.Values[i] != null)

{

e.Values[i] = Server.HtmlEncode(e.Values[i].ToString());

}

}

}

void CustomerDetail_ItemUpdated(object sender,

DetailsViewUpdatedEventArgs e)

{

CustomersView.DataBind();

}

void CustomerDetail_ItemUpdating(object sender,

DetailsViewUpdateEventArgs e)

{

for (int i = 0; i < e.NewValues.Count; i++)

{

if (e.NewValues[i] != null)

{

e.NewValues[i] = Server.HtmlEncode(e.NewValues[i].ToString());

}

}

}

void CustomerDetail_ItemDeleted(object sender,

DetailsViewDeletedEventArgs e)

{

CustomersView.DataBind();

}

</

script>

<

html xmlns="http://www.w3.org/1999/xhtml" >

<

head id="Head1" runat="server">

<title>

DetailsView Example

</title>

</

head>

<

body>

<form id="Form1" runat="server">

<h3>

</h3>

<table cellspacing="10">

<tr>

<td>

<asp:GridView ID="CustomersView" DataSourceID="Customers"

AutoGenerateColumns="False"

DataKeyNames="CustomerID" runat="server">

<HeaderStyle BackColor="Blue" ForeColor="White" />

<Columns>

<asp:CommandField ShowSelectButton="True" />

<asp:BoundField DataField="ContactName"

HeaderText="ContactName" />

<asp:BoundField DataField="CompanyName"

HeaderText="CompanyName" />

</Columns>

</asp:GridView>

</td>

<td valign="top">

<asp:DetailsView ID="CustomerDetail"

DataSourceID="Details" AutoGenerateRows="false"

AutoGenerateInsertButton="true"

AutoGenerateEditButton="true"

AutoGenerateDeleteButton="true"

EmptyDataText=""

DataKeyNames="CustomerID" GridLines="Both"

OnItemInserted="CustomerDetail_ItemInserted"

OnItemInserting="CustomerDetail_ItemInserting"

OnItemUpdated="CustomerDetail_ItemUpdated"

OnItemUpdating="CustomerDetail_ItemUpdating"

OnItemDeleted="CustomerDetail_ItemDeleted"

runat="server">

<HeaderStyle BackColor="Navy" ForeColor="White" />

<RowStyle BackColor="White" />

<AlternatingRowStyle BackColor="LightGray" />

<EditRowStyle BackColor="LightCyan" />

<Fields>

<asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True" />

<asp:BoundField DataField="ContactName" HeaderText="ContactName" />

<asp:BoundField DataField="ContactTitle" HeaderText="ContactTitle" />

<asp:BoundField DataField="CompanyName" HeaderText="CompanyName" />

<asp:BoundField DataField="Address" HeaderText="Address" />

<asp:BoundField DataField="City" HeaderText="City" />

<asp:BoundField DataField="Region" HeaderText="Region" />

<asp:BoundField DataField="PostalCode" HeaderText="PostalCode" />

<asp:BoundField DataField="Country" HeaderText="Country" />

<asp:BoundField DataField="Phone" HeaderText="Phone" />

<asp:BoundField DataField="Fax" HeaderText="Fax" />

</Fields>

</asp:DetailsView>

</td>

</tr>

</table>

<asp:SqlDataSource ID="Customers" runat="server"

ConnectionString=

"<%$ ConnectionStrings:ConnectionString1 %>"

SelectCommand="SELECT [CompanyName], [ContactName], [CustomerID]

FROM [Customers]">

</asp:SqlDataSource>

<asp:SqlDataSource ID="Details"

ConnectionString=

"<%$ ConnectionStrings:ConnectionString1 %>"

runat="server"

SelectCommand="SELECT * FROM [Customers]

WHERE ([CustomerID] = @CustomerID)"

DeleteCommand="DELETE FROM [Customers]

WHERE [CustomerID] = @CustomerID"

InsertCommand="INSERT INTO [Customers] ([CustomerID],

[CompanyName], [ContactName], [ContactTitle], [Address],

[City], [Region], [PostalCode], [Country], [Phone], [Fax])

VALUES (@CustomerID, @CompanyName, @ContactName, @ContactTitle,

@Address, @City, @Region, @PostalCode, @Country, @Phone, @Fax)"

UpdateCommand="UPDATE [Customers] SET [CompanyName] = @CompanyName,

[ContactName] = @ContactName, [ContactTitle] = @ContactTitle,

[Address] = @Address, [City] = @City, [Region] = @Region,

[PostalCode] = @PostalCode, [Country] = @Country,

[Phone] = @Phone, [Fax] = @Fax

WHERE [CustomerID] = @CustomerID">

<SelectParameters>

<asp:ControlParameter ControlID="CustomersView"

Name="CustomerID" PropertyName="SelectedValue"

Type="String" />

</SelectParameters>

<DeleteParameters>

<asp:Parameter Name="CustomerID" Type="String" />

</DeleteParameters>

<UpdateParameters>

<asp:Parameter Name="CompanyName" Type="String" />

<asp:Parameter Name="ContactName" Type="String" />

<asp:Parameter Name="ContactTitle" Type="String" />

<asp:Parameter Name="Address" Type="String" />

<asp:Parameter Name="City" Type="String" />

<asp:Parameter Name="Region" Type="String" />

<asp:Parameter Name="PostalCode" Type="String" />

<asp:Parameter Name="Country" Type="String" />

<asp:Parameter Name="Phone" Type="String" />

<asp:Parameter Name="Fax" Type="String" />

<asp:Parameter Name="CustomerID" Type="String" />

</UpdateParameters>

<InsertParameters>

<asp:Parameter Name="CustomerID" Type="String" />

<asp:Parameter Name="CompanyName" Type="String" />

<asp:Parameter Name="ContactName" Type="String" />

<asp:Parameter Name="ContactTitle" Type="String" />

<asp:Parameter Name="Address" Type="String" />

<asp:Parameter Name="City" Type="String" />

<asp:Parameter Name="Region" Type="String" />

<asp:Parameter Name="PostalCode" Type="String" />

<asp:Parameter Name="Country" Type="String" />

<asp:Parameter Name="Phone" Type="String" />

<asp:Parameter Name="Fax" Type="String" />

</InsertParameters>

</asp:SqlDataSource>

</form>

</

body>

</

html>

 

 

No comments: