Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Missing attributes in ListItems

Reply
Thread Tools

Missing attributes in ListItems

 
 
Robert Pouleijn
Guest
Posts: n/a
 
      07-22-2003
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...;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();

}

}

}



}



 
Reply With Quote
 
 
 
 
Lewis Wang [MSFT]
Guest
Posts: n/a
 
      07-24-2003
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/de...-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" <>
| 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: <>
| Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontro ls
| NNTP-Posting-Host: 217.166.59.130
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP12.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.aspnet.webcontro ls:13333
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontro ls
|
| 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...;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();
|
| }
|
| }
|
| }
|
|
|
| }
|
|
|
|

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DropDownList ListItems in a VB.NET Web Control Library Jeff ASP .Net 1 03-18-2005 11:28 PM
ListItems as checkbox in a listbox Vikram ASP .Net 1 03-01-2005 06:55 AM
Style DropDownList's ListItems Individually Doug ASP .Net 7 08-02-2004 01:34 PM
Change forecolor for individual listitems in CheckBoxList Eric Robishaw ASP .Net 1 12-27-2003 07:50 PM
ListItems are added in reverse order ASP .Net 1 10-28-2003 07:10 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57