我已經嘗試了幾種不同的解決方案,發現這裏和其他網絡上的其他地方沒有運氣,迄今。也許你知道很多人可能會幫助...如何使用asp.net克隆/複製控件(使用子控件)?
我有一堆動態創建控件的用戶,我作爲控制集合存儲在會話狀態,所以我可以顯示他們在每一個回傳。
用戶生成的每個控件都是一個帶有其他控件的div。
我在每個控件上都有一個按鈕,允許用戶刪除控件或複製它。
當用戶點擊「Duplicate」時,我打電話給我處理事件的web方法。
當我的web方法找到控制被複制,我想使該控件的副本,並將其與它保存到控件集合(上激發Page_Unload)
Dim DupCtrl As Control = Nothing
Dim int As Integer = myDynControls.Count
For i = 0 To int - 1
If myDynControls(i).ID.Contains(ctrlID) Then
DupCtrl = Clone_Control(myDynControls(i))
Exit For
End If
Next
End Function
添加到頁面(函數交易
而且Clone_Control功能:
Public Shared Function Clone_Control(OriginalControl As Object) As Object
Dim type As Type = OriginalControl.[GetType]()
Dim properties As Reflection.PropertyInfo() = type.GetProperties()
Dim retObject As [Object] = type.InvokeMember("", System.Reflection.BindingFlags.CreateInstance, Nothing, OriginalControl, Nothing)
For Each propertyInfo As Reflection.PropertyInfo In properties
If propertyInfo.CanWrite Then
propertyInfo.SetValue(retObject, propertyInfo.GetValue(OriginalControl, Nothing), Nothing)
End If
Next
Return retObject
End Function
不幸的是,啓動PropertyInfo.SetValue行.... 總是用錯誤:
"Exception has been thrown by the target of an invocation."
,當我看的InnerException:
"Cannot get inner content of dynDiv_FormCtrl_Wrapper_10432 because the contents are not literal."
任何人都可以請幫我指出了正確的方向得到這個工作?
感謝您的閱讀!
感謝您的意見,但我真的需要通過我的網絡方法來做到這一點。 我不敢相信僅僅做一個控件的克隆是如此困難! – wotney