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 ...
| |||||||
|
#1
| ||||
| ||||
| 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
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
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
any ideas here? Shem |
|
#2
| ||||
| ||||
| 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 |
|
#3
| ||||
| ||||
| 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 display panel for a ModalPopupExtender. any ideas? Shem |
|
#5
| ||||
| ||||
| 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. |
|
#6
| ||||
| ||||
| 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. |
|
#7
| ||||
| ||||
| 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? |
|
#8
| ||||
| ||||
| Quote:
|
|
#9
| ||||
| ||||
| currently I am loading the controls in the repeaters OnItemDataBound Quote:
|
|
#10
| ||||
| ||||
| 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. |
| The Following User Says Thank You to jmurrayhead For This Useful Post: | ||
Shem (October 2nd, 2008) | ||
![]() |
|
| Bookmarks |
| Thread Tools | |
| Display Modes | |
| |
| ||||
| 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 |