am currently using google feed api to get feed links dynamically. am trying to use the results returned by the api to create rss feed for my website..Now the problem is api function call takes place only after the page load so i cant access the url values returned by the api in page load function..

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
   <script type="text/javascript" src="https://www.google.com/jsapi?key=ABQIAAAAxII5vpTAk5gatTPIMAFoCxStIDvCAqMV0_KActMytIL1qEezxxQeBqRzIurcFfnrUgG2YMlC07VgbQ"></script>
    <script type="text/javascript">

        google.load("feeds", "1", { "callback": OnLoad });

        function OnLoad() {
            // Query for president feeds on cnn.com
            var query = 'atlanta bridal shows';
            google.feeds.findFeeds(query, findDone);
        }

        function findDone(result) {
            // Make sure we didn't get an error.
            if (!result.error) {
                // Get content div
                var content = document.getElementById('content');
                var html = '';
                var submenu = new Array()
                // Loop through the results and print out the title of the feed and link to
                // the url.
                for (var i = 0; i < result.entries.length; i++) {
                    var entry = result.entries[i];
                    html += '<p><a href="' + entry.url + '">' + entry.title + '</a></p>';
                    submenu[i] = entry.url;
                }
                content.innerHTML = html;

                document.getElementById('<%= Hidden1.ClientID %>').value = submenu;
            }
        }

        google.setOnLoadCallback(OnLoad);
    </script>

</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:HiddenField ID="Hidden1" runat="server" ondatabinding="Hidden1_DataBinding" 
        onunload="Hidden1_Unload" onvaluechanged="Hidden1_ValueChanged" />
<div id="content"></div>

</asp:Content>

pageload event:

 protected void Page_Load(object sender, EventArgs e)
    {
        WebClient client = new WebClient();
        string path = Request.Url.GetLeftPart(UriPartial.Authority) +
               VirtualPathUtility.ToAbsolute("~/user/feed.htm");

        Stream stream = client.OpenRead(path);
        StreamReader sr = new StreamReader(stream);
        string content = sr.ReadToEnd();

          }

What should i do to access the values returned by the api in page load??

有帮助吗?

解决方案

First of all you have to create feed.htm and put above code in it.

feed.htm (which is located at root of web-app)

<script type="text/javascript" src="https://www.google.com/jsapi?key=ABQIAAAAxII5vpTAk5gatTPIMAFoCxStIDvCAqMV0_KActMytIL1qEezxxQeBqRzIurcFfnrUgG2YMlC07VgbQ"></script>
<script type="text/javascript">

    google.load("feeds", "1", { "callback": OnLoad });

    function OnLoad() {
        // Query for president feeds on cnn.com
        var query = 'atlanta bridal shows';
        google.feeds.findFeeds(query, findDone);
    }

    function findDone(result) {
        // Make sure we didn't get an error.
        if (!result.error) {
            // Get content div
            var content = document.getElementById('content');
            var html = '';
            var submenu = new Array()
            // Loop through the results and print out the title of the feed and link to
            // the url.
            for (var i = 0; i < result.entries.length; i++) {
                var entry = result.entries[i];
                html += '<p><a href="' + entry.url + '">' + entry.title + '</a></p>';
                submenu[i] = entry.url;
            }
            content.innerHTML = html;

            document.getElementById('<%= Hidden1.ClientID %>').value = submenu;
        }
    }

    google.setOnLoadCallback(OnLoad);
</script>
<div id="content"></div>

Then after in page_load event of .aspx page use System.Net.WebClient class methods to request the feed.htm.

TestFeed.aspx (which is located at root of web-app)

Markup:

<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server"></asp:Label>
    </div>
    </form>
</body>

Code-behind:

protected void Page_Load(object sender, EventArgs e)
    {
        WebClient client = new WebClient();
        string path = Request.Url.GetLeftPart(UriPartial.Authority) + VirtualPathUtility.ToAbsolute("~/feed.htm");

        Stream stream = client.OpenRead(path);
        StreamReader sr = new StreamReader(stream);
        //To view the result
        Label1.Text = sr.ReadToEnd();
    }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top