p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/)
-   BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 (http://p2p.wrox.com/book-beginning-asp-net-3-5-c-vb-book-isbn-978-0-470-18759-3-389/)
-   -   Chap 16 pg 578. Hyperlink plus album not working (http://p2p.wrox.com/book-beginning-asp-net-3-5-c-vb-book-isbn-978-0-470-18759-3/75597-chap-16-pg-578-hyperlink-plus-album-not-working.html)

tomche August 7th, 2009 09:40 AM

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

Imar August 7th, 2009 02:15 PM

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

tomche August 8th, 2009 02:31 AM

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

Imar August 8th, 2009 07:09 AM

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

tomche August 8th, 2009 07:37 AM

It wooorrks!!!


That Wrox!

trtyler June 1st, 2016 05:56 PM

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.


All times are GMT -4. The time now is 03:46 PM.

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