Results 1 to 7 of 7

Thread: Programmatically-added user controls misbehaving

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2007
    Location
    Shawnee KS
    Posts
    6

    Programmatically-added user controls misbehaving

    Hi all,

    I'm using ASP.Net 1.1 / VB2003. I wrote a user control (ascx), which has a checkbox, a user-enterable textbox, a label, and a linkbutton. I have a page which programmatically pulls in about a dozen of these ascx's depending on the database, placing them at a PlaceHolder inside a Panel. This is done in a sub I call from Page_Init, which is where 2 web sites warned me I had to do it.

    For any number of the displayed user controls, I can check off the checkbox, enter a comment in the textbox, and click the page's SAVE button. I have the logic you'd expect: a FOR loop going thru the controls in the PlaceHolder.Controls collection, seeing if the myCtl.Checked is True, and updating the comment to the database. So far so good... everything initially comes up great, and updates the database.

    The problem is, when the page is redisplayed, my checkmark and typed-in comment are still displayed at the point on the page that I typed 'em in at. This despite the fact that when the page is redisplayed a temporary javascript alert msgbox shows me that there are zero of my ascx controls at the placeholder (I had a controls.clear for a while anyway), and I the sub reloads all of the controls with their checked's = False and Comment's = "".
    Does anybody know what I might be missing, or if there's a way to force the display of the page to reflect the controls that have actually been loaded?

    (And, I'm curious, why is my collection of user controls being cleared out on every page redisplay? EnableViewState=True for the ascx and all of its controls, plus my main page's panel & placeholder.)

    I appreciate any help you might offer... thanks. LenexaKS

  2. #2
    Hyperactive Member
    Join Date
    Jul 2001
    Location
    UK
    Posts
    271

    Re: Programmatically-added user controls misbehaving

    the list is 'cleared out' every time the page is redisplayed because when the page is redisplayed the page is being rendered again.

    This also seem a long winded way to do this, you could acheive this simpley and more effectively, by using a repeater or a datalist.

    This will give you better control over all the controls and also it should render quicker.
    §tudz

    Studzworld.com - Portfolio

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Programmatically-added user controls misbehaving

    You mentioned that the controls were not being cleared out, but then they are being cleared out.

    Without an answer to that, my guess is that because you have placed this code in the Init, the controls are rendered, and then the viewstate manager kicks in. It sees that the controls from last time exist, so it populates them.

    The websites told you to place them in the Init method so that you could preserve postback. But is that what you wanted?

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2007
    Location
    Shawnee KS
    Posts
    6

    Re: Programmatically-added user controls misbehaving

    Hi Mendhak,

    Sorry for the confusing phrasing.

    Every time my page is redisplayed, it goes thru Page_Init like you'd expect. I modified Page_Init to call a sub of mine which adds my user controls at a place holder. Just before I start adding, myPlaceHolder.Controls.Count = 0. This surprised me, but it doesn't actually hurt anything. After I programmatically add the controls, the count is, say, 12.

    If, for a few of the 12 controls, I check off the checkbox and enter a comment and click SAVE, my program is able to recognize which ones have been checked off, and update the database accordingly, so that's ok too. The page then goes thru Page_Init, which rebuilds the same list, and the page is redisplayed.

    The problem is that the controls which I checked off and entered a comment for are being displayed with my checkmark and comment still there. This despite the previous set of user controls being gone (as I verified by checking the Count property), and a new (identical-looking) set of controls added.

    This could be something very simple or embarrassingly stupid I'm doing.

    Thanks... Mike.

  5. #5
    Hyperactive Member
    Join Date
    Jul 2001
    Location
    UK
    Posts
    271

    Re: Programmatically-added user controls misbehaving

    this does sound like a viewstate issue, as the end of your sub use responce.redirect("yourpage.aspx")

    this will redirect the user to a 'clean' page
    §tudz

    Studzworld.com - Portfolio

  6. #6

    Thread Starter
    New Member
    Join Date
    Apr 2007
    Location
    Shawnee KS
    Posts
    6

    Re: Programmatically-added user controls misbehaving

    That did it, thanks! (Obvious once you think of it, which I probably never would have.) Next time I do something like this I'll look into using a repeater or datalist as you suggested.

  7. #7
    Hyperactive Member
    Join Date
    Jul 2001
    Location
    UK
    Posts
    271

    Re: Programmatically-added user controls misbehaving

    glad it worked
    §tudz

    Studzworld.com - Portfolio

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width