Click here to Skip to main content
15,881,838 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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:
ASP.NET
<%@ 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:

C#
<%@ 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
Updated 9-Feb-13 22:40pm
v2
Comments
Sandeep Mewara 10-Feb-13 3:11am    
Please elborate and share what are you trying to do.
shajarian_lover 10-Feb-13 7:20am    
i'm improved my question, please check it.

Since you already have jquery code to call the webservice, just change the iframe.src in your success function of .ajax.
 
Share this answer
 
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.

C#
document.getElementsByID("your iframe id here").attr("src",input);


Thanks
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900