Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NETIFrame
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 11:17am
Edited 9-Feb-13 22: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
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 422
1 sanket saxena 355
2 Abhinav S 258
3 OriginalGriff 250
4 CPallini 205
0 Sergey Alexandrovich Kryukov 8,704
1 OriginalGriff 5,000
2 Peter Leow 3,839
3 Maciej Los 3,535
4 Er. Puneet Goel 3,107


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