为什么我的网络表单在 OnSaveStateComplete 之前检测不到我的列表框的内容?我怎样才能更早地发现它们?

Why does my webform not detect the contents of my listbox until OnSaveStateComplete? How can I detect them earlier?

我有一个 ASP.NET 网络表单,其中包含两个下拉列表和一个按以下顺序相互级联的列表框:

部门 (ddl) -> 职务 (ddl) -> 安全模板(列表框)

加载时,部门列表已填充。 Select 一个部门,职位 DDL 中填充了与该部门匹配的职位。同样,一旦您 select 职位选项,安全模板列表框就会填充可用于该职位的匹配安全模板。

UI 似乎在这方面工作正常,只要我选择一个职位,安全模板就会出现在列表框中。但是,我知道由于一些 Debug.WriteLines; 的输出,它们只出现在页面生命周期的最后阶段。对于具有四个安全模板的职位,当我 select 在我的页面上说职位时,这四个模板出现在列表框中,但我的输出 window (在 VS 2019 中调试)仅显示一个条目(在填充真实选项之前的默认填充条目,“Select a Job Title”)。

我为每个页面生命周期状态编写了一些额外的 Debug.WriteLines 并创建了函数,它们直到 OnSaveStateComplete() 才以正确的数量触发,这在页面生命周期中为时已晚根据列表框中的值执行任何操作。

是什么原因导致这些值这么晚才“显示”到我的网络表单中,我该如何更改它以便它们出现在生命周期的早期,以便我可以对它们进行处理?

换句话说,一旦列表框填充了选项(例如,一旦我选择了职位),我希望能够(有条件地)根据列表框的新内容做一些事情。

如果我在列表框上设置 autopostback="true",那么当我 select 列表框项目时,它自然会更新并在输出 window 中显示正确的计数,但我需要它在工作职位 selected 时检测正确的计数,而不是在安全模板 selected 时检测正确的计数,因为我依赖于填充安全模板以发生另一个条件事件(取决于出现在列表框中的安全模板的内容)。

第二个 DDL 在第一个 DDL 中 selecting 某些东西时的工作方式对我来说似乎很奇怪,但是当我 select 时列表框的工作方式不同第二个 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,new ListItem("Choose Job Title",""));
            //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,new ListItem("Choose a Department first",""));
        }
        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,new ListItem("Choose a Job Title first",""));
        }
        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);

        }
    }
}

你在后面的代码中得到错误数量的结果的原因很简单:你在 lstSecurityTemplates1 中有项目之前检查项目的数量,因为项目的实际数据绑定发生在.aspx,而不是 .aspx.cs(后面的代码)。这会导致代码绑定在生命周期的后期发生。

要解决此问题,您需要进行以下更改:

  1. ddlJobTitle1_SelectedIndexChanged 中的 else 条件中删除或注释掉所有 SqlSecurityList1 代码。

  2. 从 .aspx 页面的 lstSecurityTemplates1 列表框中删除现有的 DataSourceID="SqlSecurityList" 参数。

  3. 在代码后面的ddlJobTitle1_SelectedIndexChanged事件中添加一个新的SqlConnectionSqlDataSource

    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString1"].ConnectionString);
    SqlDataSource source = new SqlDataSource();
    source.ConnectionString = conn.ConnectionString;
    source.SelectParameters.Add("chosenJobTitle",ddlJobTitle1.SelectedItem.Text);
    source.SelectParameters.Add("chosenDepartment",ddlDeptList1.SelectedValue);
    source.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";
    
    lstSecurityTemplates1.DataSource = source;
    lstSecurityTemplates1.DataBind();
    
    Response.Write("Items in lstSecurityTemplates1 = " + lstSecurityTemplates1.Items.Count);
    

响应现在将显示您刚刚选择的职位的正确编号,而不是之前在模板列表框中显示的数量。