【问题标题】:Why I am not getting the dropdownitemlist2 selected value?为什么我没有得到 dropdownitemlist2 选择的值?
【发布时间】:2020-12-28 03:57:49
【问题描述】:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Label ID="Label1" runat="server" Text="Select Country : "></asp:Label>

            <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" Width="150px">
                <asp:ListItem>*Select The  Country*</asp:ListItem>
                <asp:ListItem>India</asp:ListItem>
                <asp:ListItem>Bangladesh</asp:ListItem>
                <asp:ListItem>Pakisthan</asp:ListItem>
                <asp:ListItem>Nepal</asp:ListItem>
            </asp:DropDownList>
            <br />
            <br />

            <asp:Label ID="Label2" runat="server" Text="Select State: "></asp:Label>

            <asp:DropDownList ID="DropDownList2" runat="server"  OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged" Width="150px">
                <asp:ListItem>*Select State*</asp:ListItem>
            </asp:DropDownList>
            <br />
            <br />
            <asp:Button ID="Button1" runat="server" BackColor="#0066FF" Text="Show Your Selection" Font-Bold="True" OnClick="Button1_Click" OnClientClick="displayValue()" />
        </div>
         <script type="text/javascript">
             function displayValue()
             {
                 
                 alert("Country:  " + '<%=DropDownList1.SelectedValue%>' + " State:  " + '<%=DropDownList2.SelectedItem.Value%>');
                 //alert("  Country: " + document.getElementByName("DropDownList1").value + "  State:  " + document.getElementsByIName('DropDownList2').value); 
             }
        </script>
    </form>
</body>
</html>

我的 aspx.cs 有一个代码,另一个是我的 aspx 代码,我想在按下按钮时设置要打印的选定值,但它只显示第一个下拉列表值正确,另一个值是下拉列表的第一个值我没有在下拉列表2中获得选择值请帮助我修复它。

【问题讨论】:

  • 我没有看到任何相关的后端代码,但是您是否使用IsPostBack检查将数据绑定到DDL?
  • 在asp.net中运行,我认为主要问题出在js部分。
  • 您期望的值是多少?因为只有Select State 在 DropDownList2 中。
  • 如果我在警报中选择国家印度和州阿萨姆邦,它应该显示印度和阿萨姆邦,但它显示所有值的印度和西孟加拉邦(第一项)第一个下拉菜单工作正常,但第二个不是.
  • 第二个 DropDownList2 缺少 AutoPostBack="True"。

标签: javascript html asp.net webforms


【解决方案1】:

下拉列表 2 的自动回发错误。

【讨论】:

  • 我就是这么说的!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-19
  • 2013-07-15
  • 1970-01-01
相关资源
最近更新 更多