I have this aspx:
<body>
<div>
<script type="text/javascript">
function NewPage() {
document.location.href = "/"
}
</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)) // paring users from table
{
Label1.Text = "Not Exists"; //for invalid code
}
else
{
Label1.Text = "Exist"; //for sucsseful code
/*
I Wont call my JavaScript Function here!!!!
*/
}
}
}
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)) // paring users from table
{
Label1.Text = "Not Exists"; //for invalid code
}
else
{
Label1.Text = "Exist"; //for sucsseful code
/*
I Wont call my JavaScript Function here!!!!
*/
}
}
}
Share
Improve this question
edited Jan 29, 2013 at 8:54
Viktor S.
12.8k1 gold badge29 silver badges55 bronze badges
asked Jan 29, 2013 at 8:53
r-magalhaesr-magalhaes
4582 gold badges9 silver badges18 bronze badges
4
- What that javascript function should do? Actually, you can't run JS on server side. But you can prepare it for being executed on client side when server will return whole HTML. Are you saying that you want to change document.location only? – Viktor S. Commented Jan 29, 2013 at 8:55
- I can run JS on server, but i have more JS to call before that. My first JS take token of facebook, and I need saved that token on DB. Thanks – r-magalhaes Commented Jan 29, 2013 at 9:47
- Not sure what do you mean under "run JS on server" (and why you are asking how to do that, if you already know the answer), but as far as I understand, you are taking a token with JS. Once you have it, you can run AJAX call to server which will put it into database. – Viktor S. Commented Jan 29, 2013 at 9:57
- with your view(FAngel) i think changing my scheme, i will try make all JS code on aspx.cs(on server location) and get the tokens by ajax or something else. thanks – r-magalhaes Commented Jan 29, 2013 at 11:30
2 Answers
Reset to default 10You can call a JavaScript method from server side in asp in the following ways:
protected void button_Click(object sender , EventArgs e)
{
string jsMethodName = "NewPage()";
ScriptManager.RegisterClientScriptBlock(this, typeof(string), "uniqueKey", jsMethodName, true);
//or
//ScriptManager.RegisterStartupScript(this, GetType(), "NewPage()", false);
}
You can use either ScriptManager.RegisterStartupScript
or ScriptManager.RegisterClientScriptBlock
The 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)
1. <form id="Form1" runat="server">
2. ..
3. ..
4. ..
5. </form>
Now let's look at key differences for each method :
A.
Page.RegisterClientScriptBlock()
will insert the block of script
before Line 2.
Page.RegisterStartupScript()
will insert the script after Line 4.
B.
Page.RegisterClientScriptBlock()
should usually be used for scripts
encapsulated in functions. (hence the word "block")
Page.RegisterStartupScript()
can be used for any script, even if it's
not in a function.
C.
Page.RegisterClientScriptBlock()
should be used for functions that
don't need to run on Page load.
Page.RegisterStartupScript()
should be used for scripts that must run
on Page Load.
D.
Page.RegisterClientScriptBlock()
should be used for a script that does
not require the form elements to have been created.
Page.RegisterStartupScript()
should be used for scripts that require
the 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 learn more about these from here and here
You can add a script which will be executed when page is loaded to browser:
Page.RegisterStartupScript("unique_key", "<script type=\"text/javascript\">NewPage()</script>"); // but this is deprecated function
or like this:
ClientScript.RegisterClientScriptBlock(this.GetType(), "unique_key", "NewPage()", true);
But if you simply want to do a redirect (as I can see from your NewPage function), you can do:
Response.Redirect("http://www.example.");