0
我有一个用户注册页面,我想检查输入的用户名不是已经在用户没有回发。Javascript更新ASP:标签不更新文本
ASPX页面:
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<script src="Scripts/Registration.js" type="text/javascript"></script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"></asp:ScriptManager>
<table class="contenttable">
<tr>
<td class="RegTableLabelCol">
<asp:Label ID="Label1" runat="server" Text="User Name"></asp:Label>
</td>
<td class="RegTableDataCol">
<asp:TextBox ID="txtUserName" runat="server" ></asp:TextBox>
</td>
<td class="RegTableMessageCol">
<asp:Label ID="lblUserName" runat="server" CssClass="ErrorLabel" Text="Test "></asp:Label>
</td>
</tr>
</table>
</form>
</body>
</html>
的JavaScript:
function UserChecker(username) {
lbl = document.getElementById('<%=lblUserName.ClientID%>');
if (username != '') {
PageMethods.UserNameChecker(username, OnUserSucceeded);
}
else {
lbl.innerHTML = 'Please enter a username';
}
}
function OnUserSucceeded(result, userContext, methodName) {
lbl = document.getElementById('<%=lblUserName.ClientID%>');
if (methodName == "UserNameChecker") {
if (result == true) {
lbl.innerHTML = 'User name not available';
}
else {
lbl.innerHTML = 'User name is available ';
}
}
代码背后:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
txtUserName.Attributes.Add("onblur", "UserChecker(this.value)");
}
}
[WebMethod]
public static bool UserNameChecker(string UserName)
{
string UserNameDb = null;
SQLMethods sqlm = new SQLMethods();
DataSet dataSet1 = sqlm.IsUserNameExist(UserName);
foreach (DataRow row in dataSet1.Tables["UserName"].Rows)
{
UserNameDb = string.Format("{0}", row["UserName"]);
}
if (UserNameDb != null)
{
return true;
}
else
{
return false;
}
}
我已经把警报在JavaScript和功能似乎被解雇。麻烦的是标签没有被设置为这两种情况下(没有用户名,用户名或用户名确定)。我哪里错了?
UPDATE
的JavaScript是在.js文件中,如果我把它放在aspx页面就拿起标签和作品。
甲侧前端控制的ID:不使用'ScriptManager'只是没有。 – 2014-11-02 17:38:39
@RoyiNamir另类? – Fred 2014-11-02 17:47:41
如果您在控制台中遇到任何'JS'错误,请将其发布! – 2014-11-02 17:54:31