We have a web project written with technology ASP.NET Web Forms. This website has a usual registration form, where user is required to fill in a phone number.
I want to check on the run that this phone number is unique i.e. no other registered user has already used this phone number. Of course there is no need to send the whole form to the server, good example of this behavior could be how gmail checks if the email your are typing is free for registration.
I tried to adapt an example from microsoft and use two standart ajax components: ScriptManager and UpdatePanel.
My crazy implementation is:
- input-field "phonenumber" looses focus so this triggers "checkPhoneNumber"
- javascript function "checkPhoneNumber" clicks the button "ValidatePhoneNumber"
- server writes "Invalid phone number" in a label
Probably this is extremly bad and compicated practise, but somehow it works so far.
The problem is that I want to make more changes on the webpage with help of javascript (add&remove some classes to visualize the error/success) in "onchange" event of the label that never runs. Any ideas why is that?
Code in "register.aspx":
<body>
<form id="form1" runat="server">
<div style="padding-top: 10px">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<fieldset style="border: none">
<asp:Label ID="ValidationLabel" runat="server" Text="" onchange="validationLabelOnChange"></asp:Label><br />
<asp:Button ID="btnValidatePhoneNumber" runat="server" OnClick="btnValidatePhoneNumber_Click" Text="Button" />
</fieldset>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
Javascript in "register.aspx":
<script>
function checkPhoneNumber()
{
document.getElementById("<%=btnValidatePhoneNumber.ClientID%>").click();
};
function validationLabelOnChange()
{
var label = document.getElementById("<%=ValidationLabel.ClientID%>");
alert(label.text);
};
</script>
Code in "register.aspx.cs":
protected void btnValidatePhoneNumber_Click(object sender, EventArgs e)
{
ValidationLabel.Text = "Refreshed at " + DateTime.Now.ToString();
}
I appreciate if you can propose a better solution!
Aucun commentaire:
Enregistrer un commentaire