View Single Post
  #2 (permalink)  
Old September 5th, 2011, 06:37 AM
Imar's Avatar
Imar Imar is offline
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi there,

Yes, it can be done. From a high level overview, this is what you need:

1. In DetailsView1_ItemInserted and DetailsView1_ItemUpdated retrieve the selected genre ID from the e argument and send it to EndEditing.

2. Modify EndEditing so it forwards the selected genre ID to the Reviews page

3. In Reviews.aspx, look at the query string (only when the page first loads, not after a postback) and set the SelectedValue on the Genre list when there is a valid genre ID. Here's the code to implement this:

1. (in AddEditReview.aspx.cs)
Code:
 
protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
{
  int genreId = Convert.ToInt32(e.Values["GenreId"]);
  EndEditing(genreId);
}
 
protected void DetailsView1_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
{
  int genreId = Convert.ToInt32(e.NewValues["GenreId"]);
  EndEditing(genreId);
}
2. (in AddEditReview.aspx.cs)
Code:
private void EndEditing(int genreId)
{
  Response.Redirect("Reviews.aspx?GenreId=" + genreId.ToString());
}
3. (In Reviews.aspx.cs)
Code:
protected void Page_Load(object sender, EventArgs e)
{
  if (!Page.IsPostBack)
  {
    string genreId = Request.QueryString.Get("GenreId");
    if (!string.IsNullOrEmpty(genreId))
    {
      DropDownList1.SelectedValue = genreId;
    }
  }
}
You still run the risk that the new review is not on the first page of the GridView with reviews, but at least the correct genre has been selected.

Hope this helps,

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