【发布时间】:2017-12-14 04:30:10
【问题描述】:
我是使用 C# 和 ASP.NET 的新手,我试图创建一个注册表单,我在其中包含一个 <asp:DropDownList>,我试图捕捉 OnSelectedIndexChange 是否正常工作,但我什么也没得到。
这是我的 signup.aspx
<form id="signupform" runat="server">
<asp:TextBox runat="server" ID ="firstname" class="form-control" type="text" placeholder="First Name" style="width: 168px;" pattern = "[a-zA-Z][a-zA-Z ]{2,20}" required="true"/>
<asp:TextBox runat="server" class="form-control" type="text" value="" placeholder="Middle Name" style="width: 168px;position: relative;margin-top: -50px;margin-left: 192px;" pattern = "[a-zA-Z][a-zA-Z ]{2,20}" required="true"/>
<asp:TextBox runat="server" class="form-control" type="text" value="" placeholder="Last Name" pattern = "[a-zA-Z][a-zA-Z ]{2,20}" required="true"/>
<asp:DropDownList runat="server" ID="month" AutoPostBack="true" OnSelectedIndexChanged ="Month_Selected" class="form-control" style="width: 115px;" required>
<asp:ListItem Value ="" Text="Month" Selected ="true" Disabled ="true"></asp:ListItem>
<asp:ListItem Value ="January" Text ="January"></asp:ListItem>
<asp:ListItem Value ="February" Text ="February"></asp:ListItem>
<asp:ListItem Value ="March" Text ="March"></asp:ListItem>
<asp:ListItem Value ="April" Text ="April"></asp:ListItem>
<asp:ListItem Value ="May" Text ="May"></asp:ListItem>
<asp:ListItem Value ="June" Text ="June"></asp:ListItem>
<asp:ListItem Value ="July" Text ="July"></asp:ListItem>
<asp:ListItem Value ="August" Text ="August"></asp:ListItem>
<asp:ListItem Value ="September" Text ="September"></asp:ListItem>
<asp:ListItem Value ="October" Text ="October"></asp:ListItem>
<asp:ListItem Value ="November" Text ="November"></asp:ListItem>
<asp:ListItem Value ="December" Text ="December"></asp:ListItem>
</asp:DropDownList>
//other personal input infos
</form>
这里是 signup.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class signup : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Month_Selected(object sender, EventArgs e)
{
Response.Write("working");
}
}
请帮助我不知道出了什么问题,我试图查看其他问题,但没有一个有效。
更新 1。
通过提交表单,我可以从 DropDownList 获取值,但 OnSelectedIndexChange 仍然无法正常工作
控制台也没有错误。
【问题讨论】:
-
尝试使用
ClientScript.RegisterClientScriptBlock或ScriptManager.RegisterStartupScript(如果您有ScriptManager 控制)。 -
有没有其他方法可以输出一个javascript警告框?我也试过
Response.write("working")但我什么也没得到 -
Response.Write不显示警告框,它直接写入页面。您需要在 page/ScriptManager 中注册脚本才能使其工作。 -
我刚刚尝试
Response来检查OnSelectedIndexChange -
您是否在选择的索引更改事件中检查了
month.SelectedValue?