วันเสาร์ที่ 23 กรกฎาคม พ.ศ. 2559

ASP.NET: ดึง RSS มาแสดงใน Repeater ด้วยคำสั่ง VB.NET

เนื่องจากการที่ผมไม่เคยเขียนคำสั่ง สำหรับดึง RSS มาแสดงบนเว็บไซต์ที่เขียนด้วย ASP.NET ต้องวิจัยอยู่นาน 3 ชั่วโมง ซึ่งก็คุ้มค่าพอสมควร เพราะทำให้ผมเข้าใจการเขียนคำสั่งติดต่อกับ RSS จริงๆคือ เป็นโครงสร้างไฟล์ XML ที่มีรูปแบบตามมาตรฐาน RSS ทำให้สามารถต่อยอดและแสดงผลโดยใช้เครืองมือ Repeater  จากที่ผมใช้เวลา 3 ชั่วโมง หวังว่าท่านที่สนใจสามารถนำโค้ดนี้ไปใช้งานจะประหยัดเวลามากขึ้นนะครับ ^^

***************** VB.NET ************************* 
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim strURL As String
        Dim ds As New DataSet()
        strURL = "http://rssfeeds.sanook.com/rss/feeds/sanook/hitech.computer.index.xml"


        Dim reader As XmlTextReader = New XmlTextReader(strURL)
        ds.ReadXml(reader)

        myRepeater_rss_News_it.DataSource = ds.Tables("item")
        myRepeater_rss_News_it.DataBind()


        ds = Nothing
End Sub
***************** ASP.NET *************************
<asp:Repeater id="myRepeater_rss_News_it" runat="server">
<ItemTemplate>
    <p><%# Eval("title")%></p>
    <p><%# Eval("description")%></p>
    <p><%# Eval("link")%></p>
       <hr />
</ItemTemplate>
</asp:Repeater>

ลิงค์ตัวอย่าง RSS อื่นๆ:
http://www.thaiware.com/rss/rss_latestPost_news.php
https://www.blognone.com/atom.xml

วันเสาร์ที่ 9 กรกฎาคม พ.ศ. 2559

การเรียกข้อมูลในฟังก์ชั่น จากไฟล์ ashx เพื่อนำค่าไปใช้ หรือ แสดงบน javascript


<script type="text/javascript">

    function getData_ashx() {
        var response = '';
        $.ajax({
            type: "GET",
            url: "/class/ashx/GraphHandler.ashx",
            async: false,
            success: function (text) {
                response = text;
            }
        });
        alert(response);
                     return response;
    }
</script>


*********  GraphHandler.ashx  **********************
Imports System.Web
Imports System.Web.Services

Public Class GraphHandler
    Implements System.Web.IHttpHandler

    Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest

        context.Response.ContentType = "text/plain"
        context.Response.Write(getdata_Chart())

    End Sub

    Public Shared Function getdata_Chart() As String
        Dim str As New StringBuilder

        str.Append("{ y: '2006', a: 50, b: 90, c: 112 },")
        str.Append("{ y: '2007', a: 75, b: 65, c: 95 },")
        str.Append("{ y: '2008', a: 50, b: 40, c: 80 },")
        str.Append("{ y: '2009', a: 75, b: 65, c: 96 },")
        str.Append("{ y: '2010', a: 50, b: 40, c: 75 },")
        str.Append("{ y: '2011', a: 75, b: 65, c: 110 },")
        str.Append("{ y: '2012', a: 100, b: 90, c: 132 },")
        str.Append("{ y: '2013', a: 125, b: 110, c: 152 },")
        str.Append("{ y: '2014', a: 145, b: 135, c: 165 }")

        Return str.ToString
    End Function


    ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property

End Class

การใช้ WebClient สำหรับเรียก URL

Dim _url As String = " https :// www . MyDomain . com /?q=ทดสอบ " Dim wc As New System . Net . WebClient () wc . Encodin...