Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > BOOK: Beginning ASP.NET 4 : in C# and VB
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 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 Search this Thread Display Modes
  #1 (permalink)  
Old March 8th, 2012, 02:09 PM
Authorized User
Points: 194, Level: 3
Points: 194, Level: 3 Points: 194, Level: 3 Points: 194, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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
Reply With Quote
  #2 (permalink)  
Old March 8th, 2012, 03:55 PM
Imar's Avatar
Wrox Author
Points: 72,045, Level: 100
Points: 72,045, Level: 100 Points: 72,045, Level: 100 Points: 72,045, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,083
Thanks: 80
Thanked 1,587 Times in 1,563 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!
Reply With Quote
  #3 (permalink)  
Old March 8th, 2012, 04:20 PM
Authorized User
Points: 194, Level: 3
Points: 194, Level: 3 Points: 194, Level: 3 Points: 194, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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>
Reply With Quote
  #4 (permalink)  
Old March 9th, 2012, 07:15 AM
Imar's Avatar
Wrox Author
Points: 72,045, Level: 100
Points: 72,045, Level: 100 Points: 72,045, Level: 100 Points: 72,045, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,083
Thanks: 80
Thanked 1,587 Times in 1,563 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!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

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



All times are GMT -4. The time now is 09:39 PM.


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