为什么直到OnSaveStateComplete时,我的Web表单才检测到列表框的内容?我怎样才能更早发现它们?

如何解决为什么直到OnSaveStateComplete时,我的Web表单才检测到列表框的内容?我怎样才能更早发现它们?

我有一个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(后面的代码)。这导致代码绑定发生在生命周期的后期。

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

  1. /hotels/{hotelId}/{detailLevel}中的lstSecurityTemplates1条件中删除或注释掉所有SqlSecurityList1代码。

  2. 从.aspx页的else列表框中删除现有的ddlJobTitle1_SelectedIndexChanged参数。

  3. DataSourceID="SqlSecurityList"事件中的以下代码中添加新的lstSecurityTemplates1SqlConnection

    SqlDataSource

现在,响应将按照您的期望显示正确的编号,而不是以前在模板列表框中显示的编号。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-