I am using the below code to call a javascript function from aspx page load. But it's not working.
protected void Page_Load(object sender, EventArgs e)
{
btn_add_more_pack.Attributes.Add("OnClick", "openform()");
}
The javascript :
function openform()
{
try {
alert('enter');
}
catch (ex) {
}
}
I think the problem could be OnClick it should be onclick though it doesn't matter (not case sensitive) but you can try like below.
So on page load it should be as follows
protected void Page_Load(object sender, EventArgs e)
{
btn_add_more_pack.Attributes.Add("onclick", "openform()");
}
I have copied your code and tried in my system. Everything is working fine. But, one catch here:
I am using VS-2010 to work on your item.
Where have you added your javascript function? Is it inside .aspx page head section or a separate file?
If you have added the above Javascript function inside .aspx page head section, then you don't need to alter anything. Everything works fine.
But, if you have added the above Javascript function inside a separate .js file, you should remember to add reference in the .aspx head section like below
<script src="pathToJavascriptFile.js" type="text/javascript"></script>
Related
How to assign the javascript return value to c# code behind variable, while the page load. I am trying to do this. Please see the below example. I want the exact result like this.
Note : I already posted a question like this,but that is meaning less.
Javascript:
<script>
function GetValue() {
return 10;
}
</script>
C#.NET
protected void Page_Load(object sender, EventArgs e)
{
int Data
ClientScript.RegisterStartupScript(this.GetType(), "GetData", "a=GetValue();alert(a)", true);
Data=a;//a is Javascript Variable
}
What you are asking is impossible. The page load is part of the aspnet webforms request/response lifecycle. This determines what page content is generated.
The JavaScript will run on the client browser, after the page content has been generated. So there is no way to use any server side methods to get your value on page generation.
You can either pass the value as a querystring, post back a value or use a web service.
Take a hidden field or Label in the form. Assign return value to the Hidden form with document.getElementById('ID').value = 10;
Call this function from CodeBehind and get the value of Hiddnefield in the code behind.
I hope this helps.
To be more clear do something like this...
In your HTML define a tag like this
<asp:HiddenField ID="hfData" runat="server" />
And then write a javascript function that looks like this
function putData()
{
document.getElementById('hfData').value = 10;
}
and then on your page_load do something like this
protected void Page_Load(object sender, EventArgs e)
{
int Data = hfData.Value;
}
this should get youur client side data on server side for further processing
You can store the JavaScript value in hidden variable then access this value from the code behind.
I am making simple bot which will take values from script then it will put them into the textboxes of another website and then perform click on button so the bot can log in another website I have written the code, but I want to call it when page is fully loaded. I tried <body onload> but it doesn't work.
Is this possible that we could call our javascript function from our c# code file under the documentcompleted event or any other way?
<script type="text/javascript">
function myf() {
document.getElementsByTagName("INPUT")[1].setAttribute("value", "userabc");
document.getElementsByTagName("INPUT")[2].setAttribute("value", "passwordabc");
document.getElementsByTagName("INPUT")[3].click();
}
</script>
//C# Code file
protected void Page_Load(object sender, EventArgs e)
{
Response.Redirect("https://www.iauc.co.jp/auction/prelogin01_en.jsp?timestamp=1360652615774");
}
protected void Page_LoadComplete(object sender, EventArgs e)
{
//Can we call javascript function here
}
I did not fully understand you but if you want to run a script when the document has been fully downloaded on the client web browser, then you can user jQuery's document ready event.
Add jQuery to your web page and then add the following script block to the page after the jQuery reference.
<script type="text/javascript">
$(function () {
// write your JavaScript code here
myf();
});
</script>
This script block will get triggered when the DOM for the page has been fully loaded.
You have to write the complete java script including script tag in a string variable.
Then you can Response.Write() the string variable.
string script = "<script type="" language=""> YOUR JAVASCRIPT </script>";
Response.Redirect(script);
Try this
// here's will be your script
string script = "";
ClientScriptManager clientScript = Page.ClientScript;
clientScript.RegisterStartupScript(typeof(Page), "a key", script);
UPDATE :
this will check it if its fully loaded and then do what you want
$(document).ready(function()
{
//page is fully loaded and ready, do what you want
}
I need to understand that whether page is being called inside iframe or not at code behind. Is this possible?
I need to determine it in master page code behind.
asp.net 4.0, C#
In general, no.
Of course you can emit client script that detects iframe and reloads the page with e.g. a querystring.
It's not possible. However there's a workaround this. You can use querystring and check on page load if that querystring contains value, for example:
<iframe src="Default.aspx?iframe=true" />
In your Default.aspx.cs file:
protected void Page_Load(object sender, EventArgs e)
{
if(!string.IsNullOrEmpty(Request.QueryString["iframe"]))
{
if(Convert.ToBoolean(Request.QueryString["iframe"])
{
// this page is loaded in an iframe
}
}
}
The Master page got a ScriptManager.
Then i got a Control with a ScriptManagerProxy and an UpdatePanel.
Inside the UpdatePanel there I dynamically add a Control (also containing a ScriptManagerProxy) and from that control I need to run some JavaScript code.
DynamicControl.ascx:
<script type="text/javascript">
function doSomething() {
alert(1);
}
</script>
DynamicControl.ascx.cs:
public void Page_Load(object sender, EventArgs e)
{
...
ScriptManager.RegisterStartupScript(
this.Page, this.GetType(), "scriptID",
"<script type='text/javascript'>doSomething();</script>", false);
My problem is that the function "doSomething()" is never called and i dont know why. :S
Edit: It is called but not directly when i add the control.
If I do code like this, there will be an alertwindow:
"<script type='text/javascript'>alert(1);</script>"
Ok I think i need to add some more information:
The control is dynamical added inside a jQuery Dialog. And I found out that the javacode is first executed after i close and then open the dialog. Some kind of event is triggered so that the code is executed there i think. Is it possible to force this event? so the scripts are executet directly when the control is added ?
placeHolder.Controls.Add(dynamicReportControl);
This c# code doesn't execute the javascript tag immediately ?
Please try something like this
ScriptManager.RegisterStartupScript(
this.UpdatePanel1, this.UpdatePanel1.GetType(), "scriptID",
"<script type='text/javascript'>doSomething();</script>", false);
Where UpdatePanel1 is your UpdatePanel
Try setting the addScriptTags argument to true, and remove the script declaration from your code.
Is that code only supposed to run on every request? If so, why not just add this code to the ASCX:
window.onload = doSomething();
In my project i want to deny going back to the page when i click back button in toolbar.
How its possible in asp.net and c#.
Thank you.
Just put this javascript in the html section of an aspx page above the head section.
This causes every back to return with a forward.
<script type = "text/javascript" >
function disableBackButton()
{
window.history.forward();
}
setTimeout("disableBackButton()", 0);
</script>
plus have a look at this article
https://web.archive.org/web/20210927201700/http://www.4guysfromrolla.com/webtech/111500-1.shtml
If you want to do this by code-behind.Put following code in Page_Load or Page_Init event of the page
protected void Page_Init(object Sender, EventArgs e)
{
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1));
Response.Cache.SetNoStore();
}