R
Robert Pouleijn
Hi all,
I have found some code on the internet to avoid the following bug in ASP.Net
Dropdownlists and Listbox's:
http://support.microsoft.com/default.aspx?scid=kb;en-us;Q309338.
The code is a class that inherits from the Dropdownlist and overrides the
RenderContents subroutine. It works except for the fact that the ListItem
Attributes aren't stored in the viewestate. The first time the page is
rendered the attributes are rendered to the html output, but when the page
is posted back the attributes are missing.
My question:
Is there a possibility to somehow get the attributes in the viewstate?
Thanxs in advance!
This is the test page code:
Private m_Array() As String = {"Value1", "Value2", "Value3"}
Private Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
Dim oDrop As New CustomWebcontrols.DropdownList
oDrop.ID = "ID1"
PlaceHolder1.Controls.Add(oDrop)
oDrop.EnableViewState = True
oDrop.DataSource = m_Array
oDrop.DataBind()
oDrop.Items(1).Attributes.Add("Style", "COLOR:RED")
oDrop.Attributes.Add("style", "Z-INDEX: 101; LEFT: 50px;
POSITION: absolute; TOP: 200px")
Else
Dim oDrop As New CustomWebcontrols.DropdownList
oDrop.ID = "ID1"
PlaceHolder1.Controls.Add(oDrop)
End If
End Sub
This is the class file I found in C# that should avoid the bug:
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
namespace CustomWebcontrols
{
public class DropDownList :
System.Web.UI.WebControls.DropDownList
{
public DropDownList ()
{
//
// TODO: Add constructor logic here
//
}
override protected void RenderContents(HtmlTextWriter
writer)
{
for(int c=0;c<Items.Count;c++)
{
ListItem i = Items[c];
writer.WriteBeginTag("option");
if(i.Selected)
writer.WriteAttribute("selected","selected",false);
writer.WriteAttribute("value",i.Value,true);
IEnumerator d =
Items[c].Attributes.Keys.GetEnumerator();
while(d.MoveNext())
writer.WriteAttribute(d.Current.ToString(),Items[c].Attributes[d.Current.ToS
tring()]);
writer.Write('>');
System.Web.HttpUtility.HtmlEncode(i.Text,writer);
writer.WriteEndTag("option");
writer.WriteLine();
}
}
}
}
I have found some code on the internet to avoid the following bug in ASP.Net
Dropdownlists and Listbox's:
http://support.microsoft.com/default.aspx?scid=kb;en-us;Q309338.
The code is a class that inherits from the Dropdownlist and overrides the
RenderContents subroutine. It works except for the fact that the ListItem
Attributes aren't stored in the viewestate. The first time the page is
rendered the attributes are rendered to the html output, but when the page
is posted back the attributes are missing.
My question:
Is there a possibility to somehow get the attributes in the viewstate?
Thanxs in advance!
This is the test page code:
Private m_Array() As String = {"Value1", "Value2", "Value3"}
Private Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
Dim oDrop As New CustomWebcontrols.DropdownList
oDrop.ID = "ID1"
PlaceHolder1.Controls.Add(oDrop)
oDrop.EnableViewState = True
oDrop.DataSource = m_Array
oDrop.DataBind()
oDrop.Items(1).Attributes.Add("Style", "COLOR:RED")
oDrop.Attributes.Add("style", "Z-INDEX: 101; LEFT: 50px;
POSITION: absolute; TOP: 200px")
Else
Dim oDrop As New CustomWebcontrols.DropdownList
oDrop.ID = "ID1"
PlaceHolder1.Controls.Add(oDrop)
End If
End Sub
This is the class file I found in C# that should avoid the bug:
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
namespace CustomWebcontrols
{
public class DropDownList :
System.Web.UI.WebControls.DropDownList
{
public DropDownList ()
{
//
// TODO: Add constructor logic here
//
}
override protected void RenderContents(HtmlTextWriter
writer)
{
for(int c=0;c<Items.Count;c++)
{
ListItem i = Items[c];
writer.WriteBeginTag("option");
if(i.Selected)
writer.WriteAttribute("selected","selected",false);
writer.WriteAttribute("value",i.Value,true);
IEnumerator d =
Items[c].Attributes.Keys.GetEnumerator();
while(d.MoveNext())
writer.WriteAttribute(d.Current.ToString(),Items[c].Attributes[d.Current.ToS
tring()]);
writer.Write('>');
System.Web.HttpUtility.HtmlEncode(i.Text,writer);
writer.WriteEndTag("option");
writer.WriteLine();
}
}
}
}