简体   繁体   English

ASP.NET 下拉列表 selectedIndexChanged 没有被触发

[英]ASP.NET dropdownlist selectedIndexChanged not getting triggered

I am trying to create a form as part of a website.我正在尝试创建一个表单作为网站的一部分。 In the form the user is shown a dropdownlist with serveral options.在表单中,用户会看到一个带有多个选项的下拉列表。 If the user chooses the "Other" option, then they should be presented with a text box to fill the description of the "other" choice.如果用户选择“其他”选项,那么他们应该看到一个文本框来填写“其他”选项的描述。

My idea was to hide the div that contains the text box and enable it when the user changes the dropdown list choice to "other".我的想法是隐藏包含文本框的 div 并在用户将下拉列表选项更改为“其他”时启用它。

I am having an issue where in asp.net the dropdownlist "selectedindexchanged" event is not being triggered.我遇到了一个问题,在 asp.net 中,下拉列表“selectedindexchanged”事件没有被触发。 Below is the HTML code and the cs code.下面是 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;
            }
        }
    }
}

Based on my test, you can try the following code to show textbox when you choose Other in the dropdownlist.根据我的测试,当您在下拉列表中选择“其他”时,您可以尝试以下代码来显示文本框。

Html: 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# code: C# 代码:

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

Result:结果:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET MVC 5 DropDownList selectedindexchanged - ASP.NET MVC 5 DropDownList selectedindexchanged 嵌套的ASP.NET DropDownList SelectedIndexChanged未触发 - Nested ASP.NET DropDownList SelectedIndexChanged Not Firing ASP.NET DropDownList未在SelectedIndexChanged上绑定SQL - ASP.NET DropDownList not binding SQL on SelectedIndexChanged ASP.NET DropDownList SelectedIndexChanged与UpdatePanel AsyncPostBackTrigger - ASP.NET DropDownList SelectedIndexChanged with UpdatePanel AsyncPostBackTrigger ASP.NET MVC 4 DropDownList selectedindexchanged - ASP.NET MVC 4 DropDownList selectedindexchanged asp.net dropdownlist的第一个选择不会触发SelectedIndexChanged事件 - asp.net dropdownlist first selection does not fire SelectedIndexChanged event ASP.NET MVC C#-DropDownList SelectedIndexChanged不触发 - Asp.net MVC C# - DropDownList SelectedIndexChanged not firing ASP.Net下拉列表没有返回selectedindexchanged事件的索引 - ASP.Net dropdownlist not returning index on selectedindexchanged event ASP.NET / C#:DropDownList在服务器控件中没有触发的SelectedIndexChanged - ASP.NET / C#: DropDownList SelectedIndexChanged in server control not firing ASP.NET中DropDownList的SelectedIndexChanged以错误的顺序触发 - SelectedIndexChanged for DropDownList in ASP.NET firing in the wrong order
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM