 |
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 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
|
|
|
|
|

April 14th, 2010, 09:45 AM
|
|
Registered User
|
|
Join Date: Apr 2010
Posts: 7
Thanks: 2
Thanked 1 Time in 1 Post
|
|
Displaying the Photo Album in a LoginView?
I'v successfully made a photogallery connected to a drop down list instead of a Query String.
When putting this in a LoginView for only logged in users to see, i get an error when trying to access the page.
Anyway round this? I would like to display different content to logged and unlogged users.
Cheers!
Robin
Compilation Error:
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS0103: The name 'DropDownList1' does not exist in the current context
Source Error:
Line 22: {
Line 23: Picture myPicture = (Picture)e.NewObject;
Line 24: myPicture.PhotoAlbumId = Convert.ToInt32(DropDownList1.SelectedValue);
Line 25:
Line 26:
Source File: c:\Users\Robin\Desktop\UBMCApplication- with login view but dont work\PhotoGallery.aspx.cs Line: 24
|
|

April 14th, 2010, 10:03 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
You need to use FindControl to find your control in the LoginView
Cheers,
Imar
|
|

April 14th, 2010, 10:58 AM
|
|
Registered User
|
|
Join Date: Apr 2010
Posts: 7
Thanks: 2
Thanked 1 Time in 1 Post
|
|
OK I think Iv fixed the error for the drop down list, but i'm not sure how to use FindControl for the ListView. The blue text is my fix, the red text is for the sections I'm unsure of.
Regards
Robin
protected void LinqDataSource2_Inserting(object sender, LinqDataSourceInsertEventArgs e)
{
Picture myPicture = (Picture)e.NewObject;
myPicture.PhotoAlbumId = Convert.ToInt32(FindControl("DropDownList1.SelectedValue"));
FileUpload FileUpload1 = (FileUpload)ListView1.InsertItem.FindControl("FileUpload1");
string virtualFolder = "~/ClubPhotos/";
string physicalFolder = Server.MapPath(virtualFolder);
string fileName = Guid.NewGuid().ToString();
string extension = System.IO.Path.GetExtension(FileUpload1.FileName);
FileUpload1.SaveAs(System.IO.Path.Combine(physical Folder, fileName + extension));
myPicture.ImageUrl = virtualFolder + fileName + extension;
}
protected void ListView1_ItemInserting(object sender, ListViewInsertEventArgs e)
{
FileUpload FileUpload1 = (FileUpload)ListView1.InsertItem.FindControl("FileUpload1");
if (!FileUpload1.HasFile || !FileUpload1.FileName.ToLower().EndsWith(".jpg"))
{
CustomValidator CustomValidator1 =
(CustomValidator)ListView1.InsertItem.FindControl("CustomValidator1 ");
CustomValidator1.IsValid = false;
e.Cancel = true;
}
}
|
|

April 14th, 2010, 11:17 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
FindControl should be used on the container. So, if you have a LoginView, you use this:
myLoginView.FindControl("SomeControl")
It would help if you provided more information. For example, about the structure of your markup, the things that work and don't work, the errors you get and so on... I can't suggest a fix based on the current information alone.
Cheers,
Imar
|
|

April 14th, 2010, 01:45 PM
|
|
Registered User
|
|
Join Date: Apr 2010
Posts: 7
Thanks: 2
Thanked 1 Time in 1 Post
|
|
Imar,
Heres the contents of my photo gallery page. It contains a LoginView with my gallery in the LoggedInTemplate.
In the LoggedInTemplate I have included the photogallery linked to a drop down made exactly as in p446.
Here's the Error list I get when built:
Error 1 The name 'DropDownList1' does not exist in the current context C:\Users\Robin\Desktop\UBMCApplication- with login view but dont work\PhotoGallery.aspx.cs 24 50 C:\...\UBMCApplication- with login view but dont work\
Error 2 The name 'ListView1' does not exist in the current context C:\Users\Robin\Desktop\UBMCApplication- with login view but dont work\PhotoGallery.aspx.cs 27 46 C:\...\UBMCApplication- with login view but dont work\
Error 3 The name 'ListView1' does not exist in the current context C:\Users\Robin\Desktop\UBMCApplication- with login view but dont work\PhotoGallery.aspx.cs 40 46 C:\...\UBMCApplication- with login view but dont work\
Error 4 The name 'ListView1' does not exist in the current context C:\Users\Robin\Desktop\UBMCApplication- with login view but dont work\PhotoGallery.aspx.cs 44 51 C:\...\UBMCApplication- with login view but dont work\
Here's My Page
Code:
<asp:LoginView ID="LoginView1" runat="server">
<LoggedInTemplate>
<h1>
UBMC Photo Gallery<br />
</h1>
<p>
Please select a Photo
Album
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
DataSourceID="LinqDataSource1" DataTextField="Name" DataValueField="Id"
Width="210px">
</asp:DropDownList>
</p>
<p>
</p>
<div id="Pager2"> <asp:DataPager ID="DataPager2" PagedControlID="ListView1" PageSize="8" runat="server">
<Fields>
<asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True"
ShowNextPageButton="False" ShowPreviousPageButton="False" />
<asp:NumericPagerField />
<asp:NextPreviousPagerField ButtonType="Button" ShowLastPageButton="True"
ShowNextPageButton="False" ShowPreviousPageButton="False" />
</Fields>
</asp:DataPager>
</div>
<asp:ListView ID="ListView1" runat="server" DataKeyNames="Id"
DataSourceID="LinqDataSource2" InsertItemPosition="LastItem"
oniteminserting="ListView1_ItemInserting">
<ItemTemplate>
<li style="">
<asp:Image ID="ImageUrl1" runat="server" ToolTip='<%# Eval("Tooltip") %>' ImageUrl='<%# Eval("ImageUrl") %>' />
<br />
Description:
<asp:Label ID="DescriptionLabel" runat="server"
Text='<%# Eval("Description") %>' />
<br />
</li>
</ItemTemplate>
<InsertItemTemplate>
<li style="">Description:
<asp:TextBox ID="DescriptionTextBox" runat="server"
Text='<%# Bind("Description") %>' />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="DescriptionTextBox" runat="server" ErrorMessage="Please enter a description"></asp:RequiredFieldValidator>
<br />
Tooltip:
<asp:TextBox ID="TooltipTextBox" runat="server" Text='<%# Bind("Tooltip") %>' />
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" ControlToValidate="TooltipTextBox" runat="server" ErrorMessage="Please enter a tooltip"></asp:RequiredFieldValidator>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Please select a valid .jpg file"></asp:CustomValidator>
<asp:Button ID="InsertButton" runat="server" CommandName="Insert"
Text="Insert" />
<asp:Button ID="CancelButton" runat="server" CausesValidation="false" CommandName="Cancel"
Text="Clear" />
</li>
</InsertItemTemplate>
<LayoutTemplate>
<ul class="itemContainer">
<li ID="itemPlaceholder" runat="server" />
</ul>
<div style="">
</div>
</LayoutTemplate>
</asp:ListView>
<div id="Pager"> <asp:DataPager ID="DataPager1" PagedControlID="ListView1" PageSize="8" runat="server">
<Fields>
<asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True"
ShowNextPageButton="False" ShowPreviousPageButton="False" />
<asp:NumericPagerField />
<asp:NextPreviousPagerField ButtonType="Button" ShowLastPageButton="True"
ShowNextPageButton="False" ShowPreviousPageButton="False" />
</Fields>
</asp:DataPager>
</div>
<asp:LinqDataSource ID="LinqDataSource2" runat="server"
ContextTypeName="DataClassesDataContext" EnableDelete="True"
EnableInsert="True" OrderBy="Id desc" TableName="Pictures"
Where="PhotoAlbumId == @PhotoAlbumId"
oninserting="LinqDataSource2_Inserting">
<WhereParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="PhotoAlbumId"
PropertyName="SelectedValue" Type="Int32" />
</WhereParameters>
</asp:LinqDataSource>
<p>
<asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="DataClassesDataContext" OrderBy="Name" Select="new (Id, Name)"
TableName="PhotoAlbums">
</asp:LinqDataSource>
</p>
</LoggedInTemplate>
<AnonymousTemplate>
<h1>
Please Log In or <asp:HyperLink ID="HyperLink1" NavigateUrl="~/SignUp.aspx" runat="server">Sign Up </asp:HyperLink> for an account</h1>
</AnonymousTemplate>
</asp:LoginView>
And my CodeBehind
Code:
public partial class PhotoGallery : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void LinqDataSource2_Inserting(object sender, LinqDataSourceInsertEventArgs e)
{
Picture myPicture = (Picture)e.NewObject;
myPicture.PhotoAlbumId = Convert.ToInt32(DropDownList1.SelectedValue);
FileUpload FileUpload1 = (FileUpload)ListView1.InsertItem.FindControl("FileUpload1");
string virtualFolder = "~/ClubPhotos/";
string physicalFolder = Server.MapPath(virtualFolder);
string fileName = Guid.NewGuid().ToString();
string extension = System.IO.Path.GetExtension(FileUpload1.FileName);
FileUpload1.SaveAs(System.IO.Path.Combine(physicalFolder, fileName + extension));
myPicture.ImageUrl = virtualFolder + fileName + extension;
}
protected void ListView1_ItemInserting(object sender, ListViewInsertEventArgs e)
{
FileUpload FileUpload1 = (FileUpload)ListView1.InsertItem.FindControl("FileUpload1");
if (!FileUpload1.HasFile || !FileUpload1.FileName.ToLower().EndsWith(".jpg"))
{
CustomValidator CustomValidator1 =
(CustomValidator)ListView1.InsertItem.FindControl("CustomValidator1");
CustomValidator1.IsValid = false;
e.Cancel = true;
}
}
}
Many Thanks
|
|

April 14th, 2010, 01:58 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Please reread my previous post. You need to call FindControl on the LoginView, not on the page:
Dim myDropDown = CType(LoginView1.FindControl("myDropDown"), DropDownList)
You need to do the same thing for the ListView. The reason for this is that the control have now been placed in a different naming container (e.g. the LoginView) so you need to use FindControl to find them in that container.
Cheers,
Imar
|
|

April 14th, 2010, 02:47 PM
|
|
Registered User
|
|
Join Date: Apr 2010
Posts: 7
Thanks: 2
Thanked 1 Time in 1 Post
|
|
Sorry, i forgot to mention that i know where to put it, but I dont know how.
For example, the first blue line in the .cs code on the previous post, based on how to find a control:
myLoginView.FindControl("SomeControl")
Which one is correct?
1. myPicture.PhotoAlbumId = Convert.ToInt32(LoginView1.FindControl("DropDownLi st1.SelectedValue"));
2. myPicture.PhotoAlbumId = Convert.ToInt32(LoginView1.FindControl("DropDownLi st1"), DropDownList1.SelectedValue);
3. myPicture.PhotoAlbumId = Convert.ToInt32(LoginView1.FindControl("(DropDownL ist1.SelectedValue)");
How would I insert one in here?
FileUpload FileUpload1 = (FileUpload)ListView1.InsertItem.FindControl("File Upload1");
and here?
(CustomValidator)ListView1.InsertItem.FindControl( "CustomValidator1");
Many Thanks
|
|

April 14th, 2010, 03:19 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi Robin,
First you need to find the ListView control, and then inside it, search for the FileUpload.
Untested, but something like this should work:
ListView myListView = (ListView)LoginView1.FindControl("ListView1");
// From here, you can treat myListView as you would ListView1:
FileUpload FileUpload1 = (FileUpload)myListView.InsertItem.FindControl("Fil eUpload1");
You can do similar stuff with the DropDownList.
But do really you want everything inside a LoginView? What's the content of the AnonymousTemplate? In other words, are you maybe not better off by protecting the entire page using URL authorization and redirect non-authenticated users to a different page?
Imar
|
|
The Following User Says Thank You to Imar For This Useful Post:
|
|
|

April 14th, 2010, 07:41 PM
|
|
Registered User
|
|
Join Date: Apr 2010
Posts: 7
Thanks: 2
Thanked 1 Time in 1 Post
|
|
Solution Found - Inserting Photo Album into Login View
Cheers Imar!!
You have helped me Immensely! Here was my solution, for anyone that may need to do the same with a built Photo Album.
Change the lines in red to the one's in blue.
Many Thanks,
Robin
Code:
public partial class PhotoGallery : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void LinqDataSource2_Inserting(object sender, LinqDataSourceInsertEventArgs e)
{
Picture myPicture = (Picture)e.NewObject;
myPicture.PhotoAlbumId = Convert.ToInt32(DropDownList1.SelectedValue);
FileUpload FileUpload1 = (FileUpload)ListView1.InsertItem.FindControl("FileUpload1");
string virtualFolder = "~/ClubPhotos/";
string physicalFolder = Server.MapPath(virtualFolder);
string fileName = Guid.NewGuid().ToString();
string extension = System.IO.Path.GetExtension(FileUpload1.FileName);
FileUpload1.SaveAs(System.IO.Path.Combine(physicalFolder, fileName + extension));
myPicture.ImageUrl = virtualFolder + fileName + extension;
}
protected void ListView1_ItemInserting(object sender, ListViewInsertEventArgs e)
{
FileUpload FileUpload1 = (FileUpload)ListView1.InsertItem.FindControl("FileUpload1");
if (!FileUpload1.HasFile || !FileUpload1.FileName.ToLower().EndsWith(".jpg"))
{
CustomValidator CustomValidator1 =
(CustomValidator)ListView1.InsertItem.FindControl("CustomValidator1");
CustomValidator1.IsValid = false;
e.Cancel = true;
}
}
}
Code:
protected void LinqDataSource2_Inserting(object sender, LinqDataSourceInsertEventArgs e)
{
Picture myPicture = (Picture)e.NewObject;
DropDownList myDropDownList = (DropDownList)LoginView1.FindControl("DropDownList1");
myPicture.PhotoAlbumId = Convert.ToInt32(myDropDownList.SelectedValue);
ListView myListView = (ListView)LoginView1.FindControl("ListView1");
FileUpload FileUpload1 = (FileUpload)myListView.InsertItem.FindControl("FileUpload1");
string virtualFolder = "~/ClubPhotos/";
string physicalFolder = Server.MapPath(virtualFolder);
string fileName = Guid.NewGuid().ToString();
string extension = System.IO.Path.GetExtension(FileUpload1.FileName);
FileUpload1.SaveAs(System.IO.Path.Combine(physicalFolder, fileName + extension));
myPicture.ImageUrl = virtualFolder + fileName + extension;
}
protected void ListView1_ItemInserting(object sender, ListViewInsertEventArgs e)
{
ListView myListView = (ListView)LoginView1.FindControl("ListView1");
FileUpload FileUpload1 = (FileUpload)myListView.InsertItem.FindControl("FileUpload1");
if (!FileUpload1.HasFile || !FileUpload1.FileName.ToLower().EndsWith(".jpg"))
{
ListView myListView2 = (ListView)LoginView1.FindControl("ListView1");
CustomValidator CustomValidator1 = (CustomValidator)myListView2.InsertItem.FindControl("CustomValidator1");
CustomValidator1.IsValid = false;
e.Cancel = true;
}
}
|
|
The Following User Says Thank You to RobinB3 For This Useful Post:
|
|
|

April 15th, 2010, 03:22 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Great; and thanks for the follow up!
Imar
|
Similar Threads
|
| Thread |
Thread Starter |
Forum |
Replies |
Last Post |
| Photo Album Questions |
m3ben |
BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 |
1 |
April 4th, 2007 12:12 PM |
| Error posting photo to photo album |
abel714 |
BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 |
10 |
February 5th, 2007 03:07 AM |
| Wrox Photo Album Help |
rsearing |
BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 |
6 |
September 27th, 2006 02:30 PM |
| Photo Album Menus |
Tremmorkeep |
BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 |
1 |
August 15th, 2006 01:49 PM |
|
 |