The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

How do I use javascript in asp.net?

NaeblissNaebliss Registered User regular
edited September 2009 in Help / Advice Forum
I'm making a simple web page in visual studio 2008 and I need it to authenticate the data clientside before it is sent to the server for further authentication. I have the serverside part working but I can't for the life of me get it to run the javascript. Is there a simple way to do this?

Code:
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<script runat="server">

protected void Button1_Click(object sender, EventArgs e)
{
String accountName = "Jon";
String accountPassword = "school";
if ((TextBox1.Text == accountName) && (TextBox2.Text == accountPassword)) Label3.Text = "Hello " + TextBox1.Text;
else Label3.Text = "Login error";
}

</script>



<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
<title>Untitled Page</title>

<script language="javascript" type="text/javascript">
function validatePassword()
{
document.write("<h1>THE JAVASCRIPT RAN!!</h1>");
if (TextBox2.value.length < 8)
{
alert("password must be at least 8 characters");
return false;
}
else return true;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>

Login Please<br />
<br />

</div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Label ID="Label1" runat="server" Text="username"></asp:Label>
<p>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Label ID="Label2" runat="server" Text="password"></asp:Label>
</p>
<p>
<asp:Button ID="Button1" runat="server" Text="Login"
onclientclick="return validatePassword" onclick="Button1_Click"/>
</p>
<p>
<asp:Label ID="Label3" runat="server"></asp:Label>
</p>
</form>
</body>
</html>

This couch is the comfiest!
Street Fighter 4 (pc): sdurien
Steam: Jon http://steamcommunity.com//profiles/76561197970923897/home
Naebliss on

Posts

  • JasconiusJasconius sword criminal mad onlineRegistered User regular
    edited September 2009
    It's been a long time since I've done .NET validation but I am pretty sure that you can use the ASP.NET validator controls, and they have a property called

    EnableClientScript="True"

    which will basically transform the validation from server side to auto-generated javascript... that should do quite a bit for you.

    If not, then you have to intercept the click event on the form button and run a big validation function and return it false if anything doesn't pass.

    Jasconius on
    this is a discord of mostly PA people interested in fighting games: https://discord.gg/DZWa97d5rz

    we also talk about other random shit and clown upon each other
  • NaeblissNaebliss Registered User regular
    edited September 2009
    Thank you so much. I added asp:CustomValidator to my form and now it's running. :D

    Naebliss on
    This couch is the comfiest!
    Street Fighter 4 (pc): sdurien
    Steam: Jon http://steamcommunity.com//profiles/76561197970923897/home
Sign In or Register to comment.