我有几个下拉列表,我想让第一个DDL的值确定填充第二个DDL的内容。我想也许我可以使用autopostback将变量设置为所选值,然后在第二个数据源中,将它放在我的select查询中,如下所示:
标记:
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();
}
但仍然遇到数据类型不匹配错误。我想做的就像我想做的一样简单吗?我可以通过这种方式工作吗?
答案 0 :(得分:0)
是否可以在数据源项中使用where子句?
基本示例:http://msdn.microsoft.com/en-us/library/vstudio/tw738475(v=vs.100).aspx
答案 1 :(得分:0)
为什么不在AccessDataSource2.SelectCommand
例程中设置PopulateDDLsections()
- 在后面的代码中。
类似的东西:
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;
}
}