级联下拉列表未填充
Cascading dropdown not populating
我一直在按照以下示例在 Web 应用程序中创建一些级联下拉菜单。该示例不使用 jquery,我想暂时坚持使用它。
https://www.aspsnippets.com/Articles/Populate-Cascading-DropDownList-from-Database-in-ASPNet-Example.aspx
我无法让第二个下拉菜单使用第一个下拉菜单的结果来填充列表。这是我的代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
CategorySelect.Items.Clear();
CategorySelect.Items.Add(new ListItem("--Select Activity--", ""));
CategorySelect.AppendDataBoundItems = true;
String strConnString = ConfigurationManager
.ConnectionStrings["iSAMSConnectionString"].ConnectionString;
String strQuery = "select TblActivityManagerFolderID, txtName from dbo.TblActivityManagerFolder";
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = strQuery;
cmd.Connection = con;
try
{
con.Open();
CategorySelect.DataSource = cmd.ExecuteReader();
CategorySelect.DataTextField = "txtName";
CategorySelect.DataValueField = "TblActivityManagerFolderID";
CategorySelect.DataBind();
}
finally
{
con.Close();
con.Dispose();
}
}
}
protected void ActivitySelect_SelectedIndexChanged(object sender, EventArgs e)
{
ActivitySelect.Items.Clear();
ActivitySelect.Items.Add(new ListItem("--Select Activity--", ""));
ActivitySelect.AppendDataBoundItems = true;
String strConnString = ConfigurationManager
.ConnectionStrings["iSAMSConnectionString"].ConnectionString;
String strQuery = "select txtName, TblActivityManagerGroupID from dbo.TblActivityManagerGroup " +
"where intFolder=@txtName";
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand();
cmd.Parameters.AddWithValue("@txtName",
CategorySelect.SelectedItem.Value);
cmd.CommandType = CommandType.Text;
cmd.CommandText = strQuery;
cmd.Connection = con;
try
{
con.Open();
ActivitySelect.DataSource = cmd.ExecuteReader();
ActivitySelect.DataTextField = "txtName";
ActivitySelect.DataValueField = "TblActivityManagerGroupID";
ActivitySelect.DataBind();
if (ActivitySelect.Items.Count > 1)
{
ActivitySelect.Enabled = true;
}
else
{
ActivitySelect.Enabled = false;
}
}
finally
{
con.Close();
con.Dispose();
}
}
我正在使用 2 个表。
TblActivityManagerFolder where I am using 2 fields - TblActivityManagerFolderID
and txtName
TblActivityManagerGroup where I am using 2 fields - intFolder
(which joins to TblActivityManagerFolderID) and txtName
我的第一个下拉列表完全按照预期填充,但是当我进行选择时,第二个下拉列表中什么也没有发生。
附加代码:
<asp:SqlDataSource ID="iSAMS" runat="server" ConnectionString="<%$ ConnectionStrings:iSAMSConnectionString %>"
SelectCommand="SELECT [blnActive], [TblActivityManagerFolderID], [txtName] FROM [TblActivityManagerFolder] WHERE ([intActivity] = @intActivity)">
<SelectParameters>
<asp:Parameter DefaultValue="34" Name="intActivity" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</div>
<div class="auto-style18">
<asp:DropDownList ID="CategorySelect" runat="server" DataTextField="txtName" DataValueField="TblActivityManagerFolderID"
OnSelectedIndexChanged="ActivitySelect_SelectedIndexChanged" CssClass="newStyle1">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="CategorySelect"
ErrorMessage="Please select your answer" style="text-align: left; font-weight: 700; color: #FF0000; font-size: medium;">!</asp:RequiredFieldValidator>
</div>
<br />
<div class="auto-style19">
<div class="auto-style20">
Please select the activity undertaken from the pick list:
<asp:DropDownList ID="ActivitySelect" runat="server" DataSourceID="iSAMSActivity" DataTextField="txtName" DataValueField="TblActivityManagerGroupId" CssClass="newStyle1" OnSelectedIndexChanged="ActivitySelect_SelectedIndexChanged">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="ActivitySelect"
ErrorMessage="Please select your answer" style="text-align: left; font-weight: 700; color: #FF0000; font-size: medium;">!</asp:RequiredFieldValidator>
<asp:SqlDataSource ID="iSAMSActivity" runat="server" ConnectionString="<%$ ConnectionStrings:iSAMSConnectionString %>" SelectCommand="SELECT [txtName], [intActivity], [intFolder], [TblActivityManagerGroupId] FROM [TblActivityManagerGroup] WHERE ([intFolder] = @intFolder)">
<SelectParameters>
<asp:ControlParameter ControlID="CategorySelect" Name="intFolder" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
我也在post返回结果,所以我可以使用以下方法查看是否有效:
protected void Button1_Click(object sender, EventArgs e)
{
var activity = CategorySelect.DataTextField;
var CoCActivity = ActivitySelect.DataTextField;
var OtherSkills = SkillsTextBox.Text;
var ExtraDev = DevTextBox.Text;
var OtherActivities = OtherActivitiesTextBox.Text;
OutputLabel.Text = activity + "<br/>" + CoCActivity + "<br/>" + OtherSkills + "<br/>" + ExtraDev + "<br/>" + OtherActivities;
}
当它返回 post 时,它生成一个整数而不是下拉列表中的标签。可以更改它以使其 post 支持实际文本吗?
将 DropdownList 的 AutoPostBack
属性 设置为 True。如果您不设置它,该事件只会在其他事件强制回发(单击按钮或类似事件)时在服务器端触发
我一直在按照以下示例在 Web 应用程序中创建一些级联下拉菜单。该示例不使用 jquery,我想暂时坚持使用它。 https://www.aspsnippets.com/Articles/Populate-Cascading-DropDownList-from-Database-in-ASPNet-Example.aspx
我无法让第二个下拉菜单使用第一个下拉菜单的结果来填充列表。这是我的代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
CategorySelect.Items.Clear();
CategorySelect.Items.Add(new ListItem("--Select Activity--", ""));
CategorySelect.AppendDataBoundItems = true;
String strConnString = ConfigurationManager
.ConnectionStrings["iSAMSConnectionString"].ConnectionString;
String strQuery = "select TblActivityManagerFolderID, txtName from dbo.TblActivityManagerFolder";
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = strQuery;
cmd.Connection = con;
try
{
con.Open();
CategorySelect.DataSource = cmd.ExecuteReader();
CategorySelect.DataTextField = "txtName";
CategorySelect.DataValueField = "TblActivityManagerFolderID";
CategorySelect.DataBind();
}
finally
{
con.Close();
con.Dispose();
}
}
}
protected void ActivitySelect_SelectedIndexChanged(object sender, EventArgs e)
{
ActivitySelect.Items.Clear();
ActivitySelect.Items.Add(new ListItem("--Select Activity--", ""));
ActivitySelect.AppendDataBoundItems = true;
String strConnString = ConfigurationManager
.ConnectionStrings["iSAMSConnectionString"].ConnectionString;
String strQuery = "select txtName, TblActivityManagerGroupID from dbo.TblActivityManagerGroup " +
"where intFolder=@txtName";
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand();
cmd.Parameters.AddWithValue("@txtName",
CategorySelect.SelectedItem.Value);
cmd.CommandType = CommandType.Text;
cmd.CommandText = strQuery;
cmd.Connection = con;
try
{
con.Open();
ActivitySelect.DataSource = cmd.ExecuteReader();
ActivitySelect.DataTextField = "txtName";
ActivitySelect.DataValueField = "TblActivityManagerGroupID";
ActivitySelect.DataBind();
if (ActivitySelect.Items.Count > 1)
{
ActivitySelect.Enabled = true;
}
else
{
ActivitySelect.Enabled = false;
}
}
finally
{
con.Close();
con.Dispose();
}
}
我正在使用 2 个表。
TblActivityManagerFolder where I am using 2 fields -
TblActivityManagerFolderID
andtxtName
TblActivityManagerGroup where I am using 2 fields -
intFolder
(which joins to TblActivityManagerFolderID) andtxtName
我的第一个下拉列表完全按照预期填充,但是当我进行选择时,第二个下拉列表中什么也没有发生。
附加代码:
<asp:SqlDataSource ID="iSAMS" runat="server" ConnectionString="<%$ ConnectionStrings:iSAMSConnectionString %>"
SelectCommand="SELECT [blnActive], [TblActivityManagerFolderID], [txtName] FROM [TblActivityManagerFolder] WHERE ([intActivity] = @intActivity)">
<SelectParameters>
<asp:Parameter DefaultValue="34" Name="intActivity" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</div>
<div class="auto-style18">
<asp:DropDownList ID="CategorySelect" runat="server" DataTextField="txtName" DataValueField="TblActivityManagerFolderID"
OnSelectedIndexChanged="ActivitySelect_SelectedIndexChanged" CssClass="newStyle1">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="CategorySelect"
ErrorMessage="Please select your answer" style="text-align: left; font-weight: 700; color: #FF0000; font-size: medium;">!</asp:RequiredFieldValidator>
</div>
<br />
<div class="auto-style19">
<div class="auto-style20">
Please select the activity undertaken from the pick list:
<asp:DropDownList ID="ActivitySelect" runat="server" DataSourceID="iSAMSActivity" DataTextField="txtName" DataValueField="TblActivityManagerGroupId" CssClass="newStyle1" OnSelectedIndexChanged="ActivitySelect_SelectedIndexChanged">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="ActivitySelect"
ErrorMessage="Please select your answer" style="text-align: left; font-weight: 700; color: #FF0000; font-size: medium;">!</asp:RequiredFieldValidator>
<asp:SqlDataSource ID="iSAMSActivity" runat="server" ConnectionString="<%$ ConnectionStrings:iSAMSConnectionString %>" SelectCommand="SELECT [txtName], [intActivity], [intFolder], [TblActivityManagerGroupId] FROM [TblActivityManagerGroup] WHERE ([intFolder] = @intFolder)">
<SelectParameters>
<asp:ControlParameter ControlID="CategorySelect" Name="intFolder" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
我也在post返回结果,所以我可以使用以下方法查看是否有效:
protected void Button1_Click(object sender, EventArgs e)
{
var activity = CategorySelect.DataTextField;
var CoCActivity = ActivitySelect.DataTextField;
var OtherSkills = SkillsTextBox.Text;
var ExtraDev = DevTextBox.Text;
var OtherActivities = OtherActivitiesTextBox.Text;
OutputLabel.Text = activity + "<br/>" + CoCActivity + "<br/>" + OtherSkills + "<br/>" + ExtraDev + "<br/>" + OtherActivities;
}
当它返回 post 时,它生成一个整数而不是下拉列表中的标签。可以更改它以使其 post 支持实际文本吗?
将 DropdownList 的 AutoPostBack
属性 设置为 True。如果您不设置它,该事件只会在其他事件强制回发(单击按钮或类似事件)时在服务器端触发