我刚接触使用C#和ASP.NET我试图创建一个注册表单,其中包含<asp:DropDownList>
即时通讯试图捕获OnSelectedIndexChange
是否有效但我什么也没有得到任何响应。< / p>
这是 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仍无法正常工作
并且控制台中也没有错误。
答案 0 :(得分:0)
您可以使用ClientScript.RegisterClientScriptBlock()
方法替代ClientScript.RegisterStartupScript()
:
protected void Month_Selected(object sender, EventArgs e)
{
ClientScript.RegisterClientScriptBlock(this.Page, GetType(), "alert", "alert('working');", true);
}
如果您有ScriptManager
个实例(即<asp:ScriptManager>
),则可以使用ScriptManager.RegisterStartupScript()
方法:
protected void Month_Selected(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this, typeof(string), "alert", "alert('working');", true);
}
或使用Response.Write
手动编写script
代码:
if (!string.IsNullOrEmpty(month.SelectedValue))
{
Response.Write("<script>alert('working');</script>");
}
请注意,您需要服务器端表单标记(即<form runat="server">...</form>
才能成功执行脚本。
答案 1 :(得分:0)
您可以在字符串中传递值或文本。 然后在警报中输出它,如下所示。
protected void Month_Selected(object sender, EventArgs e)
{
string wildchild = month.SelectedItem.Text + " - " + month.SelectedItem.Value;
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('" + wildchild + "');", true);
}
答案 2 :(得分:-1)
你可以试试这个
Response.Write("<script> alert('Working')</script>");