L
LG
I want to add attributes to a custom dropdownlist and I found some code
on Internet that doesn't work after post back.
First time the control has the attributes, but after postback the
attributes are lost.
It must be a bug in the code bellow. Can anyone help?
Thanks,
LG
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Collections;
namespace PD.Web.UI.WebControls
{
/// <summary>
/// Summary description for WebCustomControl.
/// </summary>
public class PDDropDownList : System.Web.UI.WebControls.DropDownList
{
/// <summary>
/// Render this control to the output parameter specified.
/// </summary>
/// <param name="output"> The HTML writer to write out to </param>
protected override 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.ToString()]);
writer.Write('>');
System.Web.HttpUtility.HtmlEncode(i.Text,writer);
writer.WriteEndTag("option");
writer.WriteLine();
}
}
protected override object SaveViewState()
{
//Saves the ViewState of the dropdownlist
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;
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;
// return base.SaveViewState();
}
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());
}
}
}
// base.LoadViewState(savedState);
}
}
}
on Internet that doesn't work after post back.
First time the control has the attributes, but after postback the
attributes are lost.
It must be a bug in the code bellow. Can anyone help?
Thanks,
LG
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Collections;
namespace PD.Web.UI.WebControls
{
/// <summary>
/// Summary description for WebCustomControl.
/// </summary>
public class PDDropDownList : System.Web.UI.WebControls.DropDownList
{
/// <summary>
/// Render this control to the output parameter specified.
/// </summary>
/// <param name="output"> The HTML writer to write out to </param>
protected override 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.ToString()]);
writer.Write('>');
System.Web.HttpUtility.HtmlEncode(i.Text,writer);
writer.WriteEndTag("option");
writer.WriteLine();
}
}
protected override object SaveViewState()
{
//Saves the ViewState of the dropdownlist
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;
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;
// return base.SaveViewState();
}
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());
}
}
}
// base.LoadViewState(savedState);
}
}
}