Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > BOOK: Beginning ASP.NET 4 : in C# and VB
|
BOOK: Beginning ASP.NET 4 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4: in C# and VB by Imar Spaanjaars; ISBN: 9780470502211
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4 : in C# and VB 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 March 8th, 2012, 02:09 PM
Authorized User
 
Join Date: Jan 2011
Posts: 41
Thanks: 0
Thanked 1 Time in 1 Post
Default Inserting the filename from a FileUplod Control

Hi Guys,

I have a little problem here.

I have a DetailsView control and inside there I have converted one of the fields to a template and replaced it with a FileUpload control. Now when the DetailsView is inserting data, I want to get the filename from that FileUpload control and add it in the database. Here is the code I have written in the ItemInserting Event handler of the DetailsView:

Code:
 protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {
        FileUpload imageUpload = (FileUpload)DetailsView1.FindControl("FileUpload1");

        e.Values["CategoryAvatarUrl"] = imageUpload.FileName;
        e.Values["AddedDate"] = DateTime.Now;
        

    }
Now the problem is the imageUplod.FileName is returning null. It is not giving me the string that is in the FileUpload control. To test this out I first added a RequiredFieldValidator to the control. I noticed that when I add a file to the control and click Send, the string in the control disappears and the RequiredFiedValidator fires indicating I have not added a file.

I tried the same example with a textbox, but I am getting the correct value from the textbox. Can anyone help me out?

Thanks
 
Old March 8th, 2012, 03:55 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

Can you post the full code for the page. Are you using an UpdatePanel by any chance?

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!
 
Old March 8th, 2012, 04:20 PM
Authorized User
 
Join Date: Jan 2011
Posts: 41
Thanks: 0
Thanked 1 Time in 1 Post
Default

Yes I am using an update panel.

Does it affect the FileUpload Control? Here is my full code.

Code:
<%@ Page Title="Ruby Admin | Cateogries" Language="C#" MasterPageFile="~/Masters/AdminMaster.master" AutoEventWireup="true" CodeFile="Categories.aspx.cs" Inherits="Admin_Categories" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
    <style type="text/css">
        .addCategory
        {}
        </style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="CpReadable" Runat="Server">
    <div class="pageContent">
    <h1>Categories Administration</h1>
    <p>
    On this page, you can create new or edit existing categories. You can even delete categories you don't want to use 
    anymore; however, before deleting an category, you <strong>MUST</strong> delete all the services that belong to that 
    category.
    </p>

    <h1>Edit Categories</h1>

    List of all available categories: 
    <br />
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" 
            AutoGenerateColumns="False" DataKeyNames="ServiceCategoryID" 
            DataSourceID="CategorySqlDataSource" CssClass="gridView">
        <AlternatingRowStyle CssClass="alteringRows" />
        <Columns>
            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
            <asp:BoundField DataField="ServiceCategoryID" HeaderText="CategoryID" 
                InsertVisible="False" ReadOnly="True" SortExpression="ServiceCategoryID" />
            <asp:BoundField DataField="Category" HeaderText="Category" 
                SortExpression="Category" />
            <asp:BoundField DataField="CategoryAvatarUrl" HeaderText="Category Image" 
                SortExpression="CategoryAvatarUrl" />
            <asp:BoundField DataField="Description" HeaderText="Description" 
                SortExpression="Description" />
            <asp:BoundField DataField="AddedDate" DataFormatString="{0:d}" 
                HeaderText="AddedDate" SortExpression="AddedDate" />
        </Columns>
    </asp:GridView>

       
        <asp:SqlDataSource ID="CategorySqlDataSource" runat="server" 
            ConflictDetection="CompareAllValues" 
            ConnectionString="<%$ ConnectionStrings:RubyDBConnectionString %>" 
            DeleteCommand="DELETE FROM [ServiceCategoryTable] WHERE [ServiceCategoryID] = @original_ServiceCategoryID AND [Category] = @original_Category AND [CategoryAvatarUrl] = @original_CategoryAvatarUrl AND (([Description] = @original_Description) OR ([Description] IS NULL AND @original_Description IS NULL)) AND [AddedDate] = @original_AddedDate" 
            InsertCommand="INSERT INTO [ServiceCategoryTable] ([Category], [CategoryAvatarUrl], [Description], [AddedDate]) VALUES (@Category, @CategoryAvatarUrl, @Description, @AddedDate)" 
            OldValuesParameterFormatString="original_{0}" 
            SelectCommand="SELECT * FROM [ServiceCategoryTable]" 
            UpdateCommand="UPDATE [ServiceCategoryTable] SET [Category] = @Category, [CategoryAvatarUrl] = @CategoryAvatarUrl, [Description] = @Description, [AddedDate] = @AddedDate WHERE [ServiceCategoryID] = @original_ServiceCategoryID AND [Category] = @original_Category AND [CategoryAvatarUrl] = @original_CategoryAvatarUrl AND (([Description] = @original_Description) OR ([Description] IS NULL AND @original_Description IS NULL)) AND [AddedDate] = @original_AddedDate">
            <DeleteParameters>
                <asp:Parameter Name="original_ServiceCategoryID" Type="Int32" />
                <asp:Parameter Name="original_Category" Type="String" />
                <asp:Parameter Name="original_CategoryAvatarUrl" Type="String" />
                <asp:Parameter Name="original_Description" Type="String" />
                <asp:Parameter DbType="DateTime2" Name="original_AddedDate" />
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="Category" Type="String" />
                <asp:Parameter Name="CategoryAvatarUrl" Type="String" />
                <asp:Parameter Name="Description" Type="String" />
                <asp:Parameter DbType="DateTime2" Name="AddedDate" />
            </InsertParameters>
            <UpdateParameters>
                <asp:Parameter Name="Category" Type="String" />
                <asp:Parameter Name="CategoryAvatarUrl" Type="String" />
                <asp:Parameter Name="Description" Type="String" />
                <asp:Parameter DbType="DateTime2" Name="AddedDate" />
                <asp:Parameter Name="original_ServiceCategoryID" Type="Int32" />
                <asp:Parameter Name="original_Category" Type="String" />
                <asp:Parameter Name="original_CategoryAvatarUrl" Type="String" />
                <asp:Parameter Name="original_Description" Type="String" />
                <asp:Parameter DbType="DateTime2" Name="original_AddedDate" />
            </UpdateParameters>
        </asp:SqlDataSource>

       
        <br />

       
        <h1>Add Categories</h1>
       
        <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" 
            DataKeyNames="ServiceCategoryID" DataSourceID="CategorySqlDataSource" 
            DefaultMode="Insert" Height="50px" oniteminserting="DetailsView1_ItemInserting" 
            Width="125px">
            <Fields>
                <asp:BoundField DataField="ServiceCategoryID" HeaderText="ServiceCategoryID" 
                    InsertVisible="False" ReadOnly="True" SortExpression="ServiceCategoryID" />
                <asp:BoundField DataField="Category" HeaderText="Category" 
                    SortExpression="Category" />
                <asp:TemplateField HeaderText="CategoryAvatarUrl" 
                    SortExpression="CategoryAvatarUrl">
                    <InsertItemTemplate>
                        <asp:FileUpload ID="FileUpload1" runat="server" />
                    </InsertItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" 
                            Text='<%# Bind("CategoryAvatarUrl") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("CategoryAvatarUrl") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="Description" HeaderText="Description" 
                    SortExpression="Description" />
                <asp:BoundField DataField="AddedDate" HeaderText="AddedDate" 
                    SortExpression="AddedDate" />
                <asp:CommandField ShowInsertButton="True" />
            </Fields>
        </asp:DetailsView>
       
        <br />
        <br />
        
        <br />
        </ContentTemplate>
      </asp:UpdatePanel>
</div>
</asp:Content>
 
Old March 9th, 2012, 07:15 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Yes, that makes a big difference. The FileUpload doesn't work inside an UpdatePanel:
https://www.google.com/#hl=en&gs_nf=...=1050&bih=1318

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!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Images by filename ekugler Reporting Services 2 October 11th, 2007 04:21 AM
filename validation mayuraprabhu Pro VB 6 1 December 15th, 2006 02:43 PM
Dynamic Filename ozPATT Access VBA 1 November 28th, 2005 01:56 PM
Invalid filename Bhavin Crystal Reports 0 December 15th, 2004 05:00 PM
Get a filename from directory cutovoi Excel VBA 1 January 7th, 2004 03:18 AM





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