Redirect parent window from iFrame - asp.net

I have an asp.net page inside an iFrame. I want to redirect the parent page (that sits outside the iFrame) when an asp.net button on the asp.net page in the iFrame is clicked.
The asp.net button will need to do some processing before re-directing, so I guess I need to call javascript from the asp.net

just create simple ans: in javascript
<script type="text/javascript">
function NewWindow() {
document.forms[0].target = "_top";
}
</script>
call it any control like button
<asp:LinkButton ID="linkbutton1" Text="Enroll Now" OnClientClick="NewWindow();" runat="server" onclick="enrol_Click"/>

Related

How to get the text from an link on the master page?

I have a master page with a few links on it, not asp:Hyperlinks, just normal tags. The links are on a menu bar that runs along the top of the page.
Then on the child page, when I click a button, I want to be able to get the value of a specific link on the menu bar at the top of the screen on the code behind page.
Does anyone know if I can do this, and if so, how?
I'm using .net web forms.
You can use jQuery to access elements within the master page.
<script>
$(document).ready(function () {
//Some function for someID on your master page:
$("#someID").toggle();
});
</script>
Since the master page and child pages are rendered before the (document).ready method completes, it ensures that all elements built onto the final page are visible.
Placing the above script into your child page will allow you to access elements in the master page file.
You will just need to ensure you have a jQuery link/reference:
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js">
</script>
</head>
EDIT #1:
For getting the text off the master page into the code-behind of the child page you can do this (add hidden field to child page):
<asp:HiddenField ID="hdField" Value="SomeValue" runat="server" />
<script>
$(document).ready(function () {
//Some function for someID on your master page:
$("#hdField").value = ("#IDofLinkOnMasterPage").Value;
});
</script>
Then when your form posts to the child code-behind, you can look for the value of the hidden field by doing this:
var x = hdField.Value.ToString();

ASP.NET AsyncFileUpload - Internet-Explorer Access is denied

I have an asp.net AsynFileUpload control on the page and an html image tag that fires the AsyncFileUpload click event. Works fine in Firefox, Chrome and Safari but not IE.
Example
<script type="text/javascript">
function GetFile() {
document.getElementById("<%=AsyncFileUpload1.ClientID %>").click();
}
</script>
<ajaxToolkit:AsyncFileUpload runat="server" ID="AsyncFileUpload1" />
<img id="flImage1" src="images/upload.png" onclick="GetFile()" />
The uploader works fine in IE if I just use the uploader control. But I need an image on the page that will fire the uploader control click event.
When I click the image that fires the JavaScript GetFile() function, it then calls the click event for the AsynFileUpload control. I can then select my file for upload. Once I select the file I get a JavaScript alert "Access is denied".
Anyone know what the issue is and how to get around it?
Thanks in advance.
I use something like this:
document.getElementById('<%= this.AsyncUpload.ClientID %>' + '_ctl02').click();

asp:linkbutton (navigating to specific page section on post back)

I have some linkbuttons to update my gridview which is in the middle of the page. Everytime I hit edit or delete etc the window scrolls to the top on the page that gets posted back. I want it to stay focused on the grideview. I have tried a javascript function but for some reason it did not work.
(edit: the following works as far as scrolling is concerned but prevents postback)
here is what I tried
<script type="text/javascript" language="javascript">
function goto() {
window.scrollTo(10, 1100);
}
</script>
<asp:LinkButton ID="lbtnGo" runat="server" OnClientClick="javascript:goto();return false;">GO</asp:LinkButton>
source
How can I do this?
Did you try with <%# Page MaintainScrollPositionOnPostback="true" %> in the page declaration?
Regards
Client-side event fires before server-side. So even if you scroll window to correct position - after postback you will be returned to the top. You can add the following code to your server-side LinkButton click event handler:
if (!this.IsStartupScriptRegistered("ScrollToGrid"))
{
String scriptString = "<script language=\"JavaScript\">";
scriptString += "window.scrollTo(10, 1100);";
scriptString += "</script>";
this.RegisterStartupScript("ScrollToGrid", scriptString);
}
this will add javascript block to your page after postback
There are, depending on the .NET framework properties available that can help one out:
ASP.NET 1.x: use SmartNavigation. ASP.NET 2.0: use MaintainScrollPositionOnPostBack. Use an UpdatePanel control to asynchronously update parts of a page
and the best way for this is UpdatePanel control to asynchronously update parts of a page

Using Response.Redirect with jQuery Thickbox

I'm using jQuery Thickbox to display an iframe (upload.aspx) that allows a user to upload a file. In the code behind for the upload.aspx I finish by sending:
Response.Redirect("blah.aspx");
The page I redirect to is dynamic based on the results of the upload process. When this redirect happens, it happens inside the Thickbox and not the parent window as I'd like it to. Here's the calling ASP.NET page (home.aspx):
Add New
And here's the submit button inside of the upload.aspx page:
<asp:Button ID="btnUpload" runat="server" Text="Upload"
OnClick="btnUpload_Click" OnClientClick="self.parent.tb_remove();" />
This is designed to close the modal window and send control to the code behind to perform the file upload, processing, etc.
Has anyone experienced this before? How would I go about sending a redirect on the parent window?
You cannot send a redirect to a parent frame.
Instead, you need to use Javascript.
You can write top.location = "whatever"; in Javascript in the <iframe>.
Here's what I ended up doing.
Added server tags to the body element of upload.aspx:
<body id="mBody" runat="server">
Removed the Response.Redirect, and attached some JavaScript to run on the next load.
HtmlGenericControl body = (HtmlGenericControl)Page.FindControl("mBody");
body.Attributes.Add("onload", "window.top.location.href='blah.aspx';");
And removed the client click from the button:
<asp:Button ID="btnUpload" runat="server" Text="Upload"
OnClick="btnUpload_Click" />

Reverse order of operations for OnClick and OnClientClick?

I have some simple javascript that I'd like to run when a button is clicked, but I also want some postback action to occur on the server. The logical code for this looks like this:
<asp:Button ID="btnOK" runat="server" Text="Save Changes" OnClientClick="UpdateParent();" OnClick="btnOK_Click" />
<asp:Button ID="btnCancel" runat="server" Text="Cancel" OnClientClick="window.close();" />
<script language="javascript" type="text/javascript">
function UpdateParent()
{
window.opener.document.location.reload(true); // or should we postback instead?
window.close();
}
</script>
Basically, a popup window should refresh its parent and then close itself. However... if I call window.close(), the postback does not occur and the button handler is not called. But obviously OnClientClick is called before the postback happens. Am I going to have to emit this javascript in the button handler and run it when the page loads after postback? If so, what is the proper way to do this these days for ASP.NET 2.0?
It's a shame that the code above doesn't work as it's elegantly simple and straightforward.
You have to do the postback before closing the window. Also you want to do the postback before refreshing the parent window, as I guess that the reason to refresh the window is to display the information that you are about to save.
Use the RegisterStartupScript in the ClientScript object to run the code after postback:
Page.ClientScript.RegisterStartupScript(this.GetType(), "close", "window.opener.location.reload(true);window.close();", true);
However, if the parent page is a result of a postback, this would cause a dialog window in the browser informing the user that a post request is needed to reload the page. To avoid this you would have to do something like calling a function in the parent page that could do a postback to update the page.

Resources