Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET IFrame
Hello all,
 
how can i change the src of iframe from asp webmethod, if it is impossible, how can i call javascript function from webmethod??
this is my page:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
 
<!DOCTYPE html>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Change iframe src</title>
    <script src="jquery-1.9.1.js"></script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <input type="button" name="name" value="Click to change the src of iframe" onclick="change_url('1');" />
        </div>
        <div>
            <iframe id="myFrame" width="100%" height="100%"></iframe>
        </div>
    </form>
    <script type="text/ecmascript">
        function change_url(input) {
            $.ajax({
                type: "POST",
                url: "./Default.aspx/GetData",
                data: '{"id":"' + input + '"}',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    alert('iframe src was changed successfully!');
                }
            });
        }
    </script>
</body>
</html>
 
 
and this is my code:
 
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
 
<!DOCTYPE html>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Change iframe src</title>
    <script src="jquery-1.9.1.js"></script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <input type="button" name="name" value="Click to change the src of iframe" onclick="change_url('1');" />
        </div>
        <div>
            <iframe id="myFrame" width="100%" height="100%"></iframe>
        </div>
    </form>
    <script type="text/ecmascript">
        function change_url(input) {
            $.ajax({
                type: "POST",
                url: "./Default.aspx/GetData",
                data: '{"id":"' + input + '"}',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    alert('iframe src was changed successfully!');
                }
            });
        }
    </script>
</body>
</html>
 
Posted 9-Feb-13 12:17pm
Edited 9-Feb-13 23:40pm
v2
Comments
Sandeep Mewara at 10-Feb-13 3:11am
   
Please elborate and share what are you trying to do.
shajarian_lover at 10-Feb-13 7:20am
   
i'm improved my question, please check it.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Since you already have jquery code to call the webservice, just change the iframe.src in your success function of .ajax.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

HI,
 
iframe has a property called src. You are using the jquery here and its very easy to append an attribute on success of the method.
 
Use the following code on success method.
 
document.getElementsByID("your iframe id here").attr("src",input);
 
Thanks
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 365
1 Sergey Alexandrovich Kryukov 329
2 CPallini 270
3 Afzaal Ahmad Zeeshan 204
4 DamithSL 194
0 OriginalGriff 5,515
1 DamithSL 4,451
2 Maciej Los 3,902
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,175


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 14 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100