如何使用asp.net克隆/复制控件(带子控件)? [英] How to clone / copy a control (with child controls) using asp.net?

查看:175
本文介绍了如何使用asp.net克隆/复制控件(带子控件)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我已经尝试了几种不同的解决方案,这些解决方案在网上和其他地方都没有运气到目前为止。也许你们其中一个知识渊博的人可能会帮忙...

I've tried a few different solutions found on here and elsewhere on the web without luck so far. Maybe one of you knowledgeable lot might be able to help...


我有一堆由用户动态创建的控件我在会话状态中存储为控件集合,因此我可以在每次回发时显示它们。

I have a bunch of dynamically created controls by the user which I'm storing as a control collection in the session state so I can display them on every postback.


用户的每个控件generate是一个带有其他控件的div。

Each control that the user generates is a div with other controls inside it.


我在每个控件上都有一个按钮,允许用户删除合作控制或复制它。

I have a button on each control that will allow the user to either delete the control or duplicate it.


当用户点击"Duplicate"时,我正在调用处理事件的web方法。

When the user hits "Duplicate" I am calling my web method which handles the event.


当我的web方法找到要复制的控件时,我想制作该控件的副本并将其添加到页面中(另一个函数处理将其保存到控件集合中) page_Unload)
$

When my web method finds the control to be duplicated, I want to make a copy of that control and add it to the page (another function deals with saving it to the control collection (on 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."





任何人都可以帮我指点正确的方向让这个工作吗?


Can anyone please help point me in the right direction to get this working ?


感谢阅读!

Thanks for reading !


推荐答案

 你不能使用反射来获取/设置对象属性。应该总是强类型。

 You can't get/set the objects property using reflections. Should be always strongly typed.


这篇关于如何使用asp.net克隆/复制控件(带子控件)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆