Click to See Complete Forum and Search --> : Frustration With Dynamic Controls


Scott.Macmaster
November 4th, 2008, 03:56 PM
I'm having a really hard time trying to figure out dynamic controls. I cannot find a single good reference. Does anyone know of any? Anyway, I came across a strange behavior. Could someone explain?

I have a table that lists resources. Each row has an edit button. When clicked it'll hide the edit button and show a save button, cancel button, a drop down list and and text box. This code I had was working aside from another issue.

If assignedResource.cmdEdit.Attributes("Mode") = "Edit" Then
tableReport.Rows(row).Cells(1).Controls.Add(assignedResource.cmdEdit)
tableReport.Rows(row).Cells(1).Controls.Add(assignedResource.cmdSave)
tableReport.Rows(row).Cells(1).Controls.Add(New LiteralControl(" "))
tableReport.Rows(row).Cells(1).Controls.Add(assignedResource.cmdCancel)
tableReport.Rows(row).Cells(1).Controls.Add(New LiteralControl(" "))
assignedResource.cmdEdit.Visible = False
assignedResource.cmdSave.Visible = True
assignedResource.cmdCancel.Visible = True
tableReport.Rows(row).Cells(1).Controls.Add(assignedResource.lstResourceState)
tableReport.Rows(row).Cells(1).Controls.Add(assignedResource.txtNotes)
Else
tableReport.Rows(row).Cells(1).Controls.Add(assignedResource.cmdEdit)
tableReport.Rows(row).Cells(1).Controls.Add(assignedResource.cmdSave)
tableReport.Rows(row).Cells(1).Controls.Add(assignedResource.cmdCancel)
tableReport.Rows(row).Cells(1).Controls.Add(New LiteralControl(" "))
assignedResource.cmdEdit.Visible = True
assignedResource.cmdSave.Visible = False
assignedResource.cmdCancel.Visible = False
End If

However, I decided it would look better if I had the save/cancel buttons on the right side. Now, it won't save the edit mode status. Everytime it hits the if statement is returns nothing (which of course evaluates to false). Anyway, what is going on here.

If assignedResource.cmdEdit.Attributes("Mode") = "Edit" Then
tableReport.Rows(row).Cells(1).Controls.Add(assignedResource.lstResourceState)
tableReport.Rows(row).Cells(1).Controls.Add(assignedResource.txtNotes)
tableReport.Rows(row).Cells(1).Controls.Add(New LiteralControl(" "))
tableReport.Rows(row).Cells(1).Controls.Add(assignedResource.cmdEdit)
tableReport.Rows(row).Cells(1).Controls.Add(assignedResource.cmdSave)
tableReport.Rows(row).Cells(1).Controls.Add(assignedResource.cmdCancel)
tableReport.Rows(row).Cells(1).Controls.Add(New LiteralControl(" "))
assignedResource.cmdEdit.Visible = False
assignedResource.cmdSave.Visible = True
assignedResource.cmdCancel.Visible = True
Else
tableReport.Rows(row).Cells(1).Controls.Add(assignedResource.cmdEdit)
tableReport.Rows(row).Cells(1).Controls.Add(assignedResource.cmdSave)
tableReport.Rows(row).Cells(1).Controls.Add(assignedResource.cmdCancel)
tableReport.Rows(row).Cells(1).Controls.Add(New LiteralControl(" "))
assignedResource.cmdEdit.Visible = True
assignedResource.cmdSave.Visible = False
assignedResource.cmdCancel.Visible = False
End If


Thanks,

Bill Crawley
November 6th, 2008, 09:54 AM
In your second Example (the one not working), cmdEdit, cmdCancel and cmdSave become child controls, but in the first example they are not. So your 'If' statement needs to look a level deeper in the second example.

Scott.Macmaster
November 6th, 2008, 10:01 AM
What do you mean? They all look like child controls to me in both examples. I'm adding them all as children to the same cell.

What do you mean my if statement needs to look a level deeper?

Just occured to me that maybe I should do the if statement after I add the controls. Otherwise, there view state wouldn't have been loaded...but then why would it have worked before.


Thanks,
Scott

Bill Crawley
November 7th, 2008, 04:28 AM
The Literal Control adds another layer to your controls.

For instance if you swap these 2 lines:

tableReport.Rows(row).Cells(1).Controls.Add(New LiteralControl(" "))
tableReport.Rows(row).Cells(1).Controls.Add(assignedResource.cmdEdit)

the cmdEdit will become visible. The Literal control is masking the

assignedResource.cmdEdit.Attributes("Mode")

So with your new code:

assignedResource.XXXXXX.cmdEdit.Attributes("Mode") Where XXXXXX becomes the reference to LiteralControl.

If your still confused, right mouse click on your page (when run in browser) and View Source. Look for cmdEdit and it will show you how the ASP page has constructed cmdEdit for rendering, this will show you what to test for

Thread1
November 7th, 2008, 04:49 AM
have you tried the GridView control? because if i remember it correctly all the functionalities that you were looking for (such as edit, save, and cancel) is already there.

Scott.Macmaster
November 7th, 2008, 10:45 AM
Not really sure what you want me to look for in the generated html. It's pretty much what I had expected to see. I see the various controls plus a space where I had the LiteralControl. As I expected, after clicking edit the edit control it is no longer there (probably in the view state).

As far as masking/hiding the edit button, sometimes the edit button dissapears after the first postback but after the second postback it becomes visible and stays visible. However, this depends on how I have it coded. I've trying different approaches and can't seem to reproduce this behavior. It might be helpful if I could see the html when it does that but that's not the main issue and that is the controls not remembering it's view state when I have them arranged is certain ways.

You do seem to indicate that having a LiteralControl creates a branch in the control tree that ASP.NET manages but I don't see how that would affect anything since the ASP.NET matches a controls view state based on it's id not its location in the control tree.

Also, assignedResource is a custom class I wrote (not a control) that simply stores references to properties and controls that relate to each assigned resource. So I don't think that code example you gave is valid.

So, I'm still confused about what is going on. Could you explain some more?


Thanks,

Bill Crawley
November 11th, 2008, 02:28 AM
Hi Scott, Check this link out http://aspnet.4guysfromrolla.com/articles/092904-1.aspx it talks about Dynamic Web Controls and maintaining Viewstate on postback.

Scott.Macmaster
November 11th, 2008, 09:16 AM
Thanks, although it didn't really have anything I haven't already read a dozen times. I'm starting to think my confusion is with something else. As you suggested earlier, I think it has something to do with the LiteralControls I use to put a space between the dynamic controls. I seem to be getting consistent results from adding several literal controls with Text equal to an empty string. Then later, after I determine which dynamic controls are visible set the appropriate LiteralControls to a space. So basically, by having the same set of controls in the same order each time things seem to work correctly. However, it would be really nice if I new why.



Thanks

Bill Crawley
November 11th, 2008, 09:50 AM
Try something like this:

Dim MySpace As New Literal
MySpace.Text = " "
tableReport.Rows(row).Cells(1).Controls.Add(MySpace)