- - PR -
DropDownListコントロールの子コントロールにスタイルを適用するには?
1
投稿者 | 投稿内容 |
---|---|
|
投稿日時: 2003-12-03 16:12
はじめまして。子コントロールへのスタイル適用方法がみつからなかったので
質問いたします。 以下の例のように、DropDownListコントロールにて、メニューの項目ごとにス タイル(ここでは背景色)を適用したいのですが、Webサーバーコントロールで はプロパティが設けられていなければ不可能なのでしょうか? Webサーバコントロールを使って、ブラウザ上では、 <option value="sample1.asp" style="background-color:red">Sample1 のように出力されるようにしたいのですが。。。。 --------------例--------------------------------------- <%@ Page Language="C#" Trace="false" Debug="true" %> <%@ Import namespace="System.Data" %> <script runat="server"> void Page_Load(Object sender, EventArgs e) { DataTable dt = new DataTable("test"); dt.Columns.Add(new DataColumn("Text", System.Type.GetType("System.String"))); dt.Columns.Add(new DataColumn("Value", System.Type.GetType("System.String"))); DataRow dr; dr=dt.NewRow(); dr["Text"]="Sample1"; dr["Value"]="sample1.asp"; //dr["Style"]="background-color:red"; dt.Rows.Add(dr); dr=dt.NewRow(); dr["Text"]="Sample2"; dr["Value"]="sample2.asp"; //dr["Style"]="background-color:blue"; dt.Rows.Add(dr); scListItem.DataSource = dt; scListItem.DataTextField="text"; scListItem.DataValueField = "value"; scListItem.DataBind(); } </script> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"> <html> <head> <meta http-equiv="Content-type" content="text/html; charset=Shift_JIS"> <meta http-equiv="Pragma" content="no-cache"> <title>DropDownListの子コントロールにスタイル適用テスト</title> </head> <body class="wdBaseBody"> <form runat="server"> <asp:DropDownList id="scListItem" runat="server" /> <asp:DropDownList runat="server"> <asp:ListItem Value="sample1.asp" style="background-color:red">Sample1</asp:ListItem> <asp:ListItem Value="sample2.asp" style="background-color:blue">Sample2</asp:ListItem> </asp:DropDownList> <select name="test"> <option Value="sample1.asp" style="background-color:red">Sample1</option> <option Value="sample2.asp" style="background-color:blue">Sample2</option> </select> </form> </body> </html> |
|
投稿日時: 2003-12-04 11:40
似たような質問をしている方がいましたので、URLを掲載しておきます。
http://www.dotnet247.com/247reference/msgs/1/9993.aspx |
|
投稿日時: 2004-01-25 19:48
私もうまくいかず探してみました。
バグのようです。以下に情報があります。 http://www.dotnet247.com/247reference/msgs/13/68381.aspx なぜか上記記載のMSのバグナンバーは該当なしになってしまいます。 上記からの取り出しですが、下記のようなコードを使うとうまくいきました。 public class ColorDropDownList : DropDownList { override protected void RenderContents(HtmlTextWriter writer) { for(int c=0;c<Items.Count;c++) { ListItem li = Items[c]; writer.WriteBeginTag("option"); if(li.Selected) writer.WriteAttribute("selected", "selected", false); writer.WriteAttribute("value", li.Value, true); IEnumerator d = Items[c].Attributes.Keys.GetEnumerator(); while(d.MoveNext()) writer.WriteAttribute(d.Current.ToString(),Items[c].Attributes[d.Current.ToString()]); writer.Write('>'); System.Web.HttpUtility.HtmlEncode(li.Text,writer); writer.WriteEndTag("option"); writer.WriteLine(); } } } こんな風に使って <custom:colordropdownlist id="Color" runat="server"></custom:colordropdownlist> ViewStateの処理はしていないので、毎回セットします。 private void Page_Load(object sender, System.EventArgs e) { // ページを初期化するユーザー コードをここに挿入します。 if(!this.IsPostBack) { .... } for (int i= 0 ;i < Color.Items.Count;i++) Color.Items[i].Attributes.Add("style", "background-color:" + Color.Items[i].Text); } |
1