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