用户控件简单数据绑定 - ASP.NET

时间:2011-03-16 06:45:27

标签: asp.net data-binding

我有一个地址用户控件。

    <fieldset>
    <legend><%#Title%></legend>
<div>
<div>Country</div>
    <div><uc:TextBox ID="txtCountry" runat="server" /></div>
</div>


<div>
<div>City</div>    
    <div><uc:TextBox ID="txtCity" runat="server" /></div>
</div>


<div>
<div>Street</div>     
    <div><uc:TextBox ID="txtStreet" runat="server"/></div>
</div>
</fieldset>

和CodeBehind

private BEAddress _address;
    public BEAddress Address
    {

        get
        {
            if (CoordinatesVisible)
            {
                _address = new BEAddress()
                {
                    Country = txtCountry.Text,
                    City = txtCity.Text,
                    Street = txtStreet.Text,
                    Block = txtBlock.Text,
                    Building = txtBuilding.Text,

                    Latitude = txtLatitude.Text,
                    Longitude = txtLongitude.Text
                };
            }
            else
            {
                _address = new BEAddress()
                {
                    Country = txtCountry.Text,
                    City = txtCity.Text,
                    Street = txtStreet.Text,
                    Block = txtBlock.Text,
                    Building = txtBuilding.Text

                };
            }
            return _address;
        }


        set
        {
            if (CoordinatesVisible)
            {
                _address = new BEAddress()
                {
                    Country = value.Country,
                    City = value.City,
                    Street = value.Street,
                    Block = value.Block,
                    Building = value.Building,

                    Latitude = value.Latitude,
                    Longitude = value.Longitude
                };
            }
            else
            {
                _address = new BEAddress()
                {
                    Country = value.Country,
                    City = value.City,
                    Street = value.Street,
                    Block = value.Block,
                    Building = value.Building,

                    Latitude = value.Latitude,
                    Longitude = value.Longitude
                };
            }
        }

    }

如何进行简单的数据绑定?我想要这样的东西

<uc:Address runat="server" ID="uc1" Address=<%#GetAddress %> />

1 个答案:

答案 0 :(得分:1)

除非你想使用我根本不喜欢的DetailsView女巫,否则ASP.NET中没有任何开箱即用的功能。

对于我的程序,我编写了一个自己的类,以通用的方式管理我的绑定。 背后的想法是将控件属性绑定到对象属性。