Strychtur
asked on
Display order of asp.net dynamic multiple instances user controls
Hi
I have a from with a button and a placeholder. I am adding a dynamic usercontrol to the page
every time the button is clicked. When the button is first clicked the control is added into the holder.
When it is clicked a second time another user control appears under the first. What I need to do is change that order. When the button is clicked a second time I need to add the control in the holder on top of the first on instead below. So that the last control added is always first.
I use viewstate to track number of control added and Page_PreLoad to add the controls.
Click event of button
Page_PreLoad
I'm not sure Im even doing this correctly
I have a from with a button and a placeholder. I am adding a dynamic usercontrol to the page
every time the button is clicked. When the button is first clicked the control is added into the holder.
When it is clicked a second time another user control appears under the first. What I need to do is change that order. When the button is clicked a second time I need to add the control in the holder on top of the first on instead below. So that the last control added is always first.
I use viewstate to track number of control added and Page_PreLoad to add the controls.
Click event of button
Protected Sub btnAddTestRun_Click(sender As Object, e As System.EventArgs) Handles btnAddTestRun.Click
If (CInt(ViewState("TestRunCounter")) = 0) Then
Dim ts As UserControls_TestRun = CType(Page.LoadControl("~/UserControls/TestRun.ascx"), UserControls_TestRun)
ts.HeaderId = 12419
phTestRun.Controls.Add(ts)
End If
ViewState("TestRunCounter") = CStr(CInt(ViewState("TestRunCounter")) + 1)
End Sub
Page_PreLoad
Protected Sub Page_PreLoad(sender As Object, e As System.EventArgs) Handles Me.PreLoad
If (CInt(ViewState("TestRunCounter")) > 0) Then
For trc As Int32 = 0 To ViewState("TestRunCounter")
Dim ts As UserControls_TestRun = CType(Page.LoadControl("~/UserControls/TestRun.ascx"), UserControls_TestRun)
ts.HeaderId = 12419
phTestRun.Controls.Add(ts)
Next
End If
If (ViewState("TestRunCounter") = Nothing) Then
ViewState("TestRunCounter") = "0"
End If
End Sub
I'm not sure Im even doing this correctly
Instead of using Add, use AddAt() which allows you to specify the index. 0 would put it at the top and move the other controls down a notch.
http://msdn.microsoft.com/en-us/library/system.web.ui.controlcollection.addat(v=VS.71).aspx
http://msdn.microsoft.com/en-us/library/system.web.ui.controlcollection.addat(v=VS.71).aspx
ASKER
Hi
I think I may need a combination of both
Here is what I need to accomplish.
Lets say each newly added control represents a test run.
When the first one is added I check the db for the last test run
for that client then I add 1 to it.
So then user fills in test run hits save. test run is saved.
When user clicks add another test run I want to add another test run user control with the right number but still keep the first one on the page. I hope this is clear.
I'm not sure how to load an object or control with is data into memory then reload it
Need URGENT help please
Thanks for the time
I think I may need a combination of both
Here is what I need to accomplish.
Lets say each newly added control represents a test run.
When the first one is added I check the db for the last test run
for that client then I add 1 to it.
So then user fills in test run hits save. test run is saved.
When user clicks add another test run I want to add another test run user control with the right number but still keep the first one on the page. I hope this is clear.
I'm not sure how to load an object or control with is data into memory then reload it
Need URGENT help please
Thanks for the time
You'll need to store the list of testruns to be stored in the ControlState, that way you can add the controls again dynamically when reloading the page. This is one of the most difficult subjects in the ASP.NET page lifecycle. If you search Google/Bing for "Dynamically created controls postback (viewstate OR controlstate)" you should find a lot of atricles pointing you in the right direction.
It basically comes down to the fact that you need to store enough information on the controls in the ControlState (or ViewState isa you're using an older version of the Framework) to restore the control tree upon postback. You only need to create the controls and give them the same ID as you gave them before the postback. The controls should then automatically load their values from the postback data and the viewstate/controlstate.
It basically comes down to the fact that you need to store enough information on the controls in the ControlState (or ViewState isa you're using an older version of the Framework) to restore the control tree upon postback. You only need to create the controls and give them the same ID as you gave them before the postback. The controls should then automatically load their values from the postback data and the viewstate/controlstate.
ASKER
Thanks
I did some research but could not find anything on multiple instances of the same dynamic user control. control. and I still don't understand how I can load a control from viewstate or controlstate
I did some research but could not find anything on multiple instances of the same dynamic user control. control. and I still don't understand how I can load a control from viewstate or controlstate
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks.
Ok here is what I have
When I try TestRuns.Add(RunCounter) if errors Object reference not set to an instance of an object
not sure why
Ok here is what I have
Protected Sub btnAddTestRun_Click(sender As Object, e As System.EventArgs) Handles btnAddTestRun.Click
Dim testRunCtnl As UserControls_TestRun = CType(Page.LoadControl("~/UserControls/TestRun.ascx"), UserControls_TestRun)
testRunCtnl.HeaderId = 12419 'pumpTestBO.PumpTestID
testRunBO = New TestRunBO()
testRunCtnl.TestNum = testRunBO.GetNewRunNumber(testRunCtnl.HeaderId)
RunCouter += 1
testRunCtnl.ID = "TestRun_" + RunCouter.ToString
phTestRun.Controls.AddAt(0, testRunCtnl)
TestRuns.Add(RunCouter)
End Sub
Click event of buttonProtected Overrides Sub CreateChildControls()
If Not TestRuns Is Nothing Then
If TestRuns.Count > 0 Then
TestRuns.Reverse()
For Each testrun In TestRuns
'Dim control = DirectCast(UserControl.LoadControl("~/path/to/YourUserControl.ascx"), YourUserControl)
Dim testRunCtnl As UserControls_TestRun = CType(Page.LoadControl("~/UserControls/TestRun.ascx"), UserControls_TestRun)
testRunCtnl.ID = "TestRun_" & testrun
phTestRun.Controls.Add(testRunCtnl)
Next
End If
End If
End Sub
ChildOverRidePublic ReadOnly Property TestRuns() As List(Of Integer)
Get
If ViewState("TestRuns") IsNot Nothing Then
ViewState("TestRuns") = New List(Of Integer)()
End If
Return DirectCast(ViewState("TestRuns"), List(Of Integer))
End Get
End Property
ListWhen I try TestRuns.Add(RunCounter) if errors Object reference not set to an instance of an object
not sure why
ASKER
TestRuns is nothing Why?
If ViewState("TestRuns") IsNot Nothing Then
ViewState("TestRuns") = New List(Of Integer)()
End If
should be
If ViewState("TestRuns") Is Nothing Then
ViewState("TestRuns") = New List(Of Integer)()
End If
ViewState("TestRuns") = New List(Of Integer)()
End If
should be
If ViewState("TestRuns") Is Nothing Then
ViewState("TestRuns") = New List(Of Integer)()
End If
when you the second one by default it will come the second
so what u have to do is to make some replacements
like
copy the first one added into a temporary object
add the second
then restore the first object and add it .....