 |
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
|
|
|
|
|

July 26th, 2011, 10:35 AM
|
|
Authorized User
|
|
Join Date: Jun 2011
Posts: 32
Thanks: 22
Thanked 0 Times in 0 Posts
|
|
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
|
|

July 26th, 2011, 11:31 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|
The Following User Says Thank You to Imar For This Useful Post:
|
|
|

July 26th, 2011, 12:00 PM
|
|
Authorized User
|
|
Join Date: Jun 2011
Posts: 32
Thanks: 22
Thanked 0 Times in 0 Posts
|
|
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>
|
|

July 26th, 2011, 12:38 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|

July 26th, 2011, 12:40 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|
The Following User Says Thank You to Imar For This Useful Post:
|
|
|

July 26th, 2011, 01:16 PM
|
|
Authorized User
|
|
Join Date: Jun 2011
Posts: 32
Thanks: 22
Thanked 0 Times in 0 Posts
|
|
Perfect!
Perfect, that was it!
TNX for everything
|
|
 |
|