我有几个下拉列表,我想让第一个DDL的值决定填充第二个DDL的内容。我想也许我可以使用autopostback将变量设置为所选值,然后在第二个数据源中,将其放在我的选择查询中,像这样:
标记:
Select a Category:<br />
<asp:DropDownList ID="ddlCategory" runat="server" AutoPostBack="True"
DataSourceID="AccessDataSource1" DataTextField="ORG_NAME" DataValueField="ID"
OnSelectedIndexChanged="PopulateDDLsections">
</asp:DropDownList>
<br />
Select an Organization:<br />
<asp:DropDownList ID="ddlOrg" runat="server"
DataSourceID="AccessDataSource2" DataTextField="SectionName"
DataValueField="ID" >
</asp:DropDownList>
<asp:AccessDataSource ID="AccessDataSource2" runat="server"
DataFile="~/App_Data/webvideos.mdb"
SelectCommand="SELECT * FROM [ORG_SECTIONS] WHERE OrgID = ' + <%= orgID %>'"></asp:AccessDataSource>
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/webvideos.mdb"
SelectCommand="SELECT * FROM [ORGANIZATIONS]"></asp:AccessDataSource>
背后的代码:
public partial class AddRecord : System.Web.UI.Page
{
int orgID = 0;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void PopulateDDLsections(object sender, EventArgs e)
{
orgID = Convert.ToInt32(ddlOrg.SelectedValue.ToString());
}
}
起初,我得到了一个数据类型不匹配,所以我把它改成了:
protected void PopulateDDLsections(object sender, EventArgs e)
{
string orgID = ddlOrg.SelectedValue.ToString();
}
,但仍然得到一个数据类型不匹配错误。我想做的事情和我想做的一样简单吗?我能走这条路去上班吗?
是否可以在数据源项中使用where子句?
基本示例:http://msdn.microsoft.com/en-us/library/vstudio/tw738475(v=vs.100).aspx
为什么不在PopulateDDLsections()
例程中设置AccessDataSource2.SelectCommand
-在后面的代码中。
类似:
protected void PopulateDDLsections(object sender, EventArgs e)
{
int orgID;
// Make sure we parse the selected value to an int.
if(Int32.TryParse(ddlOrg.SelectedValue, out orgID))
{
// Form the select statement from the orgID we just parsed.
String command = String.Format("SELECT * FROM [ORG_SECTIONS] WHERE OrgID = {0}", orgID);
// Assign the SelectCommand.
AccessDataSource2.SelectCommand = command;
}
}