Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3
This is the forum to discuss the Wrox book Beginning ASP.NET 3.5: In C# and VB by Imar Spaanjaars; ISBN: 9780470187593
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 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 Display Modes
  #1 (permalink)  
Old August 7th, 2009, 08:40 AM
Authorized User
Points: 297, Level: 5
Points: 297, Level: 5 Points: 297, Level: 5 Points: 297, Level: 5
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2009
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chap 16 pg 578. Hyperlink plus album not working

Hello,

Have jsut completed the code in the Try It Out on page 578 and and the hyperlink doesn't work at all.

When I place the mouse over the link the mouse changes to a vertical cursor similar to a word processor and the link becomes underlined however left clicking the hyperlink has no affect.

My Default.aspx.cs code looks like this

Code:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
publicpartialclassDefault : BasePage
{
protectedvoid Page_Load(object sender, EventArgs e)
{
}
protectedvoid ListView1_ItemCreated(object sender, ListViewItemEventArgs e)
{
switch (e.Item.ItemType)
{
caseListViewItemType.DataItem:
Button deleteButton = (Button)e.Item.FindControl("DeleteButton");
deleteButton.Visible = Roles.IsUserInRole("Managers");
break;
}
}
protectedvoid ListView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protectedvoid ListView1_DataBoud(object sender, EventArgs e)
{
int photoAlbumId = Convert.ToInt32(DropDownList1.SelectedValue);
using (PlanetWroxDataContext myDatabaseContext = newPlanetWroxDataContext())
{
string photoAlbumOwner = (from p in myDatabaseContext.PhotoAlbums
where p.Id == photoAlbumId
select p.UserName).Single();
if (User.Identity.IsAuthenticated &&
(User.Identity.Name == photoAlbumOwner || User.IsInRole("Managers")))
{
HyperLink1.Visible = true;
HyperLink1.NavigateUrl = "~/ManagePhotoAlbum.aspx?PhotoAlbumId=" +
photoAlbumId.ToString();
}
else
{
HyperLink1.Visible = false;
}
}
}
}
My default.aspx code looks like this

Code:
<%@PageLanguage="C#"MasterPageFile="~/MasterPages/MasterPage.master"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="Default"Title="All Photo Albums" %>
<asp:ContentID="Content1"ContentPlaceHolderID="head"runat="Server">
</asp:Content>
<asp:ContentID="Content2"ContentPlaceHolderID="cpMainContent"runat="Server">
<asp:UpdatePanelID="UpdatePanel1"runat="server">
<ContentTemplate>
<asp:DropDownListID="DropDownList1"runat="server"AutoPostBack="True"DataSourceID="LinqDataSource1"DataTextField="Name"DataValueField="Id">
</asp:DropDownList>
<asp:ListViewID="ListView1"runat="server"DataKeyNames="Id"DataSourceID="LinqDataSource2"onitemcreated="ListView1_ItemCreated"onselectedindexchanged="ListView1_SelectedIndexChanged">
<LayoutTemplate>
<ulclass="itemContainer">
<liid="itemPlaceholder"runat="server"/>
</ul>
<divstyle="">
<asp:DataPagerID="DataPager1"runat="server"PageSize="3">
<Fields>
<asp:NextPreviousPagerFieldButtonType="Button"ShowFirstPageButton="True"ShowLastPageButton="True"/>
</Fields>
</asp:DataPager>
</div>
</LayoutTemplate>
<EmptyDataTemplate>
No data was returned.
</EmptyDataTemplate>
<ItemTemplate>
<li>
<asp:ImageID="Image1"runat="server"ImageUrl='<%# Eval("ImageUrl") %>'ToolTip='<%# Eval("ToolTip") %>'/>
<asp:LabelID="DescriptionLabel"runat="server"Text='<%# Eval("Description") %>'/>
<asp:ButtonID="DeleteButton"CommandName="Delete"runat="server"Text="Delete"/>
</li>
</ItemTemplate>
</asp:ListView>
<asp:LinqDataSourceID="LinqDataSource2"runat="server"ContextTypeName="PlanetWroxDataContext"TableName="Pictures"Where="PhotoAlbumId == @PhotoAlbumId"EnableDelete="True">
<WhereParameters>
<asp:ControlParameterControlID="DropDownList1"Name="PhotoAlbumId"PropertyName="SelectedValue"Type="Int32"/>
</WhereParameters>
</asp:LinqDataSource>
<asp:LinqDataSourceID="LinqDataSource1"runat="server"ContextTypeName="PlanetWroxDataContext"Select="new (Id, Name)"TableName="PhotoAlbums">
</asp:LinqDataSource>
<br/><br/><asp:HyperLinkID="HyperLink1"runat="server">Edit Photo ALbum</asp:HyperLink>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
My NewPhotoAlbum.aspx.cs code looks like this

Code:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
publicpartialclassNewPhotoAlbum : BasePage
{
protectedvoid Page_Load(object sender, EventArgs e)
{
 
}
protectedvoid LinqDataSource1_Inserted(object sender, LinqDataSourceStatusEventArgs e)
{
PhotoAlbum myPhotoAlbum = (PhotoAlbum)e.Result;
Response.Redirect(string.Format("ManagePhotoAlbum.aspx?PhotoAlbumId={0}",
myPhotoAlbum.Id.ToString()));
}
protectedvoid LinqDataSource1_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
}
protectedvoid LinqDataSource1_Inserting(object sender, LinqDataSourceInsertEventArgs e)
{
PhotoAlbum myPhotoAlbum = (PhotoAlbum)e.NewObject;
myPhotoAlbum.UserName = User.Identity.Name;
}
}
And lastly my NewPhotoAlbum.aspx code looks like this

Code:
<%@PageLanguage="C#"MasterPageFile="~/MasterPages/MasterPage.master"AutoEventWireup="true"CodeFile="NewPhotoAlbum.aspx.cs"Inherits="NewPhotoAlbum"Title="Create New Photo Album" %>
<asp:ContentID="Content1"ContentPlaceHolderID="head"Runat="Server">
</asp:Content>
<asp:ContentID="Content2"ContentPlaceHolderID="cpMainContent"Runat="Server">
<asp:DetailsViewID="DetailsView1"runat="server"AutoGenerateRows="False"DataKeyNames="Id"DataSourceID="LinqDataSource1"DefaultMode="Insert"Height="50px"Width="125px">
<Fields>
<asp:BoundFieldDataField="Id"HeaderText="Id"InsertVisible="False"ReadOnly="True"SortExpression="Id"/>
<asp:BoundFieldDataField="Name"HeaderText="Name"SortExpression="Name"/>
<asp:CommandFieldShowInsertButton="True"/>
</Fields>
</asp:DetailsView>
<asp:LinqDataSourceID="LinqDataSource1"runat="server"ContextTypeName="PlanetWroxDataContext"EnableInsert="True"oninserted="LinqDataSource1_Inserted"TableName="PhotoAlbums"onselecting="LinqDataSource1_Selecting">
</asp:LinqDataSource>
</asp:Content>
The Default.aspx page picks up the name of the new albums that I type but it doesn't let me add content to them.

Any help would be most appreciated.


Thanks

Tomche

Last edited by tomche; August 7th, 2009 at 09:05 AM.
Reply With Quote
  #2 (permalink)  
Old August 7th, 2009, 01:15 PM
Imar's Avatar
Wrox Author
Points: 71,164, Level: 100
Points: 71,164, Level: 100 Points: 71,164, Level: 100 Points: 71,164, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,948
Thanks: 79
Thanked 1,555 Times in 1,532 Posts
Default

Hi there,

I have asked it a few times before but I'll be happy to ask it again: please use the Remote Formatting Toolbar buton when you post code here.

Also, can you provide a *clear* step-based problem description? This is way too much code for me to go bug hunting in the hopes to find out what might be your problem. If you tell me from the start what's wrong, you'll get a much better and faster answer.

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
  #3 (permalink)  
Old August 8th, 2009, 01:31 AM
Authorized User
Points: 297, Level: 5
Points: 297, Level: 5 Points: 297, Level: 5 Points: 297, Level: 5
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2009
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Imar,
Sorry it slipped my mind and I didn't realiese until i had already posted.

My Detail.aspx.cs from the photoalbums folder looks like this

Code:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class Default : BasePage
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ListView1_ItemCreated(object sender, ListViewItemEventArgs e)
{
switch (e.Item.ItemType)
{
case ListViewItemType.DataItem:
Button deleteButton = (Button)e.Item.FindControl("DeleteButton");
deleteButton.Visible = Roles.IsUserInRole("Managers");
break;
}
}
protected void ListView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void ListView1_DataBoud(object sender, EventArgs e)
{
int photoAlbumId = Convert.ToInt32(DropDownList1.SelectedValue);
using (PlanetWroxDataContext myDatabaseContext = new PlanetWroxDataContext())
{
string photoAlbumOwner = (from p in myDatabaseContext.PhotoAlbums
where p.Id == photoAlbumId
select p.UserName).Single();
if (User.Identity.IsAuthenticated &&
(User.Identity.Name == photoAlbumOwner || User.IsInRole("Managers")))
{
HyperLink1.Visible = true;
HyperLink1.NavigateUrl = "~/ManagePhotoAlbum.aspx?PhotoAlbumId=" +
photoAlbumId.ToString();
}
else
{
HyperLink1.Visible = false;
}
}
}
}
My default.aspx code looks like this

Code:
<%@ Page Language="C#" MasterPageFile="~/MasterPages/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" Title="All Photo Albums" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cpMainContent" runat="Server">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="LinqDataSource1" DataTextField="Name" DataValueField="Id">
</asp:DropDownList>
<asp:ListView ID="ListView1" runat="server" DataKeyNames="Id" 
DataSourceID="LinqDataSource2" onitemcreated="ListView1_ItemCreated" 
onselectedindexchanged="ListView1_SelectedIndexChanged">
<LayoutTemplate>
<ul class="itemContainer">
<li id="itemPlaceholder" runat="server" />
</ul>
<div style="">
<asp:DataPager ID="DataPager1" runat="server" PageSize="3">
<Fields>
<asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" ShowLastPageButton="True" />
</Fields>
</asp:DataPager>
</div>
</LayoutTemplate>
<EmptyDataTemplate>
No data was returned.
</EmptyDataTemplate>
<ItemTemplate>
<li>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("ImageUrl") %>' ToolTip='<%# Eval("ToolTip") %>' />
<asp:Label ID="DescriptionLabel" runat="server" Text='<%# Eval("Description") %>' />
<asp:Button ID="DeleteButton" CommandName="Delete" runat="server" Text="Delete" />
</li>
</ItemTemplate>
</asp:ListView>
<asp:LinqDataSource ID="LinqDataSource2" runat="server" 
ContextTypeName="PlanetWroxDataContext" TableName="Pictures" 
Where="PhotoAlbumId == @PhotoAlbumId" EnableDelete="True">
<WhereParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="PhotoAlbumId" PropertyName="SelectedValue" Type="Int32" />
</WhereParameters>
</asp:LinqDataSource>
<asp:LinqDataSource ID="LinqDataSource1" runat="server" ContextTypeName="PlanetWroxDataContext" Select="new (Id, Name)" TableName="PhotoAlbums">
</asp:LinqDataSource>
<br /><br /><asp:HyperLink ID="HyperLink1" runat="server">Edit Photo ALbum</asp:HyperLink>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
My photoalbum.aspx.cs file looks like this
Code:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class NewPhotoAlbum : BasePage
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void LinqDataSource1_Inserted(object sender, LinqDataSourceStatusEventArgs e)
{
PhotoAlbum myPhotoAlbum = (PhotoAlbum)e.Result;
Response.Redirect(string.Format("ManagePhotoAlbum.aspx?PhotoAlbumId={0}",
myPhotoAlbum.Id.ToString()));
}
protected void LinqDataSource1_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
}
protected void LinqDataSource1_Inserting(object sender, LinqDataSourceInsertEventArgs e)
{
PhotoAlbum myPhotoAlbum = (PhotoAlbum)e.NewObject;
myPhotoAlbum.UserName = User.Identity.Name;
}
}
My newphotoalbumaspx file looks like this

Code:
%@ Page Language="C#" MasterPageFile="~/MasterPages/MasterPage.master" AutoEventWireup="true" CodeFile="NewPhotoAlbum.aspx.cs" Inherits="NewPhotoAlbum" Title="Create New Photo Album" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cpMainContent" Runat="Server">
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="Id" DataSourceID="LinqDataSource1" DefaultMode="Insert" Height="50px" Width="125px">
<Fields>
<asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:CommandField ShowInsertButton="True" />
</Fields>
</asp:DetailsView>
<asp:LinqDataSource ID="LinqDataSource1" runat="server" 
ContextTypeName="PlanetWroxDataContext" EnableInsert="True" 
oninserted="LinqDataSource1_Inserted" TableName="PhotoAlbums" 
onselecting="LinqDataSource1_Selecting">
</asp:LinqDataSource>
</asp:Content>
Remove text formatting button applied

Cheers

Thomas
Reply With Quote
  #4 (permalink)  
Old August 8th, 2009, 06:09 AM
Imar's Avatar
Wrox Author
Points: 71,164, Level: 100
Points: 71,164, Level: 100 Points: 71,164, Level: 100 Points: 71,164, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,948
Thanks: 79
Thanked 1,555 Times in 1,532 Posts
Default

Much better. This way the code is much easier to read.

It looks like your ListView is missing the handler wiring for ListView1_DataBound. That means that this method in the Code Behind is never called and thus doesn't set the NavigateUrl or hide the link. Change this:

Code:
<asp:ListView ID="ListView1" runat="server" DataKeyNames="Id" 
DataSourceID="LinqDataSource2" onitemcreated="ListView1_ItemCreated" 
onselectedindexchanged="ListView1_SelectedIndexChanged">
to this:

Code:
<asp:ListView ID="ListView1" runat="server" DataKeyNames="Id" 
DataSourceID="LinqDataSource2" onitemcreated="ListView1_ItemCreated" 
ondatabound="ListView1_DataBound">
Then rename the method in the Code Behind from

Code:
protected void ListView1_DataBoud(object sender, EventArgs e)
to this:

Code:
 
protected void ListView1_DataBound(object sender, EventArgs e)
(notice the n in Bound) and remove the code for the ListView1_SelectedIndexChanged method as it's not used.

You may want to consider downloading a copy of the sorce that comes with the book so you can compare your code with mine. May help to spot these kind of issues more easily.

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
  #5 (permalink)  
Old August 8th, 2009, 06:37 AM
Authorized User
Points: 297, Level: 5
Points: 297, Level: 5 Points: 297, Level: 5 Points: 297, Level: 5
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2009
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It wooorrks!!!


That Wrox!
Reply With Quote
  #6 (permalink)  
Old June 1st, 2016, 04:56 PM
Authorized User
Points: 103, Level: 2
Points: 103, Level: 2 Points: 103, Level: 2 Points: 103, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2015
Location: Annandale, VA
Posts: 12
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via Skype™ to trtyler
Default

Ok, Thanks. Adding the " ondatabound="ListView1_DataBound" " element to the source file also worked for me. Some how the "DataBound" event in the properties window seemed to be disabled. I typed the code for the event handler in by hand and it was not recognized until I added the above element to the source file. When I went back to the Event in the Properties window, low and behold, the event handler name had been added to the DataBound event. Thanks again for the tip.
Reply With Quote
Reply


Thread Tools
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
Chap 16 pg 574: Elements not supported Elements not known tomche BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 1 August 6th, 2009 02:48 PM
Chap 16 pg 570 System.Collections.Generic has some invalid arguments tomche BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 7 August 6th, 2009 10:08 AM
Chap 16 pg 562 MyProfile does see web.config tomche BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 7 July 28th, 2009 10:47 AM
Pg 16 Green Arrow? Astrocloud BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 5 March 17th, 2008 09:34 AM
Chap 16 example not working John Cook BOOK: Beginning VB.NET 2nd Edition/Beginning VB.NET 2003 2 February 10th, 2006 04:40 PM



All times are GMT -4. The time now is 03:31 AM.


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