かんたんASP.NET - リスト

第1章


リスト 11-1 チャートコントロールへのデータバインド

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  Dim data = {
    New With {.Name = "Col1", .Value = 10},
    New With {.Name = "Col2", .Value = 30},
    New With {.Name = "Col3", .Value = 50},
    New With {.Name = "Col4", .Value = 20},
    New With {.Name = "Col5", .Value = 40}
    }
  Chart1.DataSource = data
  Chart1.DataBind()
End Sub

protected void Page_Load(object sender, EventArgs e)
{
  var data = new[]
  {
    new { Name = "Col1", Value = 10 },
    new { Name = "Col2", Value = 30 },
    new { Name = "Col3", Value = 50 },
    new { Name = "Col4", Value = 20 },
    new { Name = "Col5", Value = 40 }
  };
  Chart1.DataSource = data;
  Chart1.DataBind();
}

↑Top


リスト 11-2 URLルーティング設定

<%@ Application Language="VB" %>
<%@ Import Namespace="System.Web.Routing" %>

<script runat="server">
  Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    Dim routes = RouteTable.Routes
    routes.MapPageRoute(
      "post-info",
      "{year}",
      "~/Post.aspx"
      )
  End Sub
</script>

<%@ Application Language="C#" %>
<%@ Import Namespace="System.Web.Routing" %>

<script runat="server">
  void Application_Start(object sender, EventArgs e)
  {
    var routes = RouteTable.Routes;
    routes.MapPageRoute(
      "post-info",
      "{year}",
      "~/Post.aspx"
      );
  }
</script>

↑Top


リスト 11-3 処理を行うWebページ(Post.aspx)

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Post.aspx.vb" Inherits="Post" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title>URLルーティングテスト</title>
</head>
<body>
  <form id="form1" runat="server">
  <div>
    <asp:Label ID="YearLabel" runat="server"></asp:Label>
    年
  </div>
  </form>
</body>
</html>

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Post.aspx.cs" Inherits="Post" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title>URLルーティングテスト</title>
</head>
<body>
  <form id="form1" runat="server">
  <div>
    <asp:Label ID="YearLabel" runat="server"></asp:Label>
    年
  </div>
  </form>
</body>
</html>

↑Top


リスト 11-4 処理を行うWebページ(Post.aspx.vb)

Partial Class Post
  Inherits System.Web.UI.Page

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim year As String = DirectCast(RouteData.Values("year"), String)
    YearLabel.Text = year
  End Sub
End Class

using System;

public partial class Post : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    string year = (string)RouteData.Values["year"];
    YearLabel.Text = year;
  }
}

↑Top


リスト 11-5 ASP.NET 4のWeb.config(空のWebサイトの場合)

<?xml version="1.0"?>

<!--
  ASP.NET アプリケーションを構成する方法の詳細については、
  http://go.microsoft.com/fwlink/?LinkId=169433 を参照してください
  -->

<configuration>
    <system.web>
        <compilation debug="false" strict="false" explicit="true" targetFramework="4.0" />
    </system.web>

</configuration>

<?xml version="1.0"?>

<!--
  ASP.NET アプリケーションを構成する方法の詳細については、
  http://go.microsoft.com/fwlink/?LinkId=169433 を参照してください
  -->

<configuration>

    <system.web>
        <compilation debug="false" targetFramework="4.0" />
    </system.web>

</configuration>

↑Top