ASP.NET 4.5 GridView and DetailsView

1.6k views Asked by At

I am trying to work on Chapter 15 exercise from Murach’s ASP.NET 4.5 web programming with C# 2012.

Here is my question.

The webpage has Grid View on the left and Details View on the right.

When I update Ahmed to Ahmad from DetailViews,

the update is not reflected in GridView.

I have to go to the next page and then come back to previous page

in order for the update to display.

I am wondering if there is any solution.

Thank you for your help!

By the way, the code is copied from the book.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Chapter 15: Customer Maintenance</title>
    <link href="Main.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <header>
        <img src="Images/banner.jpg" "Halloween Store" />
    </header>
    <section>
    <form id="form1" runat="server">
        <div id="gridview">
            <asp:GridView ID="GridView1" runat="server" AllowPaging="True"
                DataKeyNames="Email" DataSourceID="SqlDataSource1"
                SelectedIndex="0"
                AutoGenerateColumns="False" CellPadding="4" GridLines="None" 
                ForeColor="Black" Width="320px" >
                <Columns>
                    <asp:BoundField DataField="Email" HeaderText="Email"
                        ReadOnly="True" Visible="False">
                    </asp:BoundField>
                    <asp:BoundField DataField="LastName" HeaderText="LastName">
                        <HeaderStyle HorizontalAlign="Left" />
                        <ItemStyle Width="150px" />
                    </asp:BoundField>
                    <asp:BoundField DataField="FirstName" HeaderText="FirstName">
                        <HeaderStyle HorizontalAlign="Left" />
                        <ItemStyle Width="120px" />
                    </asp:BoundField>
                    <asp:CommandField ButtonType="Button" ShowSelectButton="True" />
                </Columns>
                <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
                <RowStyle BackColor="White" ForeColor="Black" />
                <AlternatingRowStyle BackColor="#E3EAEB" ForeColor="Black" />
                <SelectedRowStyle BackColor="#F46D11" ForeColor="White" />
                <PagerSettings Mode="NextPreviousFirstLast" />
                <PagerStyle BackColor="#1C5E55" ForeColor="White"
                    HorizontalAlign="Center" />
            </asp:GridView>

            <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:HalloweenConnectionString %>" 
                OldValuesParameterFormatString ="original_{0}"

                ConflictDetection="CompareAllValues" 

                DeleteCommand="DELETE FROM [Customers] 
                WHERE [Email] = @original_Email 
                AND [LastName] = @original_LastName 
                AND [FirstName] = @original_FirstName 
                AND [Address] = @original_Address            
                AND [City] = @original_City             
                AND [State] = @original_State             
                AND [ZipCode] = @original_ZipCode             
                AND [PhoneNumber] = @original_PhoneNumber"



InsertCommand="INSERT INTO [Customers] ([Email], [LastName], [FirstName], [Address], [City], [State], [ZipCode], [PhoneNumber]) 

                VALUES (@Email, @LastName, @FirstName, @Address, @City, @State, @ZipCode, @PhoneNumber)"

SelectCommand="SELECT [Email], [LastName], [FirstName], [Address], [City], [State], [ZipCode], [PhoneNumber]
                FROM [Customers] WHERE ([Email] = @Email)"

UpdateCommand="UPDATE [Customers] SET [Email] = @Email, 
                [LastName] = @LastName,
                [FirstName]= @FirstName,
                [Address] = @Address, [City] = @City, [State] = @State, [ZipCode] = @ZipCode, 
                [PhoneNumber] = @PhoneNumber
                WHERE [Email] = @original_Email 

                AND [LastName] = @original_LastName 

                AND [FirstName] = @original_FirstName

                AND [Address] = @original_Address AND [City] = @original_City AND [State] = @original_State AND [ZipCode] = @original_ZipCode 

                AND [PhoneNumber] = @original_PhoneNumber">


<SelectParameters>
<asp:ControlParameter ControlID="GridView1" Name="Email" PropertyName="SelectedValue" Type="String"/>
</SelectParameters>

<DeleteParameters>
<asp:Parameter Name="original_Email" Type="String" />
<asp:Parameter Name="original_LastName" Type="String" />
<asp:Parameter Name="original_FirstName" Type="String" />
<asp:Parameter Name="original_Address" Type="String" />
<asp:Parameter Name="original_City" Type="String" />
<asp:Parameter Name="original_State" Type="String" />
<asp:Parameter Name="original_ZipCode" Type="String" />
<asp:Parameter Name="original_PhoneNumber" Type="String" />

</DeleteParameters>
<UpdateParameters>


<asp:Parameter Name="Email" Type="String" />
<asp:Parameter Name="LastName" Type="String" />
<asp:Parameter Name="FirstName" Type="String" />
<asp:Parameter Name="Address" Type="String" />
<asp:Parameter Name="City" Type="String" />
<asp:Parameter Name="State" Type="String" />
<asp:Parameter Name="ZipCode" Type="String" />
<asp:Parameter Name="PhoneNumber" Type="String" />

<asp:Parameter Name="original_Email" Type="String" />
<asp:Parameter Name="original_LastName" Type="String" />
<asp:Parameter Name="original_FirstName" Type="String" />
<asp:Parameter Name="original_Address" Type="String" />
<asp:Parameter Name="original_City" Type="String" />
<asp:Parameter Name="original_State" Type="String" />
<asp:Parameter Name="original_ZipCode" Type="String" />
<asp:Parameter Name="original_PhoneNumber" Type="String" />

</UpdateParameters>


<InsertParameters>

<asp:Parameter Name="Email" Type="String" />
<asp:Parameter Name="LastName" Type="String" />
<asp:Parameter Name="FirstName" Type="String" />

<asp:Parameter Name="Address" Type="String" />
<asp:Parameter Name="City" Type="String" />
<asp:Parameter Name="State" Type="String" />
<asp:Parameter Name="ZipCode" Type="String" />
<asp:Parameter Name="PhoneNumber" Type="String" />

</InsertParameters>







                </asp:SqlDataSource>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server"
                ConnectionString="<%$ ConnectionStrings:HalloweenConnectionString %>"
                SelectCommand="SELECT [Email], [LastName], [FirstName] 
                               FROM [Customers] ORDER BY [LastName]">
            </asp:SqlDataSource>
        </div>
        <div id="detailsview">
            <asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="363px" AutoGenerateRows="False" DataKeyNames="Email" DataSourceID="SqlDataSource2" style="margin-left: 0px">
                <Fields>

                    <asp:TemplateField HeaderText="Email">

                        <ItemTemplate>

                            <asp:Label ID="lblEmailIT" runat="server" Text='<%# Bind("Email") %>'> 

                            </asp:Label>

                        </ItemTemplate>

                        <EditItemTemplate>
                            <asp:TextBox ID="lblEmailET" runat="server" Text='<%# Bind("Email") %>'>
                                </asp:TextBox>

                        </EditItemTemplate>

                        <InsertItemTemplate>
                            <asp:TextBox ID="txtEmailInT" runat="server" Text='<%# Bind("Email") %>'>
                            </asp:TextBox>

                            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtEmailInT" ErrorMessage="Email is a required field.">

                            </asp:RequiredFieldValidator>


                        </InsertItemTemplate>

                        <HeaderStyle HorizontalAlign="Left" Width="100px" />
                        <ItemStyle Width="200px" />

                    </asp:TemplateField>



                    <asp:TemplateField HeaderText="LastName">

                        <ItemTemplate>

                            <asp:Label ID="lblLastNameIT" runat="server" Text='<%# Bind("LastName") %>'> 

                            </asp:Label>

                        </ItemTemplate>

                        <EditItemTemplate>
                            <asp:TextBox ID="lblLastNameET" runat="server" Text='<%# Bind("LastName") %>'>
                                </asp:TextBox>

                        </EditItemTemplate>

                        <InsertItemTemplate>
                            <asp:TextBox ID="lblLastNameInT" runat="server" Text='<%# Bind("LastName") %>'>
                            </asp:TextBox>

                        </InsertItemTemplate>

                        <HeaderStyle HorizontalAlign="Left" Width="100px" />
                        <ItemStyle Width="200px" />

                    </asp:TemplateField>


                     <asp:TemplateField HeaderText="FirstName">

                        <ItemTemplate>

                            <asp:Label ID="lblFirstNameIT" runat="server" Text='<%# Bind("FirstName") %>'> 

                            </asp:Label>

                        </ItemTemplate>

                        <EditItemTemplate>
                            <asp:TextBox ID="lblFirstNameET" runat="server" Text='<%# Bind("FirstName") %>'>
                                </asp:TextBox>

                        </EditItemTemplate>

                        <InsertItemTemplate>
                            <asp:TextBox ID="lblFirstNameInT" runat="server" Text='<%# Bind("FirstName") %>'>
                            </asp:TextBox>

                        </InsertItemTemplate>

                        <HeaderStyle HorizontalAlign="Left" Width="100px" />
                        <ItemStyle Width="200px" />

                    </asp:TemplateField>


                    <asp:TemplateField HeaderText="Address">

                        <ItemTemplate>

                            <asp:Label ID="lblAddressIT" runat="server" Text='<%# Bind("Address") %>'> 

                            </asp:Label>

                        </ItemTemplate>

                        <EditItemTemplate>
                            <asp:TextBox ID="lblAddressET" runat="server" Text='<%# Bind("Address") %>'>
                                </asp:TextBox>

                        </EditItemTemplate>

                        <InsertItemTemplate>
                            <asp:TextBox ID="lblAddressInT" runat="server" Text='<%# Bind("Address") %>'>
                            </asp:TextBox>

                        </InsertItemTemplate>

                        <HeaderStyle HorizontalAlign="Left" Width="100px" />
                        <ItemStyle Width="200px" />

                    </asp:TemplateField>




                    <asp:TemplateField HeaderText="City">

                        <ItemTemplate>

                            <asp:Label ID="lblCityIT" runat="server" Text='<%# Bind("City") %>'> 

                            </asp:Label>

                        </ItemTemplate>

                        <EditItemTemplate>
                            <asp:TextBox ID="lblCityET" runat="server" Text='<%# Bind("City") %>'>
                                </asp:TextBox>

                        </EditItemTemplate>

                        <InsertItemTemplate>
                            <asp:TextBox ID="lblCityInT" runat="server" Text='<%# Bind("City") %>'>
                            </asp:TextBox>

                        </InsertItemTemplate>

                        <HeaderStyle HorizontalAlign="Left" Width="100px" />
                        <ItemStyle Width="200px" />

                    </asp:TemplateField>




                    <asp:TemplateField HeaderText="State">

                        <ItemTemplate>

                            <asp:Label ID="lblStateIT" runat="server" Text='<%# Bind("State") %>'> 

                            </asp:Label>

                        </ItemTemplate>

                        <EditItemTemplate>
                            <asp:TextBox ID="lblStateET" runat="server" Text='<%# Bind("State") %>'>
                                </asp:TextBox>

                        </EditItemTemplate>

                        <InsertItemTemplate>
                            <asp:TextBox ID="lblStateInT" runat="server" Text='<%# Bind("State") %>'>
                            </asp:TextBox>

                        </InsertItemTemplate>

                        <HeaderStyle HorizontalAlign="Left" Width="100px" />
                        <ItemStyle Width="200px" />

                    </asp:TemplateField>



                    <asp:TemplateField HeaderText="ZipCode">

                        <ItemTemplate>

                            <asp:Label ID="lblZipCodeIT" runat="server" Text='<%# Bind("ZipCode") %>'> 

                            </asp:Label>

                        </ItemTemplate>

                        <EditItemTemplate>
                            <asp:TextBox ID="lblZipCodeET" runat="server" Text='<%# Bind("ZipCode") %>'>
                                </asp:TextBox>

                        </EditItemTemplate>

                        <InsertItemTemplate>
                            <asp:TextBox ID="lblZipCodeInT" runat="server" Text='<%# Bind("ZipCode") %>'>
                            </asp:TextBox>

                        </InsertItemTemplate>

                        <HeaderStyle HorizontalAlign="Left" Width="100px" />
                        <ItemStyle Width="200px" />

                    </asp:TemplateField>


                    <asp:TemplateField HeaderText="PhoneNumber">

                        <ItemTemplate>

                            <asp:Label ID="lblPhoneNumberIT" runat="server" Text='<%# Bind("PhoneNumber") %>'> 

                            </asp:Label>

                        </ItemTemplate>

                        <EditItemTemplate>
                            <asp:TextBox ID="lblPhoneNumberET" runat="server" Text='<%# Bind("PhoneNumber") %>'>
                                </asp:TextBox>

                        </EditItemTemplate>

                        <InsertItemTemplate>
                            <asp:TextBox ID="lblPhoneNumberInT" runat="server" Text='<%# Bind("PhoneNumber") %>'>
                            </asp:TextBox>

                        </InsertItemTemplate>

                        <HeaderStyle HorizontalAlign="Left" Width="100px" />
                        <ItemStyle Width="200px" />

                    </asp:TemplateField>


                    <asp:CommandField ButtonType="Button" ShowDeleteButton="true" ShowEditButton="true" ShowInsertButton="true" />

                </Fields>
            </asp:DetailsView>
            <asp:ValidationSummary ID="ValidationSummary1" runat="server" 
                HeaderText="Please correct the following errors:" CssClass="error" />
            <p>
                <asp:Label ID = "lblError" runat="server" EnableViewState="False" CssClass="error">
                </asp:Label>
            </p>
        </div>
    </form>
    </section>
</body>
</html>
2

There are 2 answers

1
Humpy On

Yes, in your behind code under the Page_Load method insert

if(!Page.IsPostBack)
{
   GridView1.DataBind();
}
1
arun On

You have to use this event into this event you can bind the Gridview and to redirect the the same page your data will be reflected on your Gridview Hope this will help try this....

protected void DetailsView1_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e) { if (e.AffectedRows == 1) { GridView1.DataBind(); Response.Redirect("Yourpage.aspx"); } }