I have a User Control that has been added to the page dynamically. When I click a button on that user control:
- The button_click event is NOT being raised
- The page posts back
- The user control is removed from the page
Here's the button-click event on my User Control:
Protected Sub btnAddAttribute_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddAttribute.Click
Try
Dim attrName As String = txtAddAttributeName.Text
Dim attrValue As String = txtAddAttributeValue.Text
'ADD ATTRIBUTE TO ATTRIBUTE TABLE
putSQLData("INSERT INTO OD_Attribute_Values (AttributeName, AttributeValue) VALUES('" & attrValue & "', '" & attrName & "'")
'ADD ATTRIBUTE TO PRODUCT DATA FOR THIS PRODUCT
putSQLData("UPDATE OD_Product_Data SET VariantMapping = VariantMapping + ' | " & attrName & ":" & attrValue & "' WHERE SKU = '" & SelectedSKU & "'")
'ADD NEW ctrlAttribute TO PARENT PLACEHOLDER AND SET VALUES
Dim newAttr As New AttributeControl
newAttr.AttributeName = attrName
newAttr.AttributeValue = attrValue
Page.Controls.AddAt(Page.FindControl("phAttributes").ClientID, newAttr)
'REMOVE THIS CONTROL FROM PARENT PLACEHOLDER
Me.Dispose()
Catch ex As Exception
Common.SendError(ex.Message, "AttributeControl.btnAddAttribute_Click")
End Try
End Sub
Here is the dynamic adding of controls:
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
'WEB TAB
getAttributes()
End Sub
Protected Sub getAttributes()
Try
Dim attr As String = ""
Dim attrName As String = ""
Dim attrValue As String = ""
Dim ds As DataSet = getSQLData("SELECT VariantMapping FROM OD_Product_Data WHERE SKU='" & selectedSKU & "'")
For Each dr As DataRow In ds.Tables(0).Rows()
attr = dr(0).ToString
Next
ds = Nothing
Dim attrArr As Array = attr.Split("|")
For Each item As String In attrArr
Dim attrDetail As Array = item.Split(":")
attrName = attrDetail(0)
attrValue = attrDetail(1)
Dim ctrlAttributes As AttributeControl = LoadControl("ctrlAttribute.ascx")
ctrlAttributes.AttributeName = attrName
ctrlAttributes.Attrib开发者_JAVA百科uteValue = attrValue
ctrlAttributes.ID = "ctrlAttribute-" & attrName
phAttributes.Controls.Add(ctrlAttributes)
Next
Catch ex As Exception
SendError(ex.Message, "Default.getAttributes")
End Try
End Sub
Protected Sub btnAddAttribute_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddAttribute.Click
Dim ctrlAttributes As AttributeControl = LoadControl("ctrlAttribute.ascx")
ctrlAttributes.ID = "ctrlAttribute" & phAttributes.Controls.Count + 1
phAttributes.Controls.Add(ctrlAttributes)
End Sub
Protected Sub btnCreateAttribute_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCreateAttribute.Click
Dim ctrlAddAttribute As AddAttributeControl = LoadControl("ctrlAddAttribute.ascx")
ctrlAddAttribute.SelectedSKU = selectedSKU
ctrlAddAttribute.ID = "ctrlAddAttribute" & phAttributes.Controls.Count + 1
phAttributes.Controls.Add(ctrlAddAttribute)
End Sub
When adding controls dynamically to a page you must be certain to add the control to the page on each and every visit. It's a common mistake to add the dynamic control on the first visit but not on postbacks - this won't work! You need to add the dynamic controls to the control hierarchy on every visit.
Here are a couple of resources I recommend reading before working with dynamically-created controls:
- Dynamic Controls in ASP.NET
- Working with Dynamically Created Controls
- Truly Understanding Dynamic Controls
- Creating Dynamic Data User Interfaces
- Creating a Dynamic, Data-Driven User Interface
Happy Programming!
Neat workaround found here: http://www.justskins.com/forums/placeholder-child-of-child-49926.html.
精彩评论