DeveloperBarn Forums

DeveloperBarn

Programming & IT forum

Dynamically added user control, not getting it's values

This is a discussion on Dynamically added user control, not getting it's values within the .Net Development forums, part of the Programming & Scripting category; Hi guys I have a aspx page ajax enabled and using update panels, ModalpopupExtenders and on one of my ModalPopupextenders ...

Go Back   DeveloperBarn Forums > Programming & Scripting > .Net Development

  #1  
Old October 1st, 2008, 09:12 AM
Shem's Avatar
Barn Enthusiast
 
Join Date: Mar 2008
Posts: 292
Rep Power: 2
Shem is on a distinguished road
Default Dynamically added user control, not getting it's values

Hi guys

I have a aspx page ajax enabled and using update panels, ModalpopupExtenders
and on one of my ModalPopupextenders either one or more user controls are
called up into the displayed panel of the Modalpopup.

It loads only the controls that are assigned, to do this I assign the controls via
the repeaters OnItemDataBound event:
Code:
Protected Sub rpActiveSpecification_OnItemDataBound(ByVal s As Object, ByVal e As RepeaterItemEventArgs)
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            '---------------------------------------
            Dim myForms As Forms = e.Item.DataItem
            Dim pnlActiveSpecificationForms As Panel = e.Item.FindControl("pnlActiveSpecificationForms")


            ViewState("FormsArt") = "".ToString
            ViewState("FormsCarpetRug") = "".ToString
            ViewState("FormsAccessories") = "".ToString
            ViewState("FormsBedding") = "".ToString
            ViewState("FormsCasegoods") = "".ToString
            ViewState("FormsDrapery") = "".ToString
            ViewState("FormsFabric") = "".ToString
            ViewState("FormsLighting") = "".ToString
            ViewState("FormsMirrors") = "".ToString
            ViewState("FormsPillow") = "".ToString
            ViewState("FormsTrims") = "".ToString
            ViewState("FormsUpholstered") = "".ToString
            ViewState("FormsArchitectschedule") = "".ToString

            '---------------------------------------
            Select Case myForms.Name
                Case "Art"
                    FormsArt = CType(LoadControl("~/CustomControls/FormControls/form_art.ascx"), ASP.customcontrols_formcontrols_form_art_ascx)
                    pnlActiveSpecificationForms.Controls.Add(FormsArt)
                    FormsArt.SpecID = myParams.SpecID
                    ViewState("FormsArt") = "~/CustomControls/FormControls/form_art.ascx".ToString
                Case "Carpet Rug"
                    FormsCarpetRug = CType(LoadControl("~/CustomControls/FormControls/form_carpetrug.ascx"), ASP.customcontrols_formcontrols_form_carpetrug_ascx)
                    pnlActiveSpecificationForms.Controls.Add(FormsCarpetRug)
                    FormsCarpetRug.SpecID = myParams.SpecID
                    ViewState("FormsCarpetRug") = "~/CustomControls/FormControls/form_carpetrug.ascx".ToString
                Case "Accessories"
                    FormsAccessories = CType(LoadControl("~/CustomControls/FormControls/form_accessories.ascx"), ASP.customcontrols_formcontrols_form_accessories_ascx)
                    pnlActiveSpecificationForms.Controls.Add(FormsAccessories)
                    FormsAccessories.SpecID = myParams.SpecID
                    ViewState("FormsAccessories") = "~/CustomControls/FormControls/form_accessories.ascx".ToString
                Case "Bedding"
                    FormsBedding = CType(LoadControl("~/CustomControls/FormControls/form_bedding.ascx"), ASP.customcontrols_formcontrols_form_bedding_ascx)
                    pnlActiveSpecificationForms.Controls.Add(FormsBedding)
                    FormsBedding.SpecID = myParams.SpecID
                    ViewState("FormsBedding") = "~/CustomControls/FormControls/form_bedding.ascx".ToString
                Case "Casegoods"
                    FormsCasegoods = CType(LoadControl("~/CustomControls/FormControls/form_casegoods.ascx"), ASP.customcontrols_formcontrols_form_casegoods_ascx)
                    pnlActiveSpecificationForms.Controls.Add(FormsCasegoods)
                    FormsCasegoods.SpecID = myParams.SpecID
                    ViewState("FormsCasegoods") = "~/CustomControls/FormControls/form_casegoods.ascx".ToString
                Case "Drapery"
                    FormsDrapery = CType(LoadControl("~/CustomControls/FormControls/form_drapery.ascx"), ASP.customcontrols_formcontrols_form_drapery_ascx)
                    pnlActiveSpecificationForms.Controls.Add(FormsDrapery)
                    FormsDrapery.SpecID = myParams.SpecID
                    ViewState("FormsDrapery") = "~/CustomControls/FormControls/form_drapery.ascx".ToString
                Case "Fabric"
                    FormsFabric = CType(LoadControl("~/CustomControls/FormControls/form_fabric.ascx"), ASP.customcontrols_formcontrols_form_fabric_ascx)
                    pnlActiveSpecificationForms.Controls.Add(FormsFabric)
                    FormsFabric.SpecID = myParams.SpecID
                    ViewState("FormsFabric") = "~/CustomControls/FormControls/form_fabric.ascx".ToString
                Case "Lighting"
                    FormsLighting = CType(LoadControl("~/CustomControls/FormControls/form_lighting.ascx"), ASP.customcontrols_formcontrols_form_lighting_ascx)
                    pnlActiveSpecificationForms.Controls.Add(FormsLighting)
                    FormsLighting.SpecID = myParams.SpecID
                    ViewState("FormsLighting") = "~/CustomControls/FormControls/form_lighting.ascx".ToString
                Case "Mirrors"
                    FormsMirrors = CType(LoadControl("~/CustomControls/FormControls/form_mirrors.ascx"), ASP.customcontrols_formcontrols_form_mirrors_ascx)
                    pnlActiveSpecificationForms.Controls.Add(FormsMirrors)
                    FormsMirrors.SpecID = myParams.SpecID
                    ViewState("FormsMirrors") = "~/CustomControls/FormControls/form_mirrors.ascx".ToString
                Case "Pillow"
                    FormsPillow = CType(LoadControl("~/CustomControls/FormControls/form_pillow.ascx"), ASP.customcontrols_formcontrols_form_pillow_ascx)
                    pnlActiveSpecificationForms.Controls.Add(FormsPillow)
                    FormsPillow.SpecID = myParams.SpecID
                    ViewState("FormsPillow") = "~/CustomControls/FormControls/form_pillow.ascx".ToString
                Case "Trims"
                    FormsTrims = CType(LoadControl("~/CustomControls/FormControls/form_trims.ascx"), ASP.customcontrols_formcontrols_form_trims_ascx)
                    pnlActiveSpecificationForms.Controls.Add(FormsTrims)
                    FormsTrims.SpecID = myParams.SpecID
                    ViewState("FormsTrims") = "~/CustomControls/FormControls/form_trims.ascx".ToString
                Case "Uplholstered"
                    FormsUpholstered = CType(LoadControl("~/CustomControls/FormControls/form_upholstereditems.ascx"), ASP.customcontrols_formcontrols_from_upholstereditems_ascx)
                    pnlActiveSpecificationForms.Controls.Add(FormsUpholstered)
                    FormsUpholstered.SpecID = myParams.SpecID
                    ViewState("FormsUpholstered") = "~/CustomControls/FormControls/form_upholstereditems.ascx".ToString
                Case "Architectural Schedule"
                    FormsArchitectschedule = CType(LoadControl("~/CustomControls/FormControls/form_architectschedule.ascx"), ASP.customcontrols_formcontrols_form_architectschedule_ascx)
                    pnlActiveSpecificationForms.Controls.Add(FormsArchitectschedule)
                    FormsArchitectschedule.SpecID = myParams.SpecID
                    ViewState("FormsArchitectschedule") = "~/CustomControls/FormControls/form_architectschedule.ascx".ToString
            End Select
        End If
    End Sub
ok so now i have my user control to capture.
when i hit my update button, which resides in the display panel of the
ModalPopup, not in the user control, so when the update button is clicked
I call another sub which reloads the control(this is due to the controls being dynamically added) and calls the update function of that control:
Code:
Public Sub btnActiveSpecificationUpdate_OnClick(ByVal s As Object, ByVal e As EventArgs)
        Dim myButton As Button = s

        '---------------------------------------
        'decalre 
        Dim pnlActiveSpecification As Panel = myButton.Parent

        If CType(ViewState("FormsArt"), String) > "" Then
            FormsArt = CType(LoadControl("~/CustomControls/FormControls/form_art.ascx"), ASP.customcontrols_formcontrols_form_art_ascx)
            FormsArt.Update_Form()
        End If
    End Sub
and here is the update function of the user control:
Code:
Public Sub Update_Form()
        Dim myArt As Art = New Art
        Dim fieldlist As New ArrayList

        '---------------------------------------
        'get all data
        myArt = ArtManager.GetItem(SpecID)

        '---------------------------------------
        'check to see if something has been updated
        If txtArt_material.Text <> myArt.Material Then
            fieldlist.Add("material")
            myArt.Material = txtArt_material.Text
        End If
        If txtArt_colour.Text <> myArt.Colour Then
            fieldlist.Add("colour")
            myArt.Colour = txtArt_colour.Text
        End If
        If txtArt_supplierref.Text <> myArt.SupplierRef Then
            fieldlist.Add("supplierref")
            myArt.SupplierRef = txtArt_supplierref.Text
        End If
        If txtArt_finish.Text <> myArt.Finish Then
            fieldlist.Add("finish")
            myArt.Finish = txtArt_finish.Text
        End If
        If txtArt_width.Text <> myArt.Width Then
            fieldlist.Add("width")
            myArt.Width = txtArt_width.Text
        End If
        If txtArt_height.Text <> myArt.Height Then
            fieldlist.Add("height")
            myArt.Height = txtArt_height.Text
        End If
        If txtArt_framewidth.Text <> myArt.FrameWidth Then
            fieldlist.Add("framewidth")
            myArt.FrameWidth = txtArt_framewidth.Text
        End If
        If txtArt_innerframe.Text <> myArt.InnerFrame Then
            fieldlist.Add("innerframe")
            myArt.InnerFrame = txtArt_innerframe.Text
        End If
        If txtArt_glassthickness.Text <> myArt.GlassThickness Then
            fieldlist.Add("glassthickness")
            myArt.GlassThickness = txtArt_glassthickness.Text
        End If
        If txtArt_hangingrequirements.Text <> myArt.HangingRequirements Then
            fieldlist.Add("hangingrequirements")
            myArt.HangingRequirements = txtArt_hangingrequirements.Text
        End If

        myArt.SpecID = SpecID

        '---------------------------------------
        'check to see if any records need to be updated, if so, update them
        If Not fieldlist.Count = 0 Then
            ArtManager.Update(myArt)
            fieldlist.RemoveRange(0, fieldlist.Count)
        End If
    End Sub
but nothing get's passed, all the input fields from the user control are empty?
any ideas here?

Shem
Reply With Quote
  #2  
Old October 1st, 2008, 09:44 AM
jmurrayhead's Avatar
The Barnfather
 
Join Date: Mar 2008
Real name: Jason
Location: Washington, D.C.
Posts: 1,962
Blog Entries: 8
Rep Power: 15
jmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud of
Default

I'm not even going to attempt to sort through all that code lol

Keep in mind that ASP.net does not maintain the control tree of dynamically added controls. If you dynamically add a control, then you must maintain the viewstate. Every time the page is reloaded (postback, partial postback), you will have to grab the values from your viewstate.

CodeProject: Retaining State for Dynamically Created Controls in ASP.NET applications. Free source code and programming help
__________________
jmurrayhead
If you agree with me... click the icon!
If my post solved your problem, click the button in the lower right-hand corner of the post.

If you like it here...throw us a few bones to help
support us.

Join our Folding team: DeveloperBarn Folding

Reply With Quote
  #3  
Old October 2nd, 2008, 05:20 AM
Shem's Avatar
Barn Enthusiast
 
Join Date: Mar 2008
Posts: 292
Rep Power: 2
Shem is on a distinguished road
Default

my problem i have is that i need to recreate the controls in the Page_load
event.

But i need to recreate and add the control to a panel that sits in a Grandchild
repeater:
Code:
<asp:Panel ID="pnlActiveContent" runat="server" CssClass="collapsePanel">
	<asp:Repeater id="rpActiveCategory" runat="server" OnItemDataBound="rpActiveCategory_OnItemDataBound">
		<asp:Panel ID="pnlActiveCategoryContent" runat="server" Visible="false">
			<asp:Repeater id="rpActiveSubCategory" runat="server" OnItemDataBound="rpActiveSubCategory_OnItemDataBound">
				<asp:Panel ID="pnlActiveSubCategoryContent" runat="server" height="100%" CssClass="collapsePanel">
					<asp:Repeater ID="rpActiveSpecs" runat="server" OnItemDataBound="rpActiveSpecs_OnItemDataBound" OnItemCommand="rpActiveSpecs_OnItemCommand">
						<asp:Panel ID="pnlActiveSpecification" runat="server" CssClass="modalPopup" style="display:none;">
'pnlActiveSpecification' is the panel that they must be added to.
'pnlActiveSpecification' is the display panel for a ModalPopupExtender.

any ideas?
Shem
Reply With Quote
  #4  
Old October 2nd, 2008, 07:52 AM
jmurrayhead's Avatar
The Barnfather
 
Join Date: Mar 2008
Real name: Jason
Location: Washington, D.C.
Posts: 1,962
Blog Entries: 8
Rep Power: 15
jmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud of
Default

Are you still having problems with retaining values after update or is your problem now adding the control to the panel inside the nested repeater?
Reply With Quote
  #5  
Old October 2nd, 2008, 08:11 AM
Shem's Avatar
Barn Enthusiast
 
Join Date: Mar 2008
Posts: 292
Rep Power: 2
Shem is on a distinguished road
Default

after a lot of research and reading yesterday, I first need to reload the control
before i can get the results, so until i'm able to reload my control, i guess i will
not ever get or retain any values.
Reply With Quote
  #6  
Old October 2nd, 2008, 08:14 AM
jmurrayhead's Avatar
The Barnfather
 
Join Date: Mar 2008
Real name: Jason
Location: Washington, D.C.
Posts: 1,962
Blog Entries: 8
Rep Power: 15
jmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud of
Default

Quote:
Originally Posted by Shem View Post
after a lot of research and reading yesterday, I first need to reload the control
before i can get the results, so until i'm able to reload my control, i guess i will
not ever get or retain any values.
I guess what my real question is...why do you need to add the control programmatically? Can't you just stick it inside the repeater in design view and run it that way? By doing so, ASP.net will maintain the viewstate of the control.
Reply With Quote
  #7  
Old October 2nd, 2008, 08:18 AM
Shem's Avatar
Barn Enthusiast
 
Join Date: Mar 2008
Posts: 292
Rep Power: 2
Shem is on a distinguished road
Default

I have 12 different user controls, each of these capture diff data.
each subcategory will get either one or more of these user controls assigned
to it, so I can't stick all of them inside my repeater, unless I write a 'If Else'
statement inside my repeater on the aspx page, which i suppose i could do?
Reply With Quote
  #8  
Old October 2nd, 2008, 08:21 AM
jmurrayhead's Avatar
The Barnfather
 
Join Date: Mar 2008
Real name: Jason
Location: Washington, D.C.
Posts: 1,962
Blog Entries: 8
Rep Power: 15
jmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud of
Default

Quote:
Originally Posted by Shem View Post
I have 12 different user controls, each of these capture diff data.
each subcategory will get either one or more of these user controls assigned
to it, so I can't stick all of them inside my repeater, unless I write a 'If Else'
statement inside my repeater on the aspx page, which i suppose i could do?
You could always do it within the control's .ascx.vb page (i.e. build one control and use conditional to display the appropriate parts). OR you could do it in the repeater ItemDataBound event.
Reply With Quote
  #9  
Old October 2nd, 2008, 08:24 AM
Shem's Avatar
Barn Enthusiast
 
Join Date: Mar 2008
Posts: 292
Rep Power: 2
Shem is on a distinguished road
Default

currently I am loading the controls in the repeaters OnItemDataBound
Quote:
Originally Posted by Shem
It loads only the controls that are assigned, to do this I assign the controls via
the repeaters OnItemDataBound event:
And the page loading the controls is also a user control, sitting on a aspx page.
Reply With Quote
  #10  
Old October 2nd, 2008, 08:28 AM
jmurrayhead's Avatar
The Barnfather
 
Join Date: Mar 2008
Real name: Jason
Location: Washington, D.C.
Posts: 1,962
Blog Entries: 8
Rep Power: 15
jmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud of
Default

Right...what I meant was either place all of the user controls in the repeater, then on ItemDataBound, set the visibility of the appropriate control.

However, if you build just one control to perform the functionality of all the controls you're trying to load, then you can just run a conditional inside the .vb file for that control instead of setting the visibility of all the controls during ItemDataBound.

Comments on this post
Shem agrees: Thanked Post
Reply With Quote
The Following User Says Thank You to jmurrayhead For This Useful Post:
Shem (October 2nd, 2008)
Reply

  DeveloperBarn Forums > Programming & Scripting > .Net Development

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads

Thread Thread Starter Forum Replies Last Post
Dynamically Linked Drop-Down Boxes Rebelle JavaScript Programming 4 September 26th, 2008 12:00 PM
update gridview dynamically peebman2000 .Net Development 27 May 8th, 2008 10:03 PM
[Feedback] User Title Suggestions jmurrayhead Suggestions & Feedback 15 March 21st, 2008 12:54 PM


All times are GMT -4. The time now is 06:20 PM.


Copyright ©2008-2010, DeveloperBarn

Content Relevant URLs by vBSEO 3.3.2