Javascript更新ASP:标签不更新文本
我有一个用户注册页面,我想检查输入的用户名是否已经在用户中,而没有回发 ASPX页面:Javascript更新ASP:标签不更新文本,javascript,c#,asp.net,Javascript,C#,Asp.net,我有一个用户注册页面,我想检查输入的用户名是否已经在用户中,而没有回发 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>
<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中添加了警报,这些函数似乎已经启动。问题是两种情况下都没有设置标签(没有用户名、使用用户名或用户名ok)。我哪里做错了
更新
JavaScript在一个.js文件中,如果我把它放在aspx页面中,它会选择标签并工作 lbl=document.getElementById(“”);
lbl = document.getElementById('<%=lblUserName.ClientID%>');
在JS文件中,将不起作用。指示aspx页面的服务器代码部分。如果你把它放在一个JS文件中,它只是一个字符串
您可以将此部分留在页面中,并将lbl声明为全局变量,该变量在JS文件引用中起作用
或者,您可以将控件的ClientIDMode切换为static,然后在JS文件中使用控件的ID侧边提示:不要使用
ScriptManager
就是不要使用。@RoyiNamir另一种选择?如果您在console中遇到任何JS
错误,请也发布它@ArindamNayaklbl=document.getElementById(“”)代码>这一行返回空值。@Fred,我想你已经得到答案了,-Stigar,已经发布了所需内容。
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;
}
}
lbl = document.getElementById('<%=lblUserName.ClientID%>');