问题描述
我有一个ASP.NET Web表单,其中有两个下拉列表和一个列表框,它们按此顺序彼此级联:
部门(ddl)->职务(ddl)->安全模板(列表框)
在加载时,将填充“部门”列表。选择一个部门,然后用与该部门匹配的工作填充“职务标题” DDL。同样,选择“职务”选项后,“安全模板”列表框将填充有可用于该职务的匹配安全模板。
UI似乎在正常工作,因为一旦我选择了职位,安全模板就会出现在列表框中。但是,我知道由于一些Debug.WriteLines
的输出,它们只出现在页面生命周期的尽头。对于具有四个安全模板的职位,当我在页面上选择该职位时,这四个模板会显示在列表框中,但是我的“输出”窗口(在VS 2019中进行调试)仅显示一个条目(填充之前的默认填充项)带有实际选项的“选择职位”。
我写了一些额外的Debug.WriteLines
并为每个页面生命周期状态创建了函数,直到OnSaveStateComplete()
,它们才以正确的数量触发,这在页面生命周期中为时已晚任何基于列表框中值的内容。
是什么原因导致这些值这么晚才“显示”到我的Web表单中?我该如何更改这些值,使它们出现在我可以对其进行处理的生命周期的早期?
换句话说,只要列表框中填充了选项(例如,一旦我选择了职位名称),我就希望能够(有条件地)基于列表框的新内容进行操作。
如果我集合autopostback="true"
在列表框,然后自然它更新并显示在输出窗口右侧计数当我选择列表框项目,但我需要它来检测正确的计数被选择的任务标题时,而不是在选择安全模板时,因为我依赖于填充安全模板来发生另一个条件事件(取决于列表框中显示的安全模板的内容)。
对于我来说,第二个DDL在我选择第一个DDL中的内容时可以按预期的方式工作似乎很奇怪,但是当我在第二个DDL中选择内容时,列表框的工作方式却不同。我将其归结为对ASP.NET页面的生命周期不够了解。
如果这很重要,我正在使用C#7.3并面向.NET Framework 4.6.1。
Form.aspx:
<asp:Label ID="lblDeptList1" AssociatedControlID="ddlDeptList1" runat="server" Text="Employee's Department: ">
</asp:Label>
<asp:DropDownList ID="ddlDeptList1" runat="server"
DataSourceID="sqlDeptList1"
DataTextField="DepartmentName"
DataValueField="Department"
OnDataBound="ddlDeptList1_DataBound"
OnSelectedindexChanged="ddlDeptList1_SelectedindexChanged"
AutopostBack="True" >
</asp:DropDownList>
<asp:sqlDataSource ID="sqlDeptList1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString1 %>"
SelectCommand="SELECT [Department],[DepartmentName] FROM [Departments] ORDER BY [DepartmentName]">
</asp:sqlDataSource>
<asp:Label ID="lblJobTitle1" AssociatedControlID="ddlJobTitle1" runat="server" Text="Job Title: ">
</asp:Label>
<asp:DropDownList ID="ddlJobTitle1" runat="server"
DataSourceID="sqlJobList1"
DataTextField="JobTitle"
DataValueField="JobCode"
OnDataBound="ddlJobTitle1_DataBound"
OnSelectedindexChanged="ddlJobTitle1_SelectedindexChanged"
AutopostBack="True">
</asp:DropDownList>
<asp:sqlDataSource ID="sqlJobList1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString1 %>">
</asp:sqlDataSource>
<asp:Label ID="lblSecurityTemplates1" AssociatedControlID="lstSecurityTemplates1" runat="server" Text="Select Security Template(s): ">
</asp:Label>
<asp:ListBox ID="lstSecurityTemplates1"
SelectionMode="Multiple" runat="server"
DataSourceID="sqlSecurityList1"
DataTextField="Template"
DataValueField="Template"
OnDataBound="lstSecurityTemplates1_DataBound">
</asp:ListBox>
<asp:sqlDataSource ID="sqlSecurityList1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString1 %>">
</asp:sqlDataSource>
Form.aspx.cs相关位:
public partial class NewForm : Page {
protected void ddlDeptList1_DataBound(object sender,EventArgs e) {
ddlDeptList1.Items.Insert(0,new ListItem("Choose Department",""));
}
protected void ddlJobTitle1_DataBound(object sender,EventArgs e) {
//using this to avoid the Job Title list from having two "Choose Job Title" entries if a user selects a department and then selects "Choose Department"
if (!(ddlJobTitle1.Items.Count == 1 && ddlJobTitle1.Items[0].Text == "Choose Job Title")) {
ddlJobTitle1.Items.Insert(0,new ListItem("Choose Job Title",""));
}
}
protected void lstSecurityTemplates1_DataBound(object sender,EventArgs e) {
if (!Page.IsPostBack) {
lstSecurityTemplates1.Items.Insert(0,new ListItem("Choose a Department first",""));
}
}
protected void ddlDeptList1_SelectedindexChanged(object sender,EventArgs e) {
sqlJobList1.SelectParameters.Clear();
if (ddlDeptList1.Selectedindex == 0) {
ddlJobTitle1.Items.Clear();
ddlJobTitle1.Items.Insert(0,""));
//we want to clear the Security Templates listBox when the department changes,not just when the job title changes.
lstSecurityTemplates1.Items.Clear();
lstSecurityTemplates1.Items.Insert(0,""));
}
else {
sqlJobList1.SelectParameters.Add("chosenDepartment",ddlDeptList1.SelectedItem.Value);
sqlJobList1.SelectCommand = "SELECT JobCode,Department,JobTitle FROM JobTitles WHERE Department = @chosenDepartment ORDER BY JobTitle ASC";
lstSecurityTemplates1.Items.Clear();
lstSecurityTemplates1.Items.Insert(0,new ListItem("Choose a Job Title first",""));
}
}
protected void ddlJobTitle1_SelectedindexChanged(object sender,EventArgs e) {
sqlSecurityList1.SelectParameters.Clear();
if (ddlJobTitle1.SelectedItem.Text == "Choose Job Title") {
lstSecurityTemplates1.Items.Clear();
lstSecurityTemplates1.Items.Insert(0,""));
}
else {
sqlSecurityList1.SelectParameters.Add("chosenJobTitle",ddlJobTitle1.SelectedItem.Text);
sqlSecurityList1.SelectParameters.Add("chosenDepartment",ddlDeptList1.SelectedValue);
sqlSecurityList1.SelectCommand = "SELECT disTINCT SecurityTemplate + ' | ' + SecurityTemplateName As Template FROM SecurityTemplateDB stdb INNER JOIN JobTitles jt ON jt.JobCode = stdb.JobCode WHERE jt.JobTitle = @chosenJobTitle AND jt.Department = @chosenDepartment GROUP BY stdb.SecurityTemplate,stdb.SecurityTemplateName ORDER BY Template";
//sqlSecurityList1.DataBind(); - I tried adding this here,but it doesn't seem to make a difference.
//these are the debug statements that should report 4 options when 4 are shown,but instead are reporting 1 option (the default option) when 4 are shown.
Debug.WriteLine("Security List Item Count:");
Debug.WriteLine(lstSecurityTemplates1.Items.Count);
}
}
}
解决方法
您在代码中得到错误结果数的原因很简单:在const prefix = require('./config.json');
module.exports = {
name: 'say',description: '...',execute(message,args) {
const repeated = message.content
.slice(prefix.length)
.trim()
.split(/ +/g);
message.channel.send(repeated);
},};
中存在项目之前,请检查项目数,因为项目的实际数据绑定发生在.aspx中,而不是.aspx.cs(后面的代码)。这导致代码绑定发生在生命周期的后期。
要解决此问题,您需要进行以下更改:
-
从
/hotels/{hotelId}/{detailLevel}
中的lstSecurityTemplates1
条件中删除或注释掉所有SqlSecurityList1代码。 -
从.aspx页的
else
列表框中删除现有的ddlJobTitle1_SelectedIndexChanged
参数。 -
在
DataSourceID="SqlSecurityList"
事件中的以下代码中添加新的lstSecurityTemplates1
和SqlConnection
:SqlDataSource
现在,响应将按照您的期望显示正确的编号,而不是以前在模板列表框中显示的编号。