我有一个动态添加到页面的用户控件。当我单击该用户控件上的按钮时:
这是我的用户控件上的按钮单击事件:
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
以下是动态添加控件:
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.AttributeValue = 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
答案 0 :(得分:2)
向页面动态添加控件时,必须确保在每次访问时将控件添加到页面 。在第一次访问时添加动态控件但在回发时不是一个常见的错误 - 这不起作用!您需要在每次访问时将动态控件添加到控件层次结构中。
在使用动态创建的控件之前,我建议阅读以下几个资源:
快乐编程!
答案 1 :(得分:0)