.NET(Webサービス) Webサービス

VisualStudioを起動し新しいWebサイトを作成します。
「Visual Studioにインストールされたテンプレート」より「ASP.NET Webサービス」を選択します。


作成されたWebサイトには、App_Codeフォルダ配下に「Service.vb」が用意されています。
Service.vbには「HelloWorld」メソッドが用意されています。


作成したWebサービスをIIS上で動作するよう設定します。
「コントロールパネル」→「管理ツール」→「インターネットインフォーメーションサービス」を起動します。
「Webサイト」-「既定のWebサイト」を開き、「既定のWebサイト」の右クリックメニューより「新規作成」-「仮想ディレクトリ」を選択します。
「仮想ディレクトリの作成ウイザード」が起動します。
「エイリアス」に"WebService"と入力します。


「ディレクトリ」に先ほど作成したWebサイトのパスを指定します。


Webサービスの動作確認を行います。
http://localhost/WebService/Service.asmxにアクセスし下記ページが表示されればOKです。


次にWebサービスの呼び出し側のプログラムとしてWindowsアプリケーションを作成します。
作成したWindowsアプリケーションプロジェクトに「Web参照の追加」を行います。


URLにはhttp://localhost/WebService/Service.asmxを指定します。


FormにButtonを一つとLabelを一つ配置します。
ButtonのClickイベントに下記コードを記述します。
Private Sub Button1_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) _

Handles Button1.Click

    Dim CallWebService As New localhost.Service

    Dim sResult As String = CallWebService.HelloWorld

    Me.Label1.Text = sResult

End Sub


実行してみます。
Button1をクリックするとLabelに「Hello World」が表示されます。

ASP.NET AJAX Extensions1.0をインストール

ASP.NET2.0 AJAX Extensions1.0はASP.NET AJAXの基本的な機能を提供するパッケージです。ASP.NET AJAX Control Toolkitをインストールする場合にも必ずAJAX Extensions1.0を先にインストールしておく必要があります。

http://www.asp.net/ajax/ よりASP.NET AJAXをダウンロードします。






ダウンロードしたファイルを実行します。








VisualStudioを起動し新しいWebサイトを作成を実行するとテンプレート一覧に「ASP.NET AJAX-Enabled Web Site」が追加されています。

はじめてのASP.NET開発

今回は急遽ASP.NETで開発をすることになりました。
最初は以前ASP.NETで作成されたプロジェクトがあまりに汚く、バグがでているのでキレイにして欲しいという依頼でした。
どれだけ汚いんだろうとコードを見てみると、想像以上の汚でした。
それでも最初は改修しながらなんとかキレイにしようとがんばったんですが・・・お手上げ。絶対作り直すほうが早い!!

ASP.NETは今まで勉強していましたが開発経験はありません。
今回のプロジェクトは規模も小さいし納期も融通が効くらしいので勉強がてら調度いい。

作り直し以外は断固拒否し、結局作り直すことになったのですが、楽しかったですぅ。本当にいろいろ勉強になりました。
参考書片手にプログラムを作るのと実開発では学習スピードが全然違いますね~。
勉強しながらお給料がもらえるなんてすごく得した気分ですぅ。

でも
目も当てられないプロジェクトを担当した張本人が、自分でも汚いと自覚して作り直す事にしたのに「ココはこうした方がよいのでは?」とか言ってくる。

お前に言われたかないヤイっ

しかも間違ってるし。
嫌味たっぷりに「そのこだわりが1回目の開発に生かされていないのはナゼですか?」と質問しました。
「だってぇ・・・(ごにょごにょ)あんまりよく知らなかったし」

はぁ?もう少し勉強してください。

っていうか

勉強してから開発してください。

この人いつも「行き当たりばったり開発」です。

ASP.NET Tableタグのスタイルシートを動的に変更

デザイナでTABLEを選択し右クリックメニューより「サーバーコントロールとして実行」をチェックONにし,
HtmlTableサーバーコントロールに変更します。

ページにはテーマによってスタイルシートが読み込まれているものとします。
Dim table As HtmlTable = DirectCast(page.FindControl("TABLE1"), HtmlTable)
table.Attributes("Class") = "table1"
table.Rows(0).Attributes("Class") = "th1"

ASP.NET GridViewのTemplateFieldに配置したボタンのクリックイベント

「電話番号を表示」ボタンをクリックしたとき、選択した行の電話番号をグリッド下部のLabelコントロールに表示します。


TemplateFieldに「電話番号を表示」Buttonコントロールを配置します。


    Protected Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgs) _

    Handles Me.Load

        If Not IsPostBack Then

            Dim dt As New DataTable

            dt.Columns.Add("CustomerID"GetType(String))

            dt.Columns.Add("CustomerNm"GetType(String))

            dt.Columns.Add("TEL"GetType(String))


 

            Dim row As DataRow

            For i As Integer = 1 To 10

                row = dt.NewRow

                row("CustomerID") = i.ToString

                row("CustomerNm") = "会社名" & i.ToString

                row("TEL") = i & i & i & "-" & i & i & i & i & "-" & i & i & i & i
 

                dt.Rows.Add(row)

            Next


 

            Me.GridView1.DataSource = dt

            Me.GridView1.DataBind()

        End If

    End Sub


 

    Protected Sub GridView1_RowDataBound(ByVal sender As ObjectByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) _

    Handles GridView1.RowDataBound

        If e.Row.RowType = DataControlRowType.DataRow Then


 

            Dim rowData As DataRowView = DirectCast(e.Row.DataItem, DataRowView)


 

            Dim btnShowTel As Button

            btnShowTel = DirectCast(e.Row.FindControl("btnShowTel"), Button)


 

            'ここでRowCommandで必要なデータを設定しておきます。

            btnShowTel.CommandName = "btnShowTel"

            btnShowTel.CommandArgument = rowData("TEL").ToString


 

        End If

    End Sub


 

    Protected Sub GridView1_RowCommand(ByVal sender As ObjectByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) _

    Handles GridView1.RowCommand

        'RowDataBoundで設定したCommandName、CommandArgumentを使用して、ラベルに電話番号を表示します。

        If e.CommandName = "btnShowTel" Then

            Dim tel As String = e.CommandArgument.ToString

            Me.lblTel.Text = tel

        End If

    End Sub

ASP.NET GridViewの文字列をクリックしてポップアップを表示する

GridViewのセルをクリックしてポップアップを表示する とよく似ていますが、セルに表示されている「文字列」をクリックしてポップアップを表示します。



CompnyNm列をTemplateFieldにし、LinkButtonコントロールを配置します。


<script type="text/javascript" >

function ShowPopup(str)    
{   
        var w = 150;
        var h = 100;
        var x = (screen.width  - w) / 2;
        var y = (screen.height - h) / 2;
        ChildWindow = window.open('Default2.aspx?TEL=' + str ,"", "width=" + w + ",height=" + h + ",top=" + y + ",left=" + x + ",dependent=yes,location=no,menubar=no,resizable=yes,scrollbars=no,status=no,titlebar=no,toolbar=no"); 

</script>


Imports System.Data



Partial Class _Default

    Inherits System.Web.UI.Page



    Protected Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgs) _

    Handles Me.Load

        If Not IsPostBack Then

            Dim dt As New DataTable

            dt.Columns.Add("CustomerID"GetType(String))

            dt.Columns.Add("CustomerNm"GetType(String))

            dt.Columns.Add("TEL"GetType(String))



            Dim row As DataRow

            For i As Integer = 1 To 10

                row = dt.NewRow

                row("CustomerID") = i.ToString

                row("CustomerNm") = "会社名" & i.ToString

                row("TEL") = i & i & i & "-" & i & i & i & i & "-" & i & i & i & i

                dt.Rows.Add(row)

            Next



            Me.GridView1.DataSource = dt

            Me.GridView1.DataBind()

        End If

    End Sub


 

    Protected Sub GridView1_RowDataBound(ByVal sender As ObjectByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) _

    Handles GridView1.RowDataBound

        If e.Row.RowType = DataControlRowType.DataRow Then



            Dim lbtnCustomerNm As LinkButton

            lbtnCustomerNm = DirectCast(e.Row.FindControl("lbtnCustomerNm"), LinkButton)



            Dim rowData As DataRowView = DirectCast(e.Row.DataItem, DataRowView)

            lbtnCustomerNm.OnClientClick = String.Format("ShowPopup('{0}');", rowData("Tel").ToString)

            '又は

            'lbtnCustomerNm.Attributes.Add("OnClick", String.Format("ShowPopup('{0}');", rowData("Tel").ToString))



        End If

    End Sub

ASP.NET GridViewのセルをクリックしてポップアップを表示する

GridViewの「セル」をクリックしてポップアップ画面を表示します。
表示している「文字列」ではありません。



<script type="text/javascript" >

function ShowPopup(str)    
{   
        var w = 150;
        var h = 100;
        var x = (screen.width  - w) / 2;
        var y = (screen.height - h) / 2;
        ChildWindow = window.open('Default2.aspx?TEL=' + str ,"", "width=" + w + ",height=" + h + ",top=" + y + ",left=" + x + ",dependent=yes,location=no,menubar=no,resizable=yes,scrollbars=no,status=no,titlebar=no,toolbar=no"); 

</script>


Imports System.Data



Partial Class _Default

    Inherits System.Web.UI.Page



    Protected Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgs) _

    Handles Me.Load

        If Not IsPostBack Then

            Dim dt As New DataTable

            dt.Columns.Add("CustomerID"GetType(String))

            dt.Columns.Add("CustomerNm"GetType(String))

            dt.Columns.Add("TEL"GetType(String))



            Dim row As DataRow

            For i As Integer = 1 To 10

                row = dt.NewRow

                row("CustomerID") = i.ToString

                row("CustomerNm") = "会社名" & i.ToString

                row("TEL") = i & i & i & "-" & i & i & i & i & "-" & i & i & i & i

                dt.Rows.Add(row)

            Next



            Me.GridView1.DataSource = dt

            Me.GridView1.DataBind()

        End If

    End Sub


 

    Protected Sub GridView1_RowDataBound(ByVal sender As ObjectByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) _

    Handles GridView1.RowDataBound

        If e.Row.RowType = DataControlRowType.DataRow Then

            Dim rowData As DataRowView = DirectCast(e.Row.DataItem, DataRowView)

            e.Row.Cells(1).Attributes.Add("OnClick"String.Format("ShowPopup('{0}');", rowData("Tel").ToString))

        End If

    End Sub

ASP.NET GridViewにToolTipを表示する。




Imports System.Data



Partial Class _Default

    Inherits System.Web.UI.Page



    Protected Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgs) _

    Handles Me.Load

        If Not IsPostBack Then

            Dim dt As New DataTable

            dt.Columns.Add("CustomerID"GetType(String))

            dt.Columns.Add("CustomerNm"GetType(String))

            dt.Columns.Add("TEL"GetType(String))



            Dim row As DataRow

            For i As Integer = 1 To 10

                row = dt.NewRow

                row("CustomerID") = i.ToString

                row("CustomerNm") = "会社名" & i.ToString

                row("TEL") = i & i & i & "-" & i & i & i & i & "-" & i & i & i & i

                dt.Rows.Add(row)

            Next



            Me.GridView1.DataSource = dt

            Me.GridView1.DataBind()

        End If

    End Sub


 

    Protected Sub GridView1_RowDataBound(ByVal sender As ObjectByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) _

    Handles GridView1.RowDataBound

        If e.Row.RowType = DataControlRowType.DataRow Then

            Dim rowData As DataRowView = DirectCast(e.Row.DataItem, DataRowView)

            e.Row.Cells(1).Attributes.Add("Title", rowData("Tel").ToString)

        End If

    End Sub

ASP.NET GridViewに表示する書式を設定する

GridViewに表示する書式を設定するには列のDataFormatStringプロパティに書式を設定します。
その際HtmlEncodeプロパティをFalseに設定する必要があります。

ASP.NET マスターページのContentPlaceHolder内に配置したコントロールにアクセスするには

マスターページを使用したページでContentPlaceHolder内に配置したコントロールにアクセスするには

'まずページのマスターページからコンテンツを探します。

Dim content As ContentPlaceHolder

content =DirectCast(me.Master.FindControl("ContentPlaceHolder1"),ContentPlaceHolder)

            

'コンテンツからコンテンツに配置したコントロールを探します。

Dim text As TextBox = DirectCast(content.FindControl("TextBox1"),TextBox)