###前提・実現したいこと
.NET Framework4.0で、独自の<ul>要素を出力する簡単なユーザーコントロールを作成したいです。
###該当のソースコード
言語:C# , html(html5) ※WebForm1.aspxファイルをブラウザ上で表示しました。 ※各ユーザーコントロールのascx.csファイルに関しては、テンプレートによって作成されたままの状態です。 1.Unordered.ascx <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Unordered.ascx.cs" Inherits="Problem.Unordered" %> <ul class="ul"> </ul> 2.MyListitem.ascx <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MyListitem.ascx.cs" Inherits="Problem.MyListitem" %> <li class="li"> <div class="first"> <asp:Label runat="server" ID="labelFirst"></asp:Label> </div> <div class="second"> <asp:Label runat="server" ID="labelSecond"></asp:Label> </div> </li> 3-1.WebForm1.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Problem.WebForm1" %> <%@ Register TagPrefix="uc" TagName="Unordered" Src="~/Unordered.ascx" %> <%@ Register TagPrefix="uc" TagName="MyListitem" Src="~/MyListitem.ascx" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" runat="server"> <div> <uc:Unordered runat="server" ID="Unordered" /> </div> </form> </body> </html> 3-2 WebForm1.aspx.cs using System; namespace Problem { public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { // <li>要素を挿入 Unordered.Controls.Add(new MyListitem()); } } }
###試したこと
WebForm1.aspx上でuc:Unordered要素を開始・終了タグに分け、中に直接uc:MyListitem要素を挿入してみましたが、「要素'Unordered'に対しては、開始タグと終了タグの間でコンテンツを使用できません。」といったエラーメッセージが表示されました。これをヒントに少し調べてみたのですが、クラスそのものを親要素として定義するような方法が分かりませんでした。また、それらしき基本クラスを発見することはできたのですが、UserControlクラスを継承しているため利用することができませんでした。出来ることならユーザーコントロールとして作成したいと考えております。出力されるhtmlをascxファイル上で編集できたり、サーバーコントロールを利用できたりする点が便利だからです。また、MyListitemクラスの<li>要素を<div>要素でラップし、その<div>要素をサーバーコントロール化してinnerHTMLプロパティを取得してみたり(もはやサーバーコントロールのコンセプトを逸脱していますが)しましたが、これも上手くいきませんでした。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/06/29 09:59