Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > BOOK: Beginning ASP.NET 4 : in C# and VB
|
BOOK: Beginning ASP.NET 4 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4: in C# and VB by Imar Spaanjaars; ISBN: 9780470502211
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4 : in C# and VB section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 26th, 2011, 10:35 AM
Authorized User
 
Join Date: Jun 2011
Posts: 32
Thanks: 22
Thanked 0 Times in 0 Posts
Smile Update GridView after DetailsView Insert in C#

When I click on the "Insert" link after filling in the TextBoxes from a DetailsView control, I would like my GridView display to now include everything I had plus the new insertion "row" (after insertion, "re-visit" the specifiic table).

Instead I get the EmptyDataText of "There are no records to display". If I click on the link (as per book) on the left side I get a proper "refresh" and all records (including new one) is shown as it should. I would like to make it so my users do not have to click to refresh the page.

I have the following in my code:

<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
DataKeyNames="Player_ID" DataSourceID="SqlDataSource1" DefaultMode="Insert"
Height="50px" Width="125px" OnItemInserted="DetailsView1_ItemInserted">

and:

protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
{
// Refresh the GridView control after a new record is inserted
// in the DetailsView control.

GridView1.DataBind();

}//end method

with GridView1 being the ID for my GridView control.

I appreciate your thoughts!

Cliff
 
Old July 26th, 2011, 11:31 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Cliff,

Can you post the rest of your code? In particular the code for the GridView and the code that assigns data to it (using a data source control or code in the Code Behind maybe)? What you posted should work provided that the GridView uses a data source control as well, so I need to see the code to figure out why this doesn't work.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
esherr01 (July 26th, 2011)
 
Old July 26th, 2011, 12:00 PM
Authorized User
 
Join Date: Jun 2011
Posts: 32
Thanks: 22
Thanked 0 Times in 0 Posts
Default Players.aspx

Code:
<%@ Page Title="Add a New Player" Language="C#" MasterPageFile="~/MasterPages/Management.master" %>

<script runat="server">

   string value;
   Int32 theCustomer;

   protected void Page_Load(object sender, EventArgs e)
   {
     
    if (Request.QueryString.Get("Player_Name") != null)
      {
         DetailsView1.DefaultMode = DetailsViewMode.Edit;
      }

   }//end method Page_Load

   protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
   {
      
      
       if (!IsPostBack)
      {
         GolfDatabaseDataContext db = new GolfDatabaseDataContext();

         var query = from Customer in db.Customers
                     where Customer.UserName == Profile.UserName
                     select Customer.Customer_ID;
         
         //Now we have found the Customer_ID value based upon the Profile.UserName
         //Convert that value to string
         //Create a TextBox (CustomerID) based upon TextBox1 (Customer_ID) to show the Customer_ID value in the DetailsView
         // and limit the results (GridView) to the logged in Customer_ID value
          
         foreach (var result in query)
         {
            value = result.ToString();
            theCustomer = Convert.ToInt32(value);
            TextBox CustomerID = ((TextBox)DetailsView1.FindControl("TextBox1"));
            CustomerID.Text = value;
         }//end foreach

      }//if !IsPostBack

       e.Command.Parameters["@Customer_ID"].Value = theCustomer;

   }//end method SqlDataSource1_Selecting
   
    protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
   {
      // Refresh the GridView control after a new record is inserted 
      // in the DetailsView control.

      GridView1.DataBind();
      
   }//end method
   
</script>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cpMainContent" Runat="Server">
   <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
   AllowSorting="True" AutoGenerateColumns="False" AutoPostBack="true" DataKeyNames="Player_ID" 
   DataSourceID="SqlDataSource1" 
   EmptyDataText="There are no data records to display.">
   <Columns>
      <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" 
         ShowSelectButton="True" />
      <asp:BoundField DataField="Customer_ID" HeaderText="Customer_ID" 
         SortExpression="Customer_ID" />
      <asp:BoundField DataField="Player_ID" HeaderText="Player_ID" ReadOnly="True" 
         SortExpression="Player_ID" />
      <asp:BoundField DataField="Player_Name" HeaderText="Player_Name" 
         SortExpression="Player_Name" />
      <asp:BoundField DataField="Handicap" HeaderText="Handicap" 
         SortExpression="Handicap" />
   </Columns>
</asp:GridView>
   <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" 
      DataKeyNames="Player_ID" DataSourceID="SqlDataSource1" DefaultMode="Insert" 
      Height="50px" Width="125px" OnItemInserted="DetailsView1_ItemInserted">
      <Fields>
         <asp:TemplateField HeaderText="Customer_ID" SortExpression="Customer_ID">
            <EditItemTemplate>
               <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Customer_ID") %>'></asp:TextBox>
            </EditItemTemplate>
            <InsertItemTemplate>
               <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Customer_ID") %>'></asp:TextBox>
            </InsertItemTemplate>
            <ItemTemplate>
               <asp:Label ID="Label1" runat="server" Text='<%# Bind("Customer_ID") %>'></asp:Label>
            </ItemTemplate>
         </asp:TemplateField>
         <asp:BoundField DataField="Player_ID" HeaderText="Player_ID" 
            InsertVisible="False" ReadOnly="True" SortExpression="Player_ID" />
         <asp:BoundField DataField="Player_Name" HeaderText="Player_Name" 
            SortExpression="Player_Name" />
         <asp:BoundField DataField="Handicap" Visible="false" HeaderText="Handicap" 
            SortExpression="Handicap" />
         <asp:CommandField ShowInsertButton="True" />
      </Fields>
   </asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
   ConnectionString="<%$ ConnectionStrings:GolfDatabaseConnectionString1 %>" 
   DeleteCommand="DELETE FROM [Player] WHERE [Player_ID] = @Player_ID" 
   InsertCommand="INSERT INTO [Player] ([Customer_ID], [Player_Name], [Handicap]) VALUES (@Customer_ID, @Player_Name, @Handicap)" 
   ProviderName="<%$ ConnectionStrings:GolfDatabaseConnectionString1.ProviderName %>" 
   UpdateCommand="UPDATE [Player] SET [Customer_ID] = @Customer_ID, [Player_Name] = @Player_Name, [Handicap] = @Handicap WHERE [Player_ID] = @Player_ID"
   SelectCommand="SELECT [Customer_ID], [Player_ID], [Player_Name], [Handicap] FROM [Player] WHERE ([Customer_ID] = @Customer_ID)" onselecting="SqlDataSource1_Selecting"> 
   <SelectParameters>
      <asp:Parameter Name="Customer_ID" Type="Int32" />
   </SelectParameters>
   <DeleteParameters>
      <asp:Parameter Name="Player_ID" Type="Int32" />
   </DeleteParameters>
   <InsertParameters>
      <asp:Parameter Name="Customer_ID" Type="Int32" />
      <asp:Parameter Name="Player_Name" Type="String" />
      <asp:Parameter Name="Handicap" Type="Single" />
   </InsertParameters>
   <UpdateParameters>
      <asp:Parameter Name="Customer_ID" Type="Int32" />
      <asp:Parameter Name="Player_Name" Type="String" />
      <asp:Parameter Name="Handicap" Type="Single" />
      <asp:Parameter Name="Player_ID" Type="Int32" />
   </UpdateParameters>
</asp:SqlDataSource>
</asp:Content>
 
Old July 26th, 2011, 12:38 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Did you try calling SqlDataSource1.DataBind as well? It looks as if your Selecting event doesn't fire and the GridView never gets fresh data...

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old July 26th, 2011, 12:40 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Oh, wait a minute. It's this:

if (!IsPostBack)
{
...
}

Inserting a record is a post back, so this code never fires after an insert, and thus the GridView never gets a user id. Remove the check and it should work.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
esherr01 (July 26th, 2011)
 
Old July 26th, 2011, 01:16 PM
Authorized User
 
Join Date: Jun 2011
Posts: 32
Thanks: 22
Thanked 0 Times in 0 Posts
Default Perfect!

Perfect, that was it!

TNX for everything





Similar Threads
Thread Thread Starter Forum Replies Last Post
Using Gridview to Edit NOT DetailsView ? kalel_4444 BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 3 December 4th, 2008 11:36 PM
Insert, update, delete in gridview-!!! URGENT !!! dagad ASP.NET 2.0 Basics 0 September 27th, 2006 06:24 AM
Button on GridView that goes to DetailsView AndrewD ASP.NET 2.0 Basics 0 November 30th, 2005 11:09 AM
Can't get DetailsView to work with GridView oneworld95 ASP.NET 2.0 Basics 3 July 16th, 2005 07:19 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.