Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Missing attributes in ListItems

67 views
Skip to first unread message

Robert Pouleijn

unread,
Jul 22, 2003, 9:27:48 AM7/22/03
to
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();

}

}

}

}

Lewis Wang [MSFT]

unread,
Jul 24, 2003, 3:44:20 AM7/24/03
to
Hi Robert,

You must override the saveviewstat method and loadviewstat method of
DropDownlist to resolve this problem. Because the attributes of
DropDownlist'items are not saved in viewstat automatically. When you
postback, these attributes can not be loaded from viewstat.

The following code snippet demonstrates this idea. You may want to modify
the code to meet your actual requirement?

public class DropDownList :System.Web.UI.WebControls.DropDownList
{
public DropDownList ()
{
//
// TODO: Add constructor logic here
//
}

protected override object SaveViewState()
{
// Change Text Property of Label when this function is
invoked.
// Save State as a cumulative array of objects.
object baseState = base.SaveViewState();
object[] allStates = new object[this.Items.Count+1];
allStates[0] = baseState;

for(int c=0;c<Items.Count;c++)
{

int AttrisCount=Items[c].Attributes.Count;
//Attri[] Attris= new Attri[AttrisCount];
object[] Attris=new object [AttrisCount*2];
int i=0;

IEnumerator d =
Items[c].Attributes.Keys.GetEnumerator();
while(d.MoveNext())

{
Attris[i++] =d.Current.ToString();
Attris[i++]
=Items[c].Attributes[d.Current.ToString()];
}
allStates[c+1] = Attris;
}
return allStates;
}

protected override void LoadViewState(object savedState)
{
if (savedState != null)
{
// Load State from the array of objects that was
saved at ;
// SavedViewState.
object[] myState = (object[])savedState;
if (myState[0] != null)
base.LoadViewState(myState[0]);

for(int c=1;c<myState.Length;c++)
{
int
AttrisCount=((object[])myState[c]).Length;
for(int n=0;n<(AttrisCount/2);n++)
{
object[] o= (object[])myState[c];
this.Items[c-1].Attributes.Add(
o[n].ToString (),o[n+1].ToString ());

}
}
}
}
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();
}
}
}

Please check the link for more information:
Control.SaveViewState Method
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html
/frlrfsystemwebuicontrolclasssaveviewstatetopic.asp>

Hope this helps.

Best Regards,
Lewis Wang
Support Professional

This posting is provided "AS IS" with no warranties, and confers no rights.


--------------------
| From: "Robert Pouleijn" <rpou...@megacom.nl>
| Subject: Missing attributes in ListItems
| Date: Tue, 22 Jul 2003 15:27:48 +0200
| Lines: 161
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1106
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
| Message-ID: <O7hffUFU...@TK2MSFTNGP12.phx.gbl>
| Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| NNTP-Posting-Host: 217.166.59.130
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.aspnet.webcontrols:13333
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols

0 new messages