【问题标题】:ASP.NET dropdownlist selectedIndexChanged not getting triggeredASP.NET 下拉列表 selectedIndexChanged 没有被触发
【发布时间】:2021-04-12 23:01:02
【问题描述】:

我正在尝试创建一个表单作为网站的一部分。在表单中,用户会看到一个带有多个选项的下拉列表。如果用户选择“其他”选项,那么他们应该看到一个文本框来填写“其他”选项的描述。

我的想法是隐藏包含文本框的 div,并在用户将下拉列表选项更改为“其他”时启用它。

我遇到了一个问题,在 asp.net 中,下拉列表“selectedindexchanged”事件没有被触发。下面是HTML代码和cs代码。

<%@ Page Title="" Language="C#" MasterPageFile="~/master/Site1.Master" AutoEventWireup="true" CodeBehind="form.aspx.cs" Inherits="Project.forms.form" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="menu" runat="server">
    </asp:Content>
    <asp:Content ID="Content3" ContentPlaceHolderID="Banner" runat="server">
        <img src="../../image.jpg" class="img-responsive" alt="Responsive image" />
    </asp:Content>
    <asp:Content ID="Content4" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    </asp:Content>
    <asp:Content ID="Content5" ContentPlaceHolderID="ContentPlaceHolder2" runat="server">
         
      <h2 class="branding_orange">Form</h2>
        <div class="alert alert-danger" name="warningDiv" style="margin-top:10px" id="warningDiv" role="alert" runat="server">
            <p name="warningMsg" id="warningMsg" runat="server"></p>
        </div>
      <form id="compliantForm" role="form" class="form" runat="server" data-toggle="validator" onsubmit="return validation();">
          <div class="row">
               .
               .
               .
              <div class="col-xs-12 col-sm-6 col-md-4">
                  <div class="form-group">
                       <label for="person-submitting-select">Is the person submitting this complaint an: <b style="color:red">*</b></label>&nbsp;
                     <asp:DropDownList ID="cboPersonSubmitting" runat="server" AutoPostBack = "True" OnSelectedIndexChanged = "OnSelectedIndexChanged">
                        <asp:ListItem Text="select" Value="0" />
                        <asp:ListItem Text="Employee" Value="1" />
                        <asp:ListItem Text="Customer" Value="2" />
                        <asp:ListItem Text="Other" Value="3" />
                    </asp:DropDownList>
    
                  </div>
    
              </div>
    .
    .
    .
    .
        
    </asp:Content>
    
    <asp:Content ID="Content6" ContentPlaceHolderID="ContentPlaceHolder3" runat="server">
    </asp:Content>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace Project.forms
{
    public partial class form : System.Web.UI.Page
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            cboPersonSubmitting.AutoPostBack = true;
            warningDiv.Visible = false;
            warningMsg.InnerHtml = "";
            
            
        }
        .
        .
        .

        protected void OnSelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboPersonSubmitting.SelectedIndex == 3)
            {
                whoSubmittingDiv.Visible = true;
            }
            else
            {
                whoSubmittingDiv.Visible = false;
            }
        }
    }
}

【问题讨论】:

  • 也许检查验证javascript函数是否被触发,它是否会阻止你的回发
  • 大多数时候事件不会触发,因为您忘记了自动回发属性:stackoverflow.com/questions/19438389/…。但这里不是这种情况。事实上它应该工作。你可以有一个断点,看看它是否没有触发?或者在 if 之前在标签中写一些东西。并将该下拉列表移出表单,以查看它是否不相关。
  • @DragandDrop 我在“OnSelectedIndexChanged”函数中添加了一个断点,并注意到在下拉列表中更改项目时它没有到达那里。关于将下拉列表置于表单之外,我需要将其置于表单内部,因为下拉列表的值将被置于表单中并经过验证。
  • 调试通常需要隔离。单独测试时更容易知道零件是否损坏。这是minimal reproducible example 指导方针的一部分,我每天都在 Stackoverflow 之外推荐它。如果是表单错误,我们将需要有关如何使用验证 js 代码重现该问题的信息。删除所有直到它工作。把东西一一加回来。直到它破裂。
  • 我认为你必须检查javascript代码,如果它返回false,表单提交将被停止。

标签: c# asp.net .net visual-studio


【解决方案1】:

根据我的测试,当您在下拉列表中选择“其他”时,您可以尝试以下代码来显示文本框。

HTML:

 <form id="form1" runat="server">
        <div class="col-xs-12 col-sm-6 col-md-4">
                  <div class="form-group">
                       <label for="person-submitting-select">Is the person submitting this complaint an: <b style="color:red">*</b></label>&nbsp;
                     <asp:DropDownList ID="cboPersonSubmitting" runat="server" AutoPostBack = "True" OnSelectedIndexChanged="cboPersonSubmitting_SelectedIndexChanged">
                        <asp:ListItem Text="select" Value="0" />
                        <asp:ListItem Text="Employee" Value="1" />
                        <asp:ListItem Text="Customer" Value="2" />
                        <asp:ListItem Text="Other" Value="3" />
                    </asp:DropDownList>
                      <div class="Submitdiv" id="thediv" runat="server" visible="false"> 
                          <asp:TextBox ID="txtInput" runat="server" ></asp:TextBox>
                      </div>
    
                  </div>
    
              </div>
    </form>

C#代码:

 protected void cboPersonSubmitting_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboPersonSubmitting.SelectedIndex == 3)
            {
                thediv.Visible = true;
            }
            else
            {
                thediv.Visible = false;
            }
        }

结果:

【讨论】:

    猜你喜欢
    • 2016-03-14
    • 1970-01-01
    • 2011-12-16
    • 2011-08-09
    • 1970-01-01
    • 2017-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多