Wrox Programmer Forums
|
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
 
Old August 14th, 2012, 05:04 PM
Authorized User
 
Join Date: Aug 2011
Posts: 44
Thanks: 14
Thanked 0 Times in 0 Posts
Default FileUpload in an UpdatePanel

I tried to wrap the ManagePhotoAlbum.aspx with an UpdatePanel and the FileUpload did not work any more.

I read in the Ajax documentation that:
"To use a FileUpload control inside an UpdatePanel control, set the postback control that submits the file to be a PostBackTrigger control for the panel."

I set a <PostBackTrigger> to point to the InsertButton but at runtime I get the exception:
"A control with ID 'InsertButton' could not be found for the trigger in UpdatePanel 'UpdatePanel1' "

Why the <PostBackTrigger> cannot find a control which is in the same page ?

Here below you can find my code:

ManagePhotoAlbum.aspx

Code:
<%@ Page Language="C#" 
    MasterPageFile="~/PagineMaster/MasterPage.master" 
    AutoEventWireup="true" 
    CodeFile="ManagePhotoAlbum.aspx.cs" 
    Inherits="Albums_ManagePhotoAlbum" 
    Title="Gestione album fotografico" 
%>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cphMain" Runat="Server">

    <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
        <ContentTemplate>
 
 <asp:Panel ID="Panel1" runat="server" style="padding: 8px; border: thin solid #0000FF">  
        
        <asp:Panel ID="Panel2" runat="server" style="padding: 3px; border: thin solid #0000FF" 
                Height="33px">
               <h3>Gestione album fotografico</h3>
        </asp:Panel>
    <br />

    <asp:ListView ID="ListView1" runat="server" 
                  DataKeyNames="Id" 
                  DataSourceID="LinqDataSource1" 
                  InsertItemPosition="LastItem" 
            oniteminserting="ListView1_ItemInserting"> 
 
        <LayoutTemplate>
            <ul class="RiquadroFoto">
                <li ID="itemPlaceholder" runat="server" />
            </ul>
            </LayoutTemplate>    
            
        <InsertItemTemplate>
            <li>
                Descrizione 
                <br />
                <asp:TextBox ID="DescrizioneTextBox" runat="server" 
                    Text='<%# Bind("Descrizione") %>' />
                <asp:RequiredFieldValidator ID="rfvDescrizione" runat="server" ErrorMessage="Inserire la descrizione della foto" ControlToValidate="DescrizioneTextBox"></asp:RequiredFieldValidator>   
                <br />
                
                Tooltip 
                <br />
                <asp:TextBox ID="TooltipTextBox" runat="server" Text='<%# Bind("Tooltip") %>' />
                <asp:RequiredFieldValidator ID="rfvTooltip" runat="server" ErrorMessage="Inserire il tooltip della foto" ControlToValidate="TooltipTextBox"></asp:RequiredFieldValidator>
                <br />
                
                Foto 
                <br />
                <asp:FileUpload ID="FileUpload1" runat="server" />
                <asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Scegliere un file con estensione jpg"></asp:CustomValidator>
                <br /><br />
                
                <asp:Button ID="InsertButton" runat="server" CommandName="Insert" 
                    Text="Inserisci" />
                    &nbsp;&nbsp;
                <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" 
                    Text="Annulla" CausesValidation="False" />
            </li>
        </InsertItemTemplate>   
                  
        <ItemTemplate>
            <li>
                
                <asp:Label ID="DescrizioneLabel" runat="server" 
                    Text='<%# Eval("Descrizione") %>' />
                <br />
                
                <asp:Label ID="TooltipLabel" runat="server" Text='<%# Eval("Tooltip") %>' />
                <br />
                
                <asp:Image ID="ImageUrl" runat="server"  ImageUrl='<%# Eval("Url") %>' />
                <br />
                
                <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" 
                    Text="Elimina" />
            </li>
        </ItemTemplate>
                        
    </asp:ListView>

    <asp:LinqDataSource ID="LinqDataSource1" runat="server" 
            ContextTypeName="PlanetWroxDataContext" EnableDelete="True" EnableInsert="True" 
            TableName="Pictures" Where="AlbumId == @AlbumId" 
            oninserting="LinqDataSource1_Inserting">
            <WhereParameters>
                <asp:QueryStringParameter DefaultValue="-1" Name="AlbumId" 
                    QueryStringField="AlbumId" Type="Int32" />
            </WhereParameters>
     </asp:LinqDataSource>

       </asp:Panel> 
   
     </ContentTemplate>
     <Triggers>
          <asp:PostBackTrigger ControlID="InsertButton" />
     </Triggers>
   </asp:UpdatePanel>
</asp:Content>


ManagePhotoAlbum.aspx.cs
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 Albums_ManagePhotoAlbum : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        int albumId = Convert.ToInt32(Request.QueryString.Get("AlbumId"));

        using (PlanetWroxDataContext mioDataContext = new PlanetWroxDataContext())
        {
            string proprietarioAlbum = (from p in mioDataContext.PhotoAlbum
                                        where p.Id == albumId
                                        select p.Utente).Single();

            if ( (User.Identity.Name != proprietarioAlbum) && (!User.IsInRole("Manager")) )
            {
                Response.Redirect("~/"); 
            }
        }
    }
    protected void LinqDataSource1_Inserting(object sender, LinqDataSourceInsertEventArgs e)
    {
        Picture miaFoto = (Picture)e.NewObject;
        miaFoto.AlbumId = Convert.ToInt32(Request.QueryString.Get("AlbumId"));

        FileUpload mioFileUpload = (FileUpload)ListView1.InsertItem.FindControl("FileUpload1");
        string percorsoVirtuale = "~/ImmaginiDB/";
        string percorsoReale = Server.MapPath(percorsoVirtuale);
        string nomeFile = Guid.NewGuid().ToString();
        string estensione = System.IO.Path.GetExtension(mioFileUpload.FileName);

        mioFileUpload.SaveAs(System.IO.Path.Combine(percorsoReale, nomeFile + estensione));
        miaFoto.Url = percorsoVirtuale + nomeFile + estensione;
    }

    protected void ListView1_ItemInserting(object sender, ListViewInsertEventArgs e)
    {
        FileUpload mioFileUpload = (FileUpload)ListView1.InsertItem.FindControl("FileUpload1");
        if (!mioFileUpload.HasFile || !mioFileUpload.FileName.ToLower().EndsWith(".jpg"))
        {
            CustomValidator CustomValidator1 = (CustomValidator)ListView1.InsertItem.FindControl("CustomValidator1");
            CustomValidator1.IsValid = false;
            e.Cancel = true;
        }
    }
}
 
Old August 14th, 2012, 05:07 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Quote:
Why the <PostBackTrigger> cannot find a control which is in the same page ?
Because it isn't directly placed in the page but inside a different "naming container" (that of the ListView's InsertItemTemplate.

You could try setting the the ControlID of the PostBackTrigger to the client ID or name of the LinkButton (which you can find by looking at the final HTML source). Not sure if that's going to work though; haven't tested this.

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!

Last edited by Imar; August 14th, 2012 at 05:10 PM..
 
Old August 14th, 2012, 05:30 PM
Authorized User
 
Join Date: Aug 2011
Posts: 44
Thanks: 14
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Imar View Post
You could try setting the the ControlID of the PostBackTrigger to the client ID or name of the LinkButton (which you can find by looking at the final HTML source). Not sure if that's going to work though; haven't tested this.
It doesn't seem to work. I get the same exception:

A control with ID 'ctl00$cphMain$ListView1$ctrl0$InsertButton' could not be found for the trigger in UpdatePanel 'UpdatePanel1'.

Anyway, thanks
Antonius





Similar Threads
Thread Thread Starter Forum Replies Last Post
UpdateProgress and Updatepanel phuchoang2583 ASP.NET 3.5 Professionals 0 February 16th, 2011 03:48 AM
UpdatePanel question caiman ASP.NET 3.5 Basics 5 August 19th, 2010 01:05 PM
UpdatePanel without ScriptManager Manoj Bisht ASP.NET 3.5 Professionals 2 March 9th, 2009 11:12 PM
UpdatePanel daniel.mihalcea Ajax 10 September 27th, 2008 07:39 AM
UpdatePanel Problem? rajatbirth_1978 .NET Framework 2.0 0 February 14th, 2007 02:27 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.