Upload and dropdownlist from folder

Discussion in 'ASP .Net' started by Bob, Apr 28, 2006.

  1. Bob

    Bob Guest

    I've made an upload function saving to a specific folder and dropdownlist
    reading and listing files from the folder.
    When I upload, the list is rendered a second time in the dropdownlist with
    the uploaded file only being displayed in the second list. I would like to
    avoid the dropdown being filled twice but my knowledge is still pretty
    limited so I would be gratefull for help. Also if someone could perhaps
    point me to how I make the just uploaded file the selected one in the
    dropdownlist it would be perfect. It should be used later on in a Formview
    inserting records into a db. But I need to get over this first issue before
    I move on.

    Here's some code I guess it's the Formview1_PreRender stuff that's the issue
    it's logical that it renders it twice but what I should do instead I cannot
    figure out.

    <script language="VB" runat="server">
    Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim FileUpLoad1 As FileUpload =
    CType(FormView1.FindControl("FileUpLoad1"), FileUpload)
    Dim Labeltxt As Object = FormView1.FindControl("Label1")
    If FileUpLoad1.HasFile Then
    'Uncomment this line to Save the uploaded file
    FileUpLoad1.SaveAs("d:\web\pics\" & FileUpLoad1.FileName)
    Labeltxt.Text = "Received " & FileUpLoad1.FileName & " Content
    Type " & FileUpLoad1.PostedFile.ContentType & " Length " &
    FileUpLoad1.PostedFile.ContentLength
    Else
    Labeltxt.Text = "No uploaded file"
    End If

    End Sub

    Sub Formview1_PreRender(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles FormView1.PreRender

    Dim di As New System.IO.DirectoryInfo("d:\web\pics\")
    Dim fiArr As System.IO.FileInfo() = di.GetFiles()
    Dim fi As System.IO.FileInfo
    If FormView1.CurrentMode = FormViewMode.Edit Or
    FormView1.CurrentMode = FormViewMode.Insert Then
    Dim SectionDropDownList As DropDownList
    SectionDropDownList = FormView1.FindControl("PicTextBox")
    SectionDropDownList.Items.Insert(0, New ListItem("--- Choose
    Picture ---"))

    For Each fi In fiArr
    SectionDropDownList.Items.Add("/pics/" + fi.Name)
    Next fi

    End If
    End Sub
    </script>
    <form id="form1" runat="server">
    <asp:FormView ID="FormView1" DefaultMode="Insert" runat="server">
    <InsertItemTemplate>
    <asp:DropDownList ID="PicTextBox" runat="server">
    </asp:DropDownList>
    <br />
    <asp:Label id="LabelBilledeupload" runat="server" width="70px"
    text="Upload billede:" />

    <asp:FileUpLoad id="FileUpLoad1" runat="server" /> <asp:button
    id="Button1"
    runat="server"
    text="Upload billede"
    onclick="Button1_Click" /><br />
    <asp:Label id="Label1" runat="server" />
    <asp:LinkButton ID="InsertButton" runat="server"
    CausesValidation="True" CommandName="Insert"
    Text="Insert">
    </asp:LinkButton>
    <asp:LinkButton ID="InsertCancelButton" runat="server"
    CausesValidation="False" CommandName="Cancel"
    Text="Cancel">
    </asp:LinkButton>
    </InsertItemTemplate>
    </asp:FormView>
    </form>
    Bob, Apr 28, 2006
    #1
    1. Advertising

  2. Bob

    Arsalan Guest

    Use Page.IsPostback method to check if the page is loaded for first time or
    not.

    About selecting the file, iterate through the Dropdownlist's Listitem
    collection and check for the file which u've uploaded, and use selectedindex
    to set the item.

    "Bob" <> wrote in message
    news:...
    > I've made an upload function saving to a specific folder and dropdownlist
    > reading and listing files from the folder.
    > When I upload, the list is rendered a second time in the dropdownlist with
    > the uploaded file only being displayed in the second list. I would like to
    > avoid the dropdown being filled twice but my knowledge is still pretty
    > limited so I would be gratefull for help. Also if someone could perhaps
    > point me to how I make the just uploaded file the selected one in the
    > dropdownlist it would be perfect. It should be used later on in a Formview
    > inserting records into a db. But I need to get over this first issue
    > before I move on.
    >
    > Here's some code I guess it's the Formview1_PreRender stuff that's the
    > issue it's logical that it renders it twice but what I should do instead I
    > cannot figure out.
    >
    > <script language="VB" runat="server">
    > Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
    > Dim FileUpLoad1 As FileUpload =
    > CType(FormView1.FindControl("FileUpLoad1"), FileUpload)
    > Dim Labeltxt As Object = FormView1.FindControl("Label1")
    > If FileUpLoad1.HasFile Then
    > 'Uncomment this line to Save the uploaded file
    > FileUpLoad1.SaveAs("d:\web\pics\" & FileUpLoad1.FileName)
    > Labeltxt.Text = "Received " & FileUpLoad1.FileName & " Content
    > Type " & FileUpLoad1.PostedFile.ContentType & " Length " &
    > FileUpLoad1.PostedFile.ContentLength
    > Else
    > Labeltxt.Text = "No uploaded file"
    > End If
    >
    > End Sub
    >
    > Sub Formview1_PreRender(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles FormView1.PreRender
    >
    > Dim di As New System.IO.DirectoryInfo("d:\web\pics\")
    > Dim fiArr As System.IO.FileInfo() = di.GetFiles()
    > Dim fi As System.IO.FileInfo
    > If FormView1.CurrentMode = FormViewMode.Edit Or
    > FormView1.CurrentMode = FormViewMode.Insert Then
    > Dim SectionDropDownList As DropDownList
    > SectionDropDownList = FormView1.FindControl("PicTextBox")
    > SectionDropDownList.Items.Insert(0, New ListItem("--- Choose
    > Picture ---"))
    >
    > For Each fi In fiArr
    > SectionDropDownList.Items.Add("/pics/" + fi.Name)
    > Next fi
    >
    > End If
    > End Sub
    > </script>
    > <form id="form1" runat="server">
    > <asp:FormView ID="FormView1" DefaultMode="Insert" runat="server">
    > <InsertItemTemplate>
    > <asp:DropDownList ID="PicTextBox" runat="server">
    > </asp:DropDownList>
    > <br />
    > <asp:Label id="LabelBilledeupload" runat="server" width="70px"
    > text="Upload billede:" />
    >
    > <asp:FileUpLoad id="FileUpLoad1" runat="server" /> <asp:button
    > id="Button1"
    > runat="server"
    > text="Upload billede"
    > onclick="Button1_Click" /><br />
    > <asp:Label id="Label1" runat="server" />
    > <asp:LinkButton ID="InsertButton" runat="server"
    > CausesValidation="True" CommandName="Insert"
    > Text="Insert">
    > </asp:LinkButton>
    > <asp:LinkButton ID="InsertCancelButton" runat="server"
    > CausesValidation="False" CommandName="Cancel"
    > Text="Cancel">
    > </asp:LinkButton>
    > </InsertItemTemplate>
    > </asp:FormView>
    > </form>
    >
    Arsalan, Apr 28, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. THY
    Replies:
    3
    Views:
    2,902
    Alvin Bruney
    Oct 16, 2003
  2. William LaMartin

    _vti_cnf folder in the bin folder-- a problem?

    William LaMartin, Nov 10, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    1,678
    Oliver
    Nov 10, 2003
  3. Jochen Brenzlinger
    Replies:
    5
    Views:
    902
    Roedy Green
    Sep 2, 2011
  4. btopenworld

    upload user defined folder and files??

    btopenworld, May 23, 2005, in forum: ASP General
    Replies:
    2
    Views:
    104
    Patrice
    May 23, 2005
  5. David Tran
    Replies:
    0
    Views:
    110
    David Tran
    May 6, 2006
Loading...

Share This Page