Page 1 of 3 123 LastLast
Results 1 to 10 of 28

Thread: Dynamic ASP.Net web controls

  1. #1
    Join Date
    Jul 2004
    Posts
    153
    Rep Power
    0

    Default Dynamic ASP.Net web controls

    Its going a bit difficult to explain..but try to visualize.

    Alright heres the scenario i'm building a page that made totally of dynamically added server controls this is relatively simple task as long as you have certain background knowledge...

    The things is there are buttons on the pages that when clicked update information on the same page(via postback) each button corresponds to a diffifernt item.

    Initially when the page loads the controls are correctly rendered...as well as for the first couple of clicks. at this point it all works fine.

    now at some point..around the forth click...things gets misplaced and throws an exception.

    code concept:
    for loop '----------------------------

    dim lblA as new label
    lblA.id = "lblA1"
    lblA.Text = "<table><tr><td>../im../picture.jpg</td><td>"

    dim lblbr as new label
    lblbr.ID = lblbr1
    lblbr.Text = "<br>"

    dim lbltbl as new lable
    lbltbl.id = lbltbl1
    lbltbl.Text = "</table>"

    dim txtbx as new textbox
    txtbox.ID = "txt1"

    dim btn as new button
    btn.ID = "btn1"

    with pnl.contols
    .add(lblA)
    .add(txtbx)
    .add(lblbr)
    .add(btn)
    .add(lbltbl)
    end with
    next '--------------------

    now at the time when things get messed up... lblbr.Text gets set to "</td></tr>" (dont know why)..this breaks the table, leaving the btn out of place...hence throwing an exception.

    Note. maintaining ViewStates are primarily dependent on two things..the position and value of a control when the state was originally saved.
    Hence my problem the value for 'lblbr' changed which resulted in position of 'btn' being chnaged.

    whats going on can someone shed some light as to why this is happening...
    Last edited by codecarnage; Mar 31, 2006 at 09:58 AM.

  2. #2
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    I think <br> is correct inside of the <td> tags. That is the corect schema if I have an updated schema

    I need to see your actual code

    Are you using the Literal control in System.Web.UI.WebControls for the strings like "<table><tr><td>" ? Or are you using the Label control? The later should not work still because it encodes the string to an html equivalent. Correct me on this (see bottom)?

    Next thing is that I cannot follow what is done with the view state. I do not see how that applies.

    http://msdn.microsoft.com/library/en...verControl.asp

    You can use a Literal Web server control to render static text into a Web page and manipulate that text using server code. Unlike the Label Web server control, the Literal control does not add any HTML elements to the text.
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  3. #3
    Join Date
    Jul 2004
    Posts
    153
    Rep Power
    0

    Default

    1. Yes indeed i did use labels...when rendered they are converted to <span>lbl.Text</span>....its a bit unconventional but it works...for the most part...it worked perfectly fine some time ago but i added more controls and fell into this problem....u can try is for yourself u'll see...more than likely it would not be a recommended solution on msdn...
    2. the viewstate comes into play because of an error...viewstates work...within some constrainst..of which value and position plays a part..
    button moved from its initial position. here's the error:
    '---------------------------------------------------------
    Failed to load viewstate. The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request. For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request.
    '---------------------------------------------------

  4. #4
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    Are you reusing the control ids? I have to ask because lblbr.Text gets set to "</td></tr>"

    BTW, "<table><tr><td>../im../picture.jpg</td><td>" has "</td>" and "</tr> missing, if that is not a mistype.

    1. Yes indeed i did use labels...when rendered they are converted to <span>lbl.Text</span>....its a bit unconventional but it works...for the most part...it worked perfectly fine some time ago but i added more controls and fell into this problem....u can try is for yourself u'll see...more than likely it would not be a recommended solution on msdn...
    I tried it and I get a span enclosing the literal of the label, OK. I still would use the literal in this case because the span may be causing trouble or could cause additional. In html 4.0, it does not allow a <table tag inside it. (I have not check html 4.1)

    2. the viewstate comes into play because of an error...viewstates work...within some constrainst..of which value and position plays a part..
    button moved from its initial position. here's the error:
    '---------------------------------------------------------
    Failed to load viewstate. The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request. For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request.
    '---------------------------------------------------
    This seems to be the real problem but I see nothing causing the order of this
    .add(lblA)
    .add(txtbx)
    .add(lblbr)
    .add(btn)
    .add(lbltbl)
    to change
    (Put the previous declarations in the same order next time before posting cause I did not realize that it was different)
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  5. #5
    Join Date
    Jul 2004
    Posts
    153
    Rep Power
    0

    Default

    crosswire take a look at this: http://aspnet.4guysfromrolla.com/articles/092904-1.aspx

    this explains the issue with postback, viewstate and dynamically added controls

  6. #6
    Join Date
    Jul 2004
    Posts
    153
    Rep Power
    0

    Default

    although i'm in the process of using a different approach - from the code-behind to html view - i'm working on ironing out the current issues...
    1. i'll be using place holders to house the controls.
    2. i'll be using literals now instead of labels to build the structure of the table.

  7. #7
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    Good Article. I read it and a couple of nested articles, still looking deeper right now

    My previous suggestion may be 90 % b.s. Can't say I know for sure cause I never tested it. The thing I never knew was the class auto-generated from the aspx page itself, also whether view state was save automatically or not for dynamically added controls, I understand it to be manual. I did know that the implementation was to be done in PageInit() and automatic filling of the view state required unique IDs. However, this, I learnt, is not the cause for the error. The reason, I believe, is that the page presented to the user had unknown controls in the form, and after posting back, the initialization event of the page cycle could not utilize post back data from the submitted form. I guess that post back data can be used to maintain state just like view state, and the corresponding controls could not be found to reload the data. Reading the articles helped whether this is still an incorrect answer.

    "lblbr.Text gets set to '</td></tr>' " Cant get this since I understand that view state is manually set.

    PlaceHolder seems very interesing, I just checked that out too.

    I think that it may not be necessary but it depends on your design. You can also explore the Table control

    You know that I still do not remember that <br> can be used in label controls * snicks
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  8. #8
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    Ohh crap my explaination is wrong because post back data may be set manually.... I do not know
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  9. #9
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    OK I stick with the explanation

    Immediately after initialization, the page framework loads the view state for the page. The view state is a collection of name/value pairs, where controls and the page itself store any information that must be persistent across Web requests. The view state represents the call context of the page. Typically, it contains the state of the controls the last time the page was processed on the server. The view state is empty the first time the page is requested in the session. By default, the view state is stored in a hidden field silently added to the page. The name of this field is __VIEWSTATE. By overriding the LoadViewState method—a protected overridable method on the Control class—component developers can control how the view state is restored and how its contents are mapped to the internal state.

    ...
    .
    .

    Once the view state has been restored, the controls in the page tree are in the same state they were the last time the page was rendered to the browser. The next step consists of updating their state to incorporate client-side changes. The postback data-processing stage gives controls a chance to update their state so that it accurately reflects the state of the corresponding HTML element on the client. For example, a server TextBox control has its HTML counterpart in an <input type=text> element. In the postback data stage, the TextBox control will retrieve the current value of <input> tag and use it to refresh its internal state. Each control is responsible for extracting values from posted data and updating some of its properties. The TextBox control will update its Text property whereas the CheckBox control will refresh its Checked property. The match between a server control and a HTML element is found on the ID of both.
    http://msdn.microsoft.com/library/de...bjectmodel.asp

    Not exactly as I explained however
    Last edited by crosswire; Apr 5, 2006 at 10:57 PM.
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  10. #10
    Join Date
    Jul 2004
    Posts
    153
    Rep Power
    0

    Default

    Yes the article is very interestig...quite good...learnt one new thing from it. the part about the autogenerated class...found it kinda weird but it makes sense.
    - I regularlly use palceholders but in this implimentation i didn't cause i was adding the controls to a panel...which is rendered as a <div>
    - i definately say i have a complete appreciation for the page life-cycle.
    -As for my problem...fixed it...made some mods. used literals instead of labels
    'literals are rendered as static text-no <span></span> -
    furthur analysis led me to realise it was more a problem with my declaration and instanciation of the controls within the loop...

Posting Permissions

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