Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics 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 January 26th, 2008, 07:14 PM
Registered User
 
Join Date: Jan 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to sync FormView and DataList

Hi, I'm having a heck of a time with trying to have my FormView items in sync with my DataList and really need some help.

I have a FormView that displays some products like a image of the product, product name, description, etc. I also have a DataList that displays comments about the particular product.

I have a HiddenField on the FormView that contains an ID I assign to it from a stored procedure as a parameter. I use the ID to associate comments with a particular product. I'm trying to achieve the same results as say Amazon where users enter a comment about say a book and each book has user entered comments associated with a particular book.

The problem is in my Page_Load() I bind the DataList and when you hit the page you see the first product displayed and any entered comments for it. When I page forward to the next product the HiddenField is grabbing the ID that was assigned in the initial page load, if I page forward again to the next product the ID is the value of the previous product. It's always off the same holds true when I page backwards in the Formview.

How can I make sure the currently displayed product contains the right ID that will thus display the comments associated with the correct product when both paging forward/backward in my FormView?

Here is some of my code.

This is in the ItemTemplate of my FormView.
Code:
<td style="width: 50%" align="left">
    <asp:HiddenField ID="PIDHiddenField" runat="server" Value='<%# Bind("PID") %>' />
</td>
Code:
protected new void Page_Load(object sender, EventArgs e)
{
   if (!Page.IsPostBack)
   {
       commentID = ((HiddenField)ProductFormView.FindControl("PIDHiddenField")).Value;

            CommentsDataList.DataSource = ProductCommentsDS;
            CommentsDataList.DataBind();
        }
    }
This is in my DataList.
Code:
<ItemTemplate>
     <asp:Label ID="PIDLabel" runat="server" Text='<%# Bind("PID") %>'></asp:Label>
</ItemTemplate>
My DataList's SqlDataSource
Code:
<asp:SqlDataSource ID="ProductCommentsDS" runat="server" ConnectionString="<%$ ConnectionStrings:ProductComments %>"
        SelectCommand="GetComments" SelectCommandType="StoredProcedure">
        <SelectParameters>
             <asp:ControlParameter Name="commentID" ControlID="ProductFormView$PIDHiddenField" PropertyName="Value" DefaultValue="" />
        </SelectParameters>
</asp:SqlDataSource>
Code:
protected void ProductFormView_PageIndexChanging(object sender, FormViewPageEventArgs e)
{
   CommentsDataList.DataSource = null;
   CommentsDataList.DataBind();

   try
   {
     // Get the product comments associated with a particular product.
         using (SqlConnection connection = new SqlConnection(connString))
         {
             using (SqlCommand cmd = new SqlCommand("GetProductComments", connection))
             {
                connection.Open();

                fv = new FormView();
                HiddenField htf = (HiddenField)ProductFormView.FindControl("PIDHiddenField");

                CommentsDataList.DataSource = ProductCommentsDS;

                commentID = htf.Value;

                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add(new SqlParameter("@commentID", commentID));

                cmd.ExecuteNonQuery();

             }
         }

         CommentsDataList.DataBind();
   }
   catch (SqlException sqlEx)
   {
       .....     
   }
   catch (Exception ex)
   {
       .....   
   }
This has me at my wits end, so I'd really appreciate some help in getting this to work.

Thanks,
CB

 
Old January 26th, 2008, 10:43 PM
Registered User
 
Join Date: Jan 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Please disregard I was able to get this resolved on the ASP.NET forums.







Similar Threads
Thread Thread Starter Forum Replies Last Post
Active Directory Sync SQL CoolEJ SQL Server 2000 1 October 17th, 2007 11:44 AM
Database Sync Rangatharan .NET Framework 2.0 1 August 20th, 2007 02:37 AM
asynch vs. sync hdwriter BOOK: Professional Ajax ISBN: 978-0-471-77778-6 1 April 11th, 2007 10:22 PM
MySQL Sync rajuru MySQL 2 November 9th, 2006 10:08 AM
A Better way to sync a remote and local database? kevorkian SQL Server 2000 1 April 5th, 2005 08:09 AM





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