Prevent Iframe reloading on post back - iframe

I have a webpage with one iframe and a button.
On Page Load event,
if (!Page.IsPostBack)
{
string sDocUrl = //some doucmen url
Iframe1.Attributes["src"] = sDocUrl;
}
When the page is reloaded (by clicking a button), this iframe is being
reloaded too. This iframe has a static data and doesn't need to be
reloaded everytime when I click a button. Is there a way to prevent
iframe to be reloaded when webform is reloaded?

When a page is reloaded, any iframes contained in it (just as any scrips, images, anything) will always be reloaded. There's no working around that, sorry, simply because the entire page actually reloads.

You could add the iframe dynamically using JavaScript:
if (!Page.IsPostBack)
{
$('#foo').append('<iframe src="' + sDocUrl + '"></iframe>');
}
That uses jQuery to add the iframe to an element with an id of "foo". But it will only add the iframe if the expression in the 'if' statement returns true.

Related

Why when loading page inside another using ajax the parent posts back to the sub page?

I'm loading page inside div in another page..the problem is when I press LinkButton in the parent page to do postback, the postback goes to the sub page not to the parent one.
I'm using jQuery ajax to load the page:
$('#myDiv').html(refreshingDiv).load('subPagePath.aspx');
How do I solve this problem ?
You should not load the whole ASPX Page into the DIV because when you make a AJAX request to get an ASPX page from the server and load it into the DIV, actually some parameters like ViewState of the main page gets overwritten. Accordingly the ViewState will gets corrupted however if you load a HTML Page into a DIV, you can see it will be working fine.
So, Instead get the Data in the JSON Format and load in into the DIV accordingly
Hope it answers your question!!

Best way to access controls on parent page from a child page popup?

I have a parent ASP.NET page which has a link that opens a child ASPX page in the form of a popup.
I need to control the parent page from the popup page's code-behind. For example enablie/disable the visibility of a label in the parent page, or setting text value for a text box.
How can I accomplish this?
Code used to open the popup ASPX:
//JavaScript function:
function showPopup()
{
var strReturn = window.open("TaxReportInputsForm.aspx",'popup','width=390,height=120');
}
Button code:
Page.ClientScript.RegisterStartupScript(GetType(), "popup", "showPopup();", true);
The problem that you have is that HTTP is stateless and so each HTTP request/page is handled entirely independently - this means that there is no way to directly modify what is rendered on the parent page while processing a request from the child page.
What you can do however is indirectly modify what is rendered by having the child write out some flags/information to some place that both the child and the parent page can access, and then having the parent page look at that information to determine what to render. There are loads of different places that you could store this state, e.g.
Cookies
Session variables (i.e. in memory on the server)
In a hidden field / query string in the page
For example, your child page might write a ShowSomeLabel flag to a cookie in the code-behind, then when the child page returns to the client it forces a refresh of the parent page, then in the code-behind for the parent page it can read that flag from the cookie to determine what labels should be visible.
All of these methods will require at least some JavaScript to cause of refresh of the parent page, however some will need additional JavaScript to copy hidden field / query string values from the child page to the parent page before it submits. Exactly where and how you should store this information depends on your specific requirements.
you can try to use javascript,
suppose A.aspx is parent page and B.aspx is the open page,
you can use "opener" to contronl DOM of A.aspx.
A.aspx link tag and javascript :
open page B
<script type="text/javascript">
function fun_A() {
alert("hello");
}
</script>
B.aspx
<input id="btnChild" type="button" value="call function in Page A" onclick="opener.fun_A();" />
when you open B.aspx from A.aspx, and click the button from B.aspx,
you will see A.aspx alert message "hello".
hope this can help you.

ASP.NET change captcha without page refresh

I've created a simple captcha system in my assp.net web application, I use an ASPX page as ImageUrl of my captcha image, this ASPX file creates captcha image and returns it to be used as my image source. It works fine but I'm going to insert a Change Captcha button which I'd like to change my captcha WITHOUT REFRESH, I've used to methods with no success,
first I tried Ajax Update panel and inserted my change captcha button and my captcha image into it, my captcha didn't change (of course when I use a button outside of update panel to change captcha, it works fine but I have page refresh),
this is my button click code:
protected void Button1_Click(object sender, EventArgs e)
{
imgCaptcha.ImageUrl = "CreateCaptcha.aspx?New=1";
}
then I used a Jquery Ajax call (web method) to call my CaptchaImage.ASPX file and use it as my image source, but again I had no luck! what is going wrong here? in both cases my CaptchaImage.ASPX file is never called! how can I change my captcha image without page refresh?
thanks
$('#btnSearch').click(function () {
var src = //Url to image
$('#image').attr("src", src);
});
Above code changes source of image (#image) used b'coz image is the id for the imagebox.or whatever u using.

loading different pages in Iframe on button click, button is in main page

I have a back and next buttons and Iframe in main page.
On pressing next button next pages (which are forms to take input) are displayed in Iframe from server side.
I want to save data of current page in DB and then load 2nd page in Iframe. On clicking again save data of Page2 in DB and then load 3rd page in Iframe.
Similarly if user clicks back button he should get data of previous page.
Please suggest me how it can be achieved.
Thanks
To chane iframe src just use JS
function changeFrm(NewSrc){
document.getElementById('YOUR IFRAME ID').src=NewSrc;
};
And in your button add onClick="ChangeFrm('NEW SOURCE');"
Good luck!

Changing target in form of Asp.net page

I am using IFrame in my ASP.NET pages. For the page that I am referencing in my IFrame, I have a gridview (I also have CommandField that referencing another asp.net page) and paging enabled.
I wanted to display a new page (outside of Iframe) when a user clicks on the commandfield in the gridview. In order to do that I set form's target to "_parent". This works fine (it open a new page outside of Iframe). Now, my problem is that when i click on the paging to go to next page in the gridview, it also opens the gridview outside of IFrame (which I dont want to happen). I want it to open within the Iframe.
Is there any way I could set the target as I needed?
Thanks.
Use some jQuery: (Just threw this idea together, not tested)
$("#myElement").click(function() {
if(window.location != window.parent.location)
{
// window is in iframe
$(this).attr("target", "_self");
}else{
$(this).attr("target", "_parent");
}
});

Resources