Missing attributes when postback Dropdownlist

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

}



}
}
 
C

Cor Ligthert [MVP]

LG,

I am dealing with the same problem at the moment, you are not using ASP
controls you are writing a string to your clients IE. This string is not
rendered by ASPNet and therefore nothing happens, if you want to use the
possibilities of the ASPNET controls (or changed runat server controls) than
you have to do your work using the ASPX page.

TextBox1.Attributes("onblur") = "javascript:alert('Hello! Focus lost from
text box!!');"

I hope this helps,

Cor


LG said:
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);

}



}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top