Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > BOOK: Beginning ASP.NET 4 : in C# and VB
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 26th, 2011, 10:35 AM
Authorized User
Points: 130, Level: 2
Points: 130, Level: 2 Points: 130, Level: 2 Points: 130, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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
Reply With Quote
  #2 (permalink)  
Old July 26th, 2011, 11:31 AM
Imar's Avatar
Wrox Author
Points: 72,035, Level: 100
Points: 72,035, Level: 100 Points: 72,035, Level: 100 Points: 72,035, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,079
Thanks: 80
Thanked 1,587 Times in 1,563 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!
Reply With Quote
The Following User Says Thank You to Imar For This Useful Post:
esherr01 (July 26th, 2011)
  #3 (permalink)  
Old July 26th, 2011, 12:00 PM
Authorized User
Points: 130, Level: 2
Points: 130, Level: 2 Points: 130, Level: 2 Points: 130, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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>
Reply With Quote
  #4 (permalink)  
Old July 26th, 2011, 12:38 PM
Imar's Avatar
Wrox Author
Points: 72,035, Level: 100
Points: 72,035, Level: 100 Points: 72,035, Level: 100 Points: 72,035, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,079
Thanks: 80
Thanked 1,587 Times in 1,563 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!
Reply With Quote
  #5 (permalink)  
Old July 26th, 2011, 12:40 PM
Imar's Avatar
Wrox Author
Points: 72,035, Level: 100
Points: 72,035, Level: 100 Points: 72,035, Level: 100 Points: 72,035, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,079
Thanks: 80
Thanked 1,587 Times in 1,563 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!
Reply With Quote
The Following User Says Thank You to Imar For This Useful Post:
esherr01 (July 26th, 2011)
  #6 (permalink)  
Old July 26th, 2011, 01:16 PM
Authorized User
Points: 130, Level: 2
Points: 130, Level: 2 Points: 130, Level: 2 Points: 130, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2011
Posts: 32
Thanks: 22
Thanked 0 Times in 0 Posts
Default Perfect!

Perfect, that was it!

TNX for everything
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 06:17 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.