Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.1
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 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 June 16th, 2005, 04:20 AM
Registered User
 
Join Date: Jun 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Loading User Controls Dynamically

Hi,

I am trying to dynamically load a user control into a new row of an ASP.net table on the click event of a button. This works the first time I click the button but subsequent clicks of the button fail to add new rows.

I realise I need to re-add the rows that previously existed in the table, and from what I have read the best place to do this is the Page_Init because then the usercontrol should utilise its viewstate to pre-populate itself, but since the rows are added dynamically they don't exist within the Page_Init method when the button is clicked for a second time.

Does anyone know the best way to do this??

My code is this:

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
        LoadFilters()
    End Sub

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here

    End Sub

    Private Sub btnAddFilter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddFilter.Click
        AddFilter()
    End Sub

    Private Sub LoadFilters()
        Dim row As TableRow
        Dim i As Int32
        Dim hidFilter As HtmlInputHidden

        hidFilter = FindControl("hidFilterCount")

        For i = 1 To Convert.ToInt32(hidFilter.Value)
            AddFilter()
        Next
    End Sub

    Private Sub AddFilter()
        Dim trSearchFilter As New TableRow
        Dim tdSearchFilter As New TableCell
        Dim ucSearchFilter As New AdminSearchFilter

        ucSearchFilter = LoadControl("~\UserControls\AdminSearchFilter.ascx ")
        tdSearchFilter.Controls.Add(ucSearchFilter)

        trSearchFilter.Cells.Add(tdSearchFilter)
        tblSearchFilters.Rows.Add(trSearchFilter)

        hidFilterCount.Value = Convert.ToInt32(hidFilterCount.Value) + 1

        trSearchFilter.Dispose()
        tdSearchFilter.Dispose()
    End Sub

Regards,

Matt
 
Old June 20th, 2005, 12:08 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

I think maybe your hidfilter variable may be getting reset.

 
Old June 21st, 2005, 05:41 AM
Registered User
 
Join Date: Jun 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am pretty certain you're right. Basically the problem is keeping track of how many rows have been added to the table. The viewstate doesn't seem to maintain this.

Does anyone know how to do this?





Similar Threads
Thread Thread Starter Forum Replies Last Post
User controls' content: Chapter 2 User Controls AGS BOOK: Professional ASP.NET 2.0 Server Control and Component Development ISBN: 978-0-471-79350-2 10 July 26th, 2007 05:36 AM
Need urgent help loading user controls at runtime rpeters83 ASP.NET 2.0 Professional 7 September 3rd, 2006 10:17 AM
Loading forms dynamically bmains C# 2005 1 November 30th, 2005 03:53 PM
Loading and Destroying web user controls into a pa see07 ASP.NET 1.x and 2.0 Application Design 5 February 4th, 2005 04:05 PM
Loading user control dynamically from code-behind jacob ASP.NET 1.0 and 1.1 Basics 2 April 12th, 2004 06:06 PM





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