i have a web page in c#, in the codebehind i generate a url, on the aspx page i want to update an iframe to show this url.
after looking a while for the means to do this, i found i can register javascript to force the refresh of the iframe, but now i am experiencing a trouble.
no matter what i try, the url seems to never change, remaining at the on load defined url.
let me show you some of the code so you can see what i am doing and maybe help me with thi issue.
i have this string, who handles the url i want to go
public String currentMap = "google.com";
this is the function who registers the javascript
protected void Page_Load(object sender, EventArgs e)
{
UtilityClass utility = new UtilityClass();
this.Page.ClientScript.RegisterStartupScript(
this.GetType(),
"StartupScript",
"Sys.Application.add_load(MyLoad);",
true);}
this registers the javascript function, this function is suppoused to load a modified url, like this
<script type="text/javascript">
function MyLoad(sender) {
$get('maps').innerHTML += "<%= currentMap %>";
}</script>
while i can see how i the inner html updates (you can see i use the =+ operator, because i wanted to see if i was adding something to the page), but the value for currentMap is always the same, and the iframe does not gets updated.
i launch a function, when i click an object in a gridview, this function does something like this.
currentMap = "<iframe src=\"somepages.html" id=\"viewerframe\" width=\"100%\" height=\"450\"></iframe>"\"";
i can see the iframe updating, but the value remains at http://google.com (a test value hard coded).
how can i update the div so it shows the correct url in the iframe?
thank you for the help
To update the src of an iframe you can use this code:
//Set upload frame source
var uploadUrl = '/Profile/upload.aspx?id=' + $.url().param('id').replace('/', '');
$('#uploadFrame').prop('src', uploadUrl);
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.
In my Page_Load I identify the page header block from my master page and add javascript from an XML file to that part of the page.
It doesn't seem to be loading in time. If I explicitly put the script reference on the page, it works. But not when I load through the page_load event.
Should it be loaded sooner?
You're not adding any code so I'm not able to tell you what you're doing wrong. Instead, I'll tell you a valid way to add script references from code behind.
Something like this on Page_Load should do the trick
var js = new HtmlGenericControl("script");
js.Attributes["type"] = "text/javascript";
js.Attributes["src"] = "myFile.js";
Page.Header.Controls.Add(js);
You can't you put in following method in ur page,
protected void OnInit(EventArgs e)
Use this here i have js folder on root and it work fine for me.
function addJavascript(jsname, pos) {
var th = document.getElementsByTagName(pos)[0];
var s = document.createElement('script');
s.setAttribute('type', 'text/javascript');
s.setAttribute('src', jsname);
th.appendChild(s);
}
addJavascript('js/table.js', 'Head');
call addJavascript method with file path and parent tag where you want to add javascript file
Hope it works for you..
How to refresh the parent window while closing the popup window(child window).
We are calling the java script functions in code behind to refresh the parent window by using page.ClientScript.RegisterStartupScript().But t is working fine in IE(internet explorer) but not working in Mozilla Firefox and Google Chrome.
In the Mozilla Firefox the pop up value is saving in the database but it is not updating into the parent page.If i did refresh manually the value is getting updating into the parent page. If i put debugger in RefreshPage()(javascript function) function in IE it is firing but not in Firefox.
The below code for call the javascript function in .cs class.
page.ClientScript.RegisterStartupScript(this.GetType(), "PopupSave", "<script>javascript:alert('" + dsMessage.Tables[0].Rows[0]["ErrorMessage"].ToString() + "');window.open('','_self','');window.close();window.document.forms[0].submit();</script>");
The above code RefreshPage() is the javascript function to refresh the page
i.e.
function RefreshPage() { window.document.forms[0].submit(); }
Please help me i tried with different scenarios but no output.
instead of RefreshPage() i used different functions
like reload(),
window.opener.forms[0].submit(),
likewise but still no output anyone knows please help me.
Try this function
on submit button click run this script
<script language='javascript'> window.opener.frames.location='somepage.aspx';window.close();</script>
this will help you !!!
Before trying to make js calls between windows.
Set 'document.domain' to the same value on both pages.
protected void ButtonClick(object sender, EventArgs e)
{
string closeAndRefreshScript = #"<script type='text/javascript'>
window.opener.location.reload();
window.close();
</script>";
base.Response.Write(closeAndRefreshScript);
}
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();
I understand there are MANY posts on this, but I have tried every single one and none of them work!
I have an aspx and aspx.cs page being run using visual studio 2010.
on my aspx page I have a table
<table id="viewPendingTable" runat="server"></table>
On the aspx.cs page, the table is loaded with data. This works fine, No Problem. Once the page is loaded, the table contains the dynamically added data I require.
I am using jquery dataTables to style my table using the standard
$(document).ready(function () {
$('#viewPendingTable').dataTable({
"bJQueryUI": true,
});
});
When I create a test table with dummy information directly into the html, the data table displays correctly as required. But when I apply The jquery script to the dynamic table. Nothing happens. The dynamic data is displayed as required, but the data table (search functions, style etc) are not present.
Now I suspect this is because the scripts are run before the table is populated with data.
This is where I began my mission to force the jquery/javascript to run AFTER the aspx.cs has run and the table has been populated.
But I failed... So just as a test I thought I'd run an alert('YAAY'); from the c# side using registered start up scripts. but to no Avail that wouldnt work.
So can somebody PLEASE tell me why this isn't working.
I've tried Multiple Different ways of startup scripts!
My aspx is:
<table id="viewPendingTable" runat="server"></table>
<script type="text/javascript" language="javascript" id="ShowTable">
<!--
loadTable();
function loadTable() {
$(document).ready(function () {
$('#viewPendingTable').dataTable({
"bJQueryUI": true,
"sPaginationType": "full_numbers"
});
});
alert('ALEX');
document.getElementById("table_id").width = "100%";
}
//-->
</script>
And my aspx.cs is:
protected void Page_Load(object sender, EventArgs e)
{
loadMyTable();
ClientScriptManager a = null;
a.RegisterStartupScript(GetType(), "anotherkey", "alert('ASDFASDFASDF');", true);
a.RegisterStartupScript(GetType(), "anfgsdgsderkey", "alert('MOTHER');", false);
a.RegisterStartupScript(this.GetType(), "AKey", "loadTable();", true);
ClientScript.RegisterStartupScript(GetType(), "MyScript", "<script language=javascript>" + "alert('Hello ASP.NET'); }</script>");
ScriptManager.RegisterStartupScript(this.Page, GetType(), "script", "alert('Success!');", true);
Page.ClientScript.RegisterClientScriptBlock(GetType(), "script", "alert('AAA');", true);
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "script",
"alert('a');" +
"$(document).ready(function (){" +
"$('#viewPendingTable').dataTable({" +
"\"bJQueryUI\": true" +
"\"sPaginationType\": \"full_numbers\"" +
"});" +
"}); alert('b'); document.getElementById('viewPendingTable').width = 100%", true);
}
I have tried more methods for trying to run javascript after page load but they were lost in the anger of failing so badly.
Somebody PLEASE HELP!
Why aren't my simple javascript alerts running?
Even if I got them to work... would the data table be correctly styled by executing the jquery after page Load???
Thankyou Kindly for your time
Alex
p.s. No comments on asp:gridView or asp:DataTable as That Failed EPICALLY and i posted a question on it days ago and nobody has replied. If you care to take a look
CLICK HERE
Change this line of code and try
"$('# " + viewPendingTable.ClientID + "').dataTable({" +
in you document.read script that you have written
Its because you can get acutal ID of the table control when the page get render on the browser
Edit
Make use of
RegisterStartupScript because it emits your JavaScript at the end of the page just before </form> tag (before the <form> tag ends).
The most likely issue you're facing is that id="viewPendingTable" doesn't exist on your page.
By adding the runat="server" attribute on your table, the ID will be changed so that ASP.NET can bind to it on postback.
You'll need to use the ClientID property of the table in your jQuery:
viewPendingTable.ClientID
First of all, client side code is ALWAYS run after the server side code is executed. Although client side code can be run before the html is rendered in your browser. So this has nothing to do with that.
If you check the generated html, you will see that asp.net will generate the id's voor the element to which you apply the runat attribute. To solve this, add a class to the table and change your js selector to $(".classname").
Extra tip: Use a debugger like firebug which allows you to debug your js code, which makes it easy to solve problems like these :)