Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamically created controls and State 1

Status
Not open for further replies.

CassidyHunt

IS-IT--Management
Jan 7, 2004
688
US
I am creating fileupload controls and a button control using the below code. Everything works great for adding the controls to the form. However when I click the button all the controls disappear and nothing is available to check for upload.

Code:
<%@ Page Language="VB" MasterPageFile="~/Admin/MasterPage.master" Title="The Hunt Family -- Picture Manager" %>

<script runat="server">

Protected Sub bUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs)
             
        Dim uTable As Table = up.FindControl("uTable")
        
        For Each o As Object In uTable.Controls
            If TypeOf (o) Is System.Web.UI.HtmlControls.HtmlTableRow Then
                Dim tr As System.Web.UI.HtmlControls.HtmlTableRow = o
                For Each a As Object In tr.Controls
                    If TypeOf (a) Is System.Web.UI.HtmlControls.HtmlTableCell Then
                        Dim tc As System.Web.UI.HtmlControls.HtmlTableCell = a
                        For Each b As Object In tc.Controls
                            If TypeOf (b) Is System.Web.UI.WebControls.FileUpload Then
                                Dim fu As FileUpload = b
                                If fu.HasFile Then
                                    fu.SaveAs(Server.MapPath("../Images/PictureStore/").ToString & fu.FileName)
                                End If
                            End If
                        Next
                    End If
                Next
            End If
        Next
       
    End Sub
    
    Private Sub BuildFTPTable()
        Dim table As New Table
        table.ID = "uTable"
       
        Dim ftpSize As Integer = CInt(ConfigurationManager.AppSettings("ftpSize").ToString)
        
        For i As Integer = 1 To ftpSize
            table.Rows.Add(New TableRow)
            table.Rows(i - 1).Cells.Add(New TableCell)
            table.Rows(i - 1).Cells(0).Controls.Add(New FileUpload)
        Next
        
        Dim b As New Button
        b.Text = "Upload Files"
        AddHandler b.Click, AddressOf bUpload_Click
        table.Rows.Add(New TableRow)
        table.Rows(ftpSize).Cells.Add(New TableCell)
        table.Rows(ftpSize).Cells(0).Controls.Add(b)
        
        up.Controls.Add(table)
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        If Not Me.IsPostBack Then
            BuildFTPTable()
        End If
    End Sub

    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim table As Table = up.FindControl("uTable")
        
        If Not table Is Nothing Then
            Response.Write("I am something")
        Else
            Response.Write("I am nothing")
        End If
    End Sub
</script>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:Panel ID="up" runat="server" CssClass="Content" >
        
        <hr />
        <h1>Recent Picture Uploads</h1>
        <asp:DataList ID="recentpictures" runat="server" RepeatColumns="5" RepeatDirection="Horizontal">
            <ItemTemplate>
                <center>
                    <img src='<%# Eval("VirtualServerPath") %>' alt='<%# Eval("filename") %>' /><br />
                    <asp:Label ID="filename" runat="server" Text='<%# Eval("filename") %>' />
                </center>
            </ItemTemplate>
        </asp:DataList>
        <hr />
    </asp:Panel>
</asp:Content>

I am guessing there is a command to keep the controls available during the round trip but I do not know what that would be.

Thanks

Cassidy
 
call BuildFTPTable() in the Page_Init event not the Page_Load event. also don't check for postback
Code:
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs)
   BuildFTPTable()
End Sub

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
That was a thought I had too. I tried it and it seems hold the state of the button but not the file upload boxes. I am going to try an add an id to each upload box and see if that helps keep the state there.

Thanks

Cassidy
 
Worked like a charm. Have to make sure to put an id on all dynamic controls and instead of the html control I had above I needed to change it to web.ui.webcontrols

Thanks

Cassidy
 
just curious... since you only have 1 column with 1 control, why not just add the upload control? or, if you wanted to add other controls, why not use WebUser controls?

with WebUser controls you could create public gets/sets to access the information you need.

you could also reduce the number of loops/if statements reqiured.



Jason Meckley
Programmer
Specialty Bakers, Inc.
 
That is where this was heading. My main concern was attempting to create controls dynamically and maintain state. I figured I would have a problem with that since it was my first attempt to try it. So like all my code I always seem to over due or through in uneccessary things and widdle it down as I go.

You absolutly right though. The table and everything were left overs from my first couple attempts and it would be far easier to eliminate them. Just didn't do it while figuring out my approach.

Thanks

cassidy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top