引用基于UserControl类的字符串值

时间:2011-01-24 15:08:21

标签: .net asp.net vb.net user-controls

我目前有以下代码:

Dim ucAppName As UserControl_appName = CType(Parent.FindControl(strucParam(i).ParentFindControl), UserControl_appName)

如何修改它以便“UserControl_appName”不是硬编码的。我想把它作为一个字符串传递。谢谢!

3 个答案:

答案 0 :(得分:0)

提示:

您需要将System.Type传递给您的方法......

您需要使用Object.GetType()以及typeof运算符。您还可以使用其限定名称创建类的类型的实例(如果它在当前正在执行的程序集中)或AssemblyQualifiedName

以下基本思路:

<强> web窗体

<h2>Passing Types</h2>
<asp:Panel runat="server" ID="pnl1">
    <p>
        <asp:TextBox runat="server" ID="txt1" />
    </p>
    <p>
        <asp:DropDownList runat="server" ID="ddl1" />
    </p>
    <p>
        <asp:HyperLink runat="server" ID="hl1" Text="Link1" NavigateUrl="http://www.google.co.uk" Target="_blank" />
    </p>
</div>
<asp:Label runat="server" ID="lout" />
<div>
    <p>
        <asp:DropDownList runat="server" ID="ddlTypes">
        </asp:DropDownList>
        &nbsp;
        <asp:Button runat="server" ID="btnGetValueFromType" Text="Get Value By Control Type" onclick="btnGetValueFromType_Click" />
    </p>
</asp:Panel>

<强>代码隐藏道歉,自从我做了任何 VB.net 以来已经有一段时间了,所以这个例子在 C#中)

    protected void Page_Load(object sender, EventArgs e)
    {
        if (! IsPostBack){
            buildGui();
        }
    }

    private void buildGui(){
        List<string> lstStrings = new List<string>(5);

        lstStrings.Add("one");
        lstStrings.Add("two");
        lstStrings.Add("three");
        lstStrings.Add("four");
        lstStrings.Add("five");

        ddl1.DataSource = lstStrings;
        ddl1.DataBind();

        //Populate the Types Dropdown with some fullyqualified type names (includes the assembly name,version number, PublicKey etc.)
        Dictionary<string, string> lstTypes = new Dictionary<string, string>(3);

        lstTypes.Add(this.ddl1.GetType().Name, this.ddl1.GetType().AssemblyQualifiedName);
        lstTypes.Add(this.txt1.GetType().Name, this.txt1.GetType().AssemblyQualifiedName);
        lstTypes.Add(this.hl1.GetType().Name, this.hl1.GetType().AssemblyQualifiedName);

        //lstTypes.Add(this.hl1.GetType().Name, this.hl1.GetType().AssemblyQualifiedName);
        lstTypes.Add(this.CRJunk01.GetType().Name, this.CRJunk01.GetType().AssemblyQualifiedName);

        ddlTypes.DataSource = lstTypes;
        ddlTypes.DataValueField = "value";
        ddlTypes.DataTextField = "key";
        ddlTypes.DataBind();
    }

    protected void btnGetValueFromType_Click(object sender, EventArgs e)
    {
        Type t = Type.GetType(ddlTypes.SelectedValue);

        lout.Text = "Looking for a type: " + t.ToString();

        foreach(Control c in this.pnl1.Controls){
            if (c.GetType().Equals(Type.GetType(ddlTypes.SelectedValue)))//Look for controls whose type is the same as the type selected in ddl1.
            {
                lout.Text += string.Format("<br/>Got Control of type: {0}", c.GetType().Name);
                //Cast to your custom Interface/BaseClass and extract the value.
                lout.Text += "<br/>Value is: " + ((ICrJunkControl)(c)).ControlValue;
            }
        }
    }

这是你可能需要“利用”的东西。

最后说明,如果所有自定义控件都从基类或接口继承,那么最好。这样,当您根据类型找到所需对象时,可以将其转换为接口或基类,然后获取定制共享成员的值,例如: ControlValue。基类的实现将覆盖它。这将有助于克服与​​不同控制相关的问题,这些控制具有暴露其状态的不同成员。

e.g。

文字框 - &gt;文字
DropdownList - &gt;的SelectedValue
超链接 - &gt; Text / NavigateUrl

HTH,

答案 1 :(得分:0)

使用界面并在您的usercontrol上实现它。然后,您可以将用户控件强制转换为界面,并具有可用的所有常用属性和方法。

Public Interface ucInterface

    Property textbox() As String
    Function calculate() As Double

End Interface

Public Class UserControl_appName
    Inherits System.Web.UI.UserControl
    Implements ucInterface

    Public Function calculate() As Double Implements ucInterface.calculate
        Return 99 / 10
    End Function

    Public Property textbox() As String Implements ucInterface.textbox
        Get
            Return txtBox.text
        End Get
        Set(ByVal value As String)
            txtBox.text = value
        End Set
    End Property

End Class

Public Class Test
    Public Sub Test()

        Dim ucAppName As ucInterface = CType(Parent.FindControl(strucParam(i).ParentFindControl), ucInterface)

        ucAppName.textbox = "Test 1"
        ucAppName.calculate()

    End Sub
End Class

答案 2 :(得分:0)

查看system.Reflection。您还可以从其他程序集中检索对象,甚至从引用的库中检索对象。当然,要使其工作,您需要记住此代码返回对“对象”的引用。你仍然会有定义一个界面的问题,这个界面会破坏所有控件的常用实现,或者另一种类型确定方法。

Imports System.Reflection
Public Class ObjectFactory
Public Shared Function NamedObject(ByVal ObjectName As String) As Object
    Dim Assem = [Assembly].GetExecutingAssembly()

    Dim MyType As Type = Assem.GetType(ObjectName.Trim)
    Dim MyObject As Object = Nothing
    Try
        MyObject = Activator.CreateInstance(MyType)
    Catch MyProblem As TargetInvocationException
        MessageBox.Show(MyProblem.ToString)
    End Try

    Return MyObject
End Function

结束班