User control simple data binding - ASP.NET
I have an Address User control.
<fieldset>
<legend><%#Title%></legend>
<div>
<div>Country&开发者_如何学运维lt;/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>
And 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
};
}
}
}
How do I do simple data binding? I want to something like this
<uc:Address runat="server" ID="uc1" Address=<%#GetAddress %> />
There is nothing out of the box in ASP.NET unless you want to use the DetailsView which I don't like at all.
For my programs I wrote a own class that manages the binding for me in a generic way. The idea behind was to bind a control property to a object property.
精彩评论