【发布时间】: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