I have this aspx:
<body>
<div>
<script type="text/javascript">
function NewPage() {
document.location.href = "http://www.nextservice.pt/"
}
</script>
<form id="form1" runat="server">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Btn2" runat="server" Text="OK" onclick="Button2_Click" />
CODE1: <asp:Label ID="Label1" runat="server" Text="Label" ForeColor="#CC0000" />
</form>
</div>
</body>
and I’m working with web forms, and I wont call this button on aspx.cs
public partial class SITE_TESTER : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button2_Click (object sender, EventArgs e)
{
string code = TextBox1.Text.ToString();
if (!verifyCode(code)) // comparing users from table
{
Label1.Text = "Not Exists"; //for invalid code
}
else
{
Label1.Text = "Exist"; //for sucsseful code
/*
I Wont call my JavaScript Function here!!!!
*/
}
}
}
you can call a javascript method from server side in asp.net by following ways:
you can use either
ScriptManager.RegisterStartupScriptorScriptManager.RegisterClientScriptBlockso difference between the two is explained below:
Let’s say we have a .aspx page with the following form tag : (Line
nos. are for reference)
Now let’s look at key differences for each method :
A.
Page.RegisterClientScriptBlock()will insert the block of scriptbefore Line 2.
Page.RegisterStartupScript()will insert the script after Line 4.B.
Page.RegisterClientScriptBlock()should usually be used for scriptsencapsulated in functions. (hence the word “block”)
Page.RegisterStartupScript()can be used for any script, even if it’snot in a function.
C.
Page.RegisterClientScriptBlock()should be used for functions thatdon’t need to run on Page load.
Page.RegisterStartupScript()should be used for scripts that must runon Page Load.
D.
Page.RegisterClientScriptBlock()should be used for a script that doesnot require the form elements to have been created.
Page.RegisterStartupScript()should be used for scripts that requirethe form elements to have been created and uses references to them.
Notice that all the 4 differences are essentially related to each
other (they build upon the prev. one). The difference put in one line
can sometimes be too subtle.
you can know more about these from here and here