简体   繁体   English

如何将DropDownList的DataTextField设置为显式实现的属性

[英]How to set DataTextField for DropDownList to explicitly implemented property

How do i set the DropDownList's DataTextField property to an explicitly implemented property ? 如何将DropDownList的DataTextField属性设置为显式实现的属性?

example: 例:

    interface IIdValue{
      string Id ;
    }
    class Employee : IIdValue
{
    public string Id{get;set;}

    string IIdValue.Id
    {
       get ;
       set ;
    } 
}

How do i use this to set the DataTextField property in dropdownlist to point to the Id property of IIdValue.Id ? 我如何使用它来设置dropdownlist中的DataTextField属性以指向IIdValue.Id的Id属性?

Given how Interfaces work, you can't just explicitly put in the property name in DataTextField. 鉴于接口的工作方式,您不能只在DataTextField中显式地输入属性名称。 You have to use some Reflection, or another means, to get the value. 您必须使用一些反思或其他方式来获取价值。 Here is a solution I made. 这是我提出的解决方案。

HTML 的HTML

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="_testForPw.aspx.cs" Inherits="_testForPw" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:DropDownList ID="ddlTest" runat="server" />
        </div>
    </form>
</body>
</html>

CODE BEHIND 后面的代码

using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Web.UI.WebControls;

public partial class _testForPw :  System.Web.UI.Page
{

    public void Page_Load(object sender, EventArgs e)
    {
        TestClass obj1 = new TestClass() { Id = "1234", Name = "Test" };
        TestClass obj2 = new TestClass() { Id = "5678", Name = "FooBar" };
        Collection<TestClass> objs = new Collection<TestClass>() { obj1, obj2 };

        this.ddlTest.DataSource = TestClass.GetItems(objs);
        this.ddlTest.DataBind();
    }

    public partial class TestClass : ITestClass
    {
        public string Id { get; set; }
        public string Name { get; set; }

        [AttributeProvider(typeof(ITestClass))]
        string ITestClass.Test
        {
            get
            {
                return "Test12345";
            }
            set
            {
                // do nothing, for testing only
            }
        }

        public static Collection<ListItem> GetItems(Collection<TestClass> objs)
        {
            Collection<ListItem> results = new Collection<ListItem>();

            foreach (TestClass item in objs)
            {
                System.Reflection.PropertyInfo info = item.GetType().GetInterface("ITestClass").GetProperty("Test");

                if (info != null)
                {
                    results.Add(new ListItem() { Text = info.GetValue(item).ToString(), Value = item.Id });
                }

            }

            return results;
        }
    }

    public interface ITestClass
    {
       string Test { get; set; }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM