ASP.NETでフォームの継承は可能か?

このQ&Aのポイント
  • ASP.NETで似たような画面を複数作る場合、フォームの継承はできるのか疑問に思っています。
  • C#の経験があるため、C#の継承のようにフォームを継承することが可能なのか知りたいです。
  • 現在、no2からno1を継承しようとしていますが、表示されません。どのような方法があるのでしょうか?
回答を見る
  • ベストアンサー

【ASP.NET】フォームの継承は出来ますか?

ASP.NETで似たような画面を複数作るにあたって、 フォーム(デザイン?)の継承が出来たらなぁと思っています。 C#(当方、C#の経験しかありません(--;) )ですと Form1にButtonコントロールを貼り付けて、Form2からForm1を継承すると Form1のButtonコントロールを継承出来ますよね。(デザイナ上も表示される) ASP.NETでもそういったことが出来ないでしょうか? 現在、下のような状態です。 no2からno1を継承しようとしています。 違うんだろうなということは分るのですが インターネットで調べても分からないので 「こんな感じだろ!」という独断と偏見で作りました…^^; ======================================= 【 no1.aspx 】 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="no1.aspx.cs" Inherits="xxx.no1" %> <!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>無題のページ</title> </head> <body> <form id="form1" runat="server"> <div> <!-- ボタンを貼ってみました --> <asp:Button ID="Button1" runat="server" BackColor="#FFC0FF" BorderColor="Blue" Text="Button" /> </div> </form> </body> </html> 【 no2.aspx 】 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="no2.aspx.cs" Inherits="xxx.no1" %> <!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>無題のページ</title> </head> <body> <form id="form1" runat="server"> <div> <!-- Inheritsに"xxx.no1"と記述したので、no1で貼ったボタンが表示されたら嬉しい… --> </div> </form> </body> </html> ======================================= えー…、no2を実行しても真っ白画面でした。 出来れば継承したものがno2のデザイナ上にも表示されて欲しいのですが…。 また 「そういう方法はないけど、こういう方法ならあるよ」 や 「ASP.NETで似たような画面を複数作る場合は普通こうするんだよ」 ということがあれば教えて頂けたらなと思います。 よろしくお願い致します。

質問者が選んだベストアンサー

  • ベストアンサー
  • aspnet
  • ベストアンサー率79% (72/91)
回答No.1

class1.aspxは動的コンパイル時に、class1.aspx.cs(裏コード)が生成した"class1クラス"("class1クラス"はさらにSystem.Web.UI.Pageを継承している)しますので、class2.aspxで、class1を継承すると、class1.aspxの基底クラスを継承したことになり、class1.aspxが生成したクラスは継承できません。 asp.netで共通UIのページを作るときは、「マスターページ」(共通ページを作る場合)か、「ユーザコントロール」(共通部品を作る場合)を使うのが常套手段です。

sakura8400
質問者

お礼

回答ありがとうございます。 >class1を継承すると、class1.aspxの基底クラスを継承したことになり Σ(´д`;)な、なるほど。。。ここが分かっていませんでした。 勉強になります。 「マスターページ」…探し求めていたのはまさにコレです! 無事にUI部分は共通化できました。ありがとうございました。 よろしければコチラもお願い致します。。。 【ASP.NET】マスターページに配置されたボタンのイベントを拾いたい http://oshiete1.goo.ne.jp/qa5232630.html

関連するQ&A

  • <ASP>にTextBoxの値を設定する方法

    TextBoxに記述されたDB接続文字列を<ASP>内で設定しようと試みておりますが、失敗します。どのように記述を改めればよろしいでしょうか? ----------------- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebForm1" %> <script runat="server"> protected void Page_Load(object sender, EventArgs e) { TextBox1.Text = Application["DB_CON_STRING"].ToString(); } </script> <!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>無題のページ</title> </head> <body> <form id="form1" runat="server"> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <div> </div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"> <Columns> <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" /> <asp:BoundField DataField="DATETIME" HeaderText="DATETIME" SortExpression="DATETIME" /> <asp:BoundField DataField="MESSAGE" HeaderText="MESSAGE" SortExpression="MESSAGE" /> </Columns> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="form1.TextBox1.Text"★ここがうまくいかない★ SelectCommand="SELECT * FROM TEST ORDER BY ID DESC"></asp:SqlDataSource> </form> </body> </html>

  • ページごとに有効な変数

    こんにちは。 ASP.NET2.0+C#で開発しています。 こういった表現でよいのか、いまいち自信がないのですが、ページごとに有効な変数が欲しい場合には、どうすればよいのでしょうか? 例えば、カウンタを実現しようとして、 .aspx <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Count.aspx.cs" Inherits="Count" %> <!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>無題のページ</title> </head> <body> <form id="form1" runat="server"> 「<asp:Label ID="Num" runat="server" />」<br /> <asp:Button ID="CountButton" runat="server" OnClick="Count_Click" Text="カウント" /> </form> </body> </html> .aspx.cs public partial class Count : System.Web.UI.Page { int count; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { count = 0; Num.Text = count.ToString(); } } protected void Count_Click(object sender, EventArgs e) { count++; Num.Text = count.ToString(); } } と、した場合、countは毎回初期化されてしまうため、ブラウザ上の表示は1から先に進みません。 かといって、 static int count; とすると、複数のブラウザでページを開いた場合に、値を共有してしまいます。 表示が進み、かつ値を共有しないようにするためには、どのような手法を用いればよいのでしょうか? なお、今回は実験のため、int変数一つですが、実際には可変長の配列となる予定です。よろしくお願いします。

  • SqlDataSource1のドロップダウンリストにページロード時に値

    SqlDataSource1のドロップダウンリストにページロード時に値をいれたいのですが どのようにしたらいいのでしょうか? SqlDataSource にDropDownList1と同じ内容のデータベース 1 りんご 2 みかん 3 ぶどう でページロード時3を指定しているようにしたいのですが 下記ではDropDownList1はできるのですがSqlDataSourceを使っているDropDownList2 では 項目一覧に存在しないため、'DropDownList2' に SelectedValue を指定することは無効です。 パラメータ名: value というエラーがになってしまいます。 どうしたらできるでしょうか? <%@ Page Language="VB" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) DropDownList1.Text = "3" DropDownList2.Text = "3" End Sub </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>無題のページ</title> </head> <body> <form id="form1" runat="server"> <div> <asp:DropDownList ID="DropDownList1" runat="server"> <asp:ListItem>りんご</asp:ListItem> <asp:ListItem>みかん</asp:ListItem> <asp:ListItem>ぶどう</asp:ListItem> </asp:DropDownList> <asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="SqlDataSource1" DataTextField="MyLike" DataValueField="NO"> </asp:DropDownList> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:testConnectionString %>" SelectCommand="SELECT [NO], [MyLike] FROM [MyLike]"></asp:SqlDataSource> </div> </form> </body> </html>

  • ASP.NETでTimerコントロールを使用したい

    ASP.NETWebアプリケーションについての質問です。 ajaxのTimerコントロールでイベントを発生させ、画面の再描画をしようとしています。 コードは以下のような感じで、OnPreRenderイベント内で描画処理を行います。 Partial Public Class _Default Inherits System.Web.UI.Page Private str As String = "" Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load str = CStr(Date.Now) End Sub Protected Overrides Sub OnPreRender( _ ByVal e As System.EventArgs _ ) Label1.Text = str End Sub End Class Timerコントロールを配置して実行したところ、 Script controls may not be registered before PreRender. というエラーが出てしまいます。 調べてみたのですが原因がわかりません。 何かプロパティの設定などが必要なのでしょうか? aspx のコードは以下のとおりです。 <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="WebApplication5._Default" %> <%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="System.Web.UI" TagPrefix="asp" %> <!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>無題のページ</title> </head> <body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server" ></asp:ScriptManager> <asp:Timer ID="Timer1"runat="server"></asp:Timer> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Labe> l </form> </body> </html> 回答よろしくお願いいたします。

    • ベストアンサー
    • AJAX
  • FormViewのEditItemTemplateに配置したボタンへのjavascript組み込み

    はじめまして セバといいます。 VBでaspxを利用しシステムを開発していますが、 次のような現象が発生し困っています。 仕様 1.FormviewのItemTemplateに配置している「edit_Chage」ボタンで FormviewをEditmodeに変更します。 2.FormviewのEditItemTemplateには「hello」ボタンを配置し、 ボタン押下時にjavascriptを起動します。 問題点 FormviewのFormView1_ModeChangedにてイベントを拾い、 「hello」ボタンにjavascriptを組み込もうとしていますが、 この時点では「hello」ボタンのインスタンスが生成おらず、 『オブジェクト参照がオブジェクト インスタンスに設定されていません。』 のエラーになってしまいます。 サンプルプログラム /////////////////////////////// Default.aspx <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %> <!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>無題のページ</title> </head> <body> <form id="form1" runat="server"> <div> </div> <asp:FormView ID="FormView1" runat="server" DataSourceID="SqlDataSource1"> <EditItemTemplate> tmp1: <asp:TextBox ID="tmp1TextBox" runat="server" Text='<%# Bind("tmp1") %>' /> <asp:Button ID="Button1" runat="server" Text="hello" /> <br /> <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="更新" /> &nbsp;<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="キャンセル" /> </EditItemTemplate> <InsertItemTemplate> </InsertItemTemplate> <ItemTemplate> tmp1: <asp:Label ID="tmp1Label" runat="server" Text='<%# Bind("tmp1") %>' /> <br /> <asp:Button ID="Button2" runat="server" CommandName="edit" Text="edit_Chage" /> </ItemTemplate> </asp:FormView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:testConnectionString %>" SelectCommand="SELECT [tmp1] FROM [tmpTBL]"> </asp:SqlDataSource> </form> </body> </html> /////////////////////////////// Default.aspx.vb Partial Class _Default Inherits System.Web.UI.Page Protected Sub FormView1_ModeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.ModeChanged '「Hello」ボタンへのjavascript組み込み If FormView1.CurrentMode = FormViewMode.Edit Then Dim btn As Button btn = CType(FormView1.FindControl("Button1"), Button) Dim sbScript As New System.Text.StringBuilder() sbScript.Append("alert('Hello')") btn.Attributes("onclick") = sbScript.ToString() End If End Sub End Class //////////////////////// また、 「Hello」ボタンのインスタンスを強制的に作ろうと、 Default.aspx.vbにFormView1.DataBind()を組み込みましたが、 インスタンスは生成されたものの、 onclickタグは生成されませんでした。 If FormView1.CurrentMode = FormViewMode.Edit Then Dim btn As Button FormView1.DataBind() <--- ここに追加 btn = CType(FormView1.FindControl("Button1"), Button) EditItemTemplateに配置したボタンにプログラムでjavascriptを組み込みたいのですが、 どなたか解決方法をご存知な方がいましたら、ご教示願いします。

  • yahoo API ASP.NETで表示させるには

    お世話になります 環境 Windowsxp pro asp.net vb ACCESS(テーブル) 超初心者 Yahoo APIですが下記を参考中に作成しています http://www.atmarkit.co.jp/fdotnet/dotnettips/477 aspx-------------------------------------------------------------- <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default6.aspx.vb" Inherits="loop_Default6" %> <!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>webAPI</title> </head> <body> <form id="form1" runat="server"> <div> <asp:TextBox ID="txtKeyword" runat="server"></asp:TextBox> <asp:Button ID="btnSearch" runat="server" OnClick="btnSearch_Click" Text="検索" /> <br /> <asp:BulletedList ID="list" runat="server" DataSourceID="Xml" DataTextField="Title" DataValueField="Url" DisplayMode="HyperLink"> </asp:BulletedList> <asp:XmlDataSource ID="Xml" runat="server" DataFile="http://search.yahooapis.jp/WebSearchService/V2/webSearch?appid=自分のID&query=apiとは?&results=20" TransformFile="~/App_Data/YahooSearch.xsl"></asp:XmlDataSource> <br /> </div> </form> </body> </html> aspx.vb Partial Class loop_Default6 Inherits System.Web.UI.Page Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click Xml.DataFile = _ "http://search.yahooapis.jp/WebSearchService/V2/webSearch?appid=自分のID&query=" _ & Server.UrlEncode(txtKeyword.Text) & "&results=50" End Sub End Class xsl--------------------------------------------- <?xml version="1.0" encoding="UTF-8" ?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:yahoo="urn:yahoo:jp:srch" version="1.0"> <xsl:template match="/yahoo:ResultSet"> <ResultSet> <xsl:for-each select="yahoo:Result"> <Result> <xsl:attribute name="Title"> <xsl:value-of select="yahoo:Title" /> </xsl:attribute> <xsl:attribute name="Url"> <xsl:value-of select="yahoo:Url" /> </xsl:attribute> </Result> </xsl:for-each> </ResultSet> </xsl:template> </xsl:stylesheet> -------------------------------------------- 現在は表示ぐらいまでならできました これをyahooオークションで転用したいのですが ASP.NETでググってもあまりHITしません 分かる方初心者ですがご教授願います

  • window.close()で閉じられない

    教えていただけますか? インターネット上のサンプルを参考に 押したらウインドウを閉じるボタンを作ったのですが、 うまく機能しません。 サーバーはレンタルサーバーです どの様にしたら良いでしょうか? インターネット上のサンプルでは閉じられました。 <!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> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>無題ドキュメント</title> </head> <body> <form> <INPUT type="button" value="閉じる" onClick="window.close()"> <input type="button" value="閉じる" onClick="close()"> </form> <A href="JavaScript:window.close()">閉じる</A> </body> </html>

  • UpdatePanel と GridViewについて教えてください

    現在、ASP.NET(VB)にて開発を行っています。今悩んでいる事は GridView内でUpdatePanelを使用した際にエラーが表示されます。 エラー内容は下記の通りです。 InvalidCastException はユーザーコードによってハンドルされませんでした。 ______________________________________________________________________ 型 'System.Web.UI.UpdatePanel' のオブジェクト型 'System.Web.UI.WebControl.GridViewRow' にキャストできません。 どなたか、エラーの対処方をご存知の方がいらっしゃいましたらご指導宜しくお願いします。なお、ソースは下記の様に記述してみました。 <%@ Page Language="VB" %> <%@ Import Namespace="System.Data.SqlClient" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> Protected Sub TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Dim txt As TextBox = DirectCast(sender, TextBox) Dim row As GridViewRow = DirectCast(txt.Parent.Parent, GridViewRow) CD.Text = txt.Text End Sub </script> <html> <body> <form id="form1" runat="server"> <asp:ScriptManager ID="SM" runat="server"> </asp:ScriptManager> <asp:TextBox ID="CD" runat="server" Width="50px"></asp:TextBox> <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" DataKeyNames="CD" DataSourceID="Source" ShowFooter="True"> <Columns> <asp:TemplateField HeaderText="CD"> <ItemTemplate> <asp:Label ID="lbl" runat="server" Text='<%# Bind("CD") %>'></asp:Label> </ItemTemplate> <FooterTemplate> <asp:UpdatePanel ID="UP" runat="server"> <ContentTemplate> <asp:TextBox ID="NewCD" runat="server" AutoPostBack="True" OnTextChanged="TextChanged" Width="50px"></asp:TextBox> </ContentTemplate> </asp:UpdatePanel> </FooterTemplate> </asp:TemplateField> </Columns> </asp:GridView> <asp:SqlDataSource ID="Source" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionStrings %>" SelectCommand="SELECT * FROM [TABLE]" ProviderName="System.Data.SqlClient"> </asp:SqlDataSource> </form> </body> </html>

  • DOCTYPE宣言とdocument.body.clientHeight

    お世話になります。 ブラウザのサイズを取得するにはどうすればよいのでしょうか? document.body.clientHeightで取得出来るのは分かりましたが DTDにより動作したり、しなくなるのも分かりました。 ×動作しない <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> ○動作する <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 後者のものですとIEでは動作しますが、firefoxでは動作しません。 何か対処法は、御座いませんでしょうか? 一番望む形は <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> の宣言で、ブラウザサイズが取れるようになるのがベストです。 CSSを使用しているので<?xml version="1.0" encoding="UTF-8"?>を入れると 表示モードが互換になるので・・・。 対応したいブラウザはIE6.0、firefox,Safariです。 一応ソース <?xml version="1.0" encoding="UTF-8"?> <!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> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script> function init(){ var bodyHeight = document.body.clientHeight; alert(bodyHeight); } </script> </head> <body onload="init();"> <div> ほげ </div> </body> </html>

  • phpのフォーム入力で

    phpの勉強初日なのですが、さっそくつまづいてしまいました。 買った本に沿って、簡単なフォーム入力のhtmlファイルと、 その処理のphpファイルを下記のように作ってみたのですが、上手く行きません。 htmlのフォームからのpostは出来ているようなのですが、 phpファイルがコールされると、phpファイルのソースがそのまま表示されてしまって、 postした文字列がechoされません。。。 初日でつまづいてしまっていきなり挫折しそうです。。。 どなたかお詳しい方、お教えいただけると幸いです。 ■input.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>input</title> </head> <body> <form action="inputphp.php" method="post"> <label for="my_name">お名前:</label> <input id="my_name" type="text" name="my_name" size="15" maxlengh="255" value=""/> <input type="submit" value="送信"/> </form> </body> </html> ■inputphp.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>inputphp.php</title> </head> <body> <?php print($_REQUEST['my_name']); ?> </body> </html>

    • ベストアンサー
    • PHP