我有两个与两个下拉列表相关的SqlDataSource。 PopulateDLL()方法允许我使用城镇填充第一个下拉列表并过滤第二个下拉列表,其中包含与该城镇相关的街道名称,但是一些街道名称也有建筑物名称(即市政厅,主街道),我是什么尝试做的是包含具有适当街道名称的建筑物名称。我研究了这个并得到了代码
dt.Columns.Add("StreetName + BuildingName"); ddl_FilteredLocation.DataTextField = "BuildingName"; //ddl_FilteredLocation.DataValueField = ""; ddl_FilteredLocation.DataBind();
然而它仍然只调用街道名称,我尝试了不同的变化,有时只会出现建筑物名称,但没有街道名称,任何帮助都会很棒,谢谢!
<%-- DS for DDL Location --%>
<asp:SqlDataSource ID="ds_Location" EnableCaching="false" DataSourceMode="DataSet" runat="server" >
ConnectionString="<%$ ConnectionStrings:DataTown %>"
SelectCommand="SELECT DISTINCT Town FROM [DataTown].[dbo].[DT] Order By Town">
<asp:SqlDataSource>
<%-- DS for DDL Street Name, filterExpression is used so that when a user selects a town from ddl_Location the streets for that town are filtered --%>
<asp:SqlDataSource ID="ds_StreetName" EnableCaching="true" DataSourceMode="DataSet" runat="server" ConnectionString="<%$ ConnectionStrings:DataTown %>"
SelectCommand="SELECT Town, BuildingName, StreetName FROM [DataTown].[dbo].[DT] Order By StreetName" FilterExpression="Town = '{0}'">
<FilterParameters>
<asp:ControlParameter Name="Town" ControlID="fmv_AccidentHSUData$ddl_Location" PropertyName="SelectedValue" />
</FilterParameters>
</asp:SqlDataSource>
<asp:DropDownList ID="ddl_Location" AutoPostBack="true" Width="100%" runat="server"></asp:DropDownList>
<asp:DropDownList ID="ddl_FilteredLocation" DataSourceID="ds_StreetName" DataTextField="StreetName" DataValueField="StreetName" AutoPostBack="true" Width="100%" runat="server">
protected void PopulateDDL()
{
DropDownList ddl_Location = (DropDownList)fmv_DataTown.FindControl("ddl_Location");
DropDownList ddl_FilteredLocation = (DropDownList)fmv_DataTown.FindControl("ddl_FilteredLocation");
ddl_Location.Items.Clear();
DataView view = (DataView)ds_Location.Select(DataSourceSelectArguments.Empty);
DataTable dt = view.ToTable();
dt.Columns.Add("StreetName + BuildingName");
ddl_FilteredLocation.DataTextField = "BuildingName";
//ddl_FilteredLocation.DataValueField = "";
ddl_FilteredLocation.DataBind();
foreach (DataRow row in dt.Rows)
{
string Town = row["Town"].ToString();
//string StreetName = row["StreetName"].ToString();
string FullText = "";
if (Town.Length > 0)
{
FullText = Town;
}
else
{
}
ddl_Location.Items.Add(new ListItem(FullText, FullText));
}
}
答案 0 :(得分:0)
下面的代码是我遇到的问题的解决方案
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
PopulateDDL();
}
}
protected void PopulateDDL()
{
DropDownList ddl_Location = (DropDownList)fmv_DataTown.FindControl("ddl_Location");
DropDownList ddl_FilteredLocation = (DropDownList)fmv_DataTown.FindControl("ddl_FilteredLocation");
ddl_Location.Items.Clear();
DataView view = (DataView)ds_Location.Select(DataSourceSelectArguments.Empty);
DataTable dt = view.ToTable();
foreach (DataRow row in dt.Rows)
{
string Town = row["Town"].ToString();
string FullText = "";
if (Town.Length > 0)
{
FullText = Town;
}
else
{
}
ddl_Location.Items.Add(new ListItem(FullText, FullText));
}
}