如何解决用户控制数据绑定-ASP.NET
| 我开发了“地址”用户控件。它包含2个下拉菜单(国家和城市)和2个文本框(街道和建筑物)。第二个dropDown(城市)取决于选择的第一个dropdown国家。<div>Country</div>
<div> <asp:DropDownList runat=\"server\" ID=\"ddlCountry\" DataTextField=\"Name\"
DataValueField=\"Id\" DataSource=\"<%#Facade.Addresses.GetCountries() %>\"
AutoPostBack=\"true\" onselectedindexchanged=\"ddlCountry_SelectedIndexChanged\" /></div>
</div>
<div>
<div>City</div>
<div>
<asp:DropDownList runat=\"server\" ID=\"ddlCity\" DataTextField=\"Name\"
DataValueField=\"Name\" />
</div>
</div>
<div>
<div>Street</div>
<div><uc:TextBox ID=\"txtStreet\" Text=\"<%#Address.Street %>\" runat=\"server\"/></div>
</div>
<div>
<div>House</div>
<div> <uc:TextBox ID=\"txtHouse\" Text=\"<%#Address.House%>\" runat=\"server\"/></div>
</div>
public partial class AddressControl : UserControl
{
protected void Page_Init(object sender,EventArgs e)
{
ddlCountry.DataBind();
BindCity(int.Parse(ddlCountry.SelectedValue));
}
public BEAddress Address
{
get
{
return new BEAddress
{
Country = ddlCountry.SelectedItem.Text,City = ddlCity.SelectedItem.Text,Street = txtStreet.Text,House= txtHouse.Text
};
}
set
{
BindCity(int.Parse(ddlCountry.Items.FindByText(value.Country).Value));
var a = ddlCity.Items.FindByText(value.Country);
if (a !=null)
a.Selected = true;
txtStreet.Text = value.Street;
txtHouse.Text = value.House;
}
}
protected void ddlCountry_SelectedIndexChanged(object sender,EventArgs e) {
BindCity(int.Parse(ddlCountry.SelectedValue));
}
private void BindCity(int countryId) {
ddlCity.DataSource = Facade.Addresses.GetCities(countryId);
ddlCity.DataBind();
}
}
使用
ucAddress.Address = StoreDataSource.Address;
使用setter(ucAddress.Address = StoreDataSource.Address
)时,dropDown \“ Cities \”没有正确包含值。
如何更改代码以强制控件正常工作?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com(将#修改为@)