|
Did anyone find a possible solution for mixing ungrouped items?
|
|
|
|
|
I found the issue and fixed that. Below is the fix (in VB.NET)
Protected Overloads Overrides Sub RenderContents(ByVal writer As HtmlTextWriter)
Dim list As DropDownList = TryCast(Me.Control, DropDownList)
Dim currentOptionGroup As String
Dim renderedOptionGroups As New List(Of String)()
Dim tagClosed As Boolean = True
For Each item As ListItem In list.Items
If item.Attributes("OptionGroup") Is Nothing OrElse item.Attributes("OptionGroup") = "" Then
If Not tagClosed Then RenderOptionGroupEndTag(writer) : tagClosed = True
RenderListItem(item, writer)
Else
currentOptionGroup = item.Attributes("OptionGroup")
If renderedOptionGroups.Contains(currentOptionGroup) Then
RenderListItem(item, writer)
Else
If tagClosed = False Then
RenderOptionGroupEndTag(writer)
tagClosed = True
End If
RenderOptionGroupBeginTag(currentOptionGroup, writer)
renderedOptionGroups.Add(currentOptionGroup)
RenderListItem(item, writer)
tagClosed = False
End If
End If
Next
If tagClosed = False Then
RenderOptionGroupEndTag(writer)
End If
End Sub
I hope that will help.
Sameers
|
|
|
|
|
I am using this adapter for listbox. Everything works fine except that optgroup structure disappears after PostBack. When I used following two functions for saving and loading viewstate it gives me error CS0115: 'ListAdapter.ListBoxAdapter.SaveViewState()': no suitable method found to override
The code I added for saving and loading viewstate is:
protected override object SaveViewState()
{
// Create an object array with one element for the CheckBoxList's
// ViewState contents, and one element for each ListItem in skmCheckBoxList
object[] state = new object[this.Items.Count + 1];
object baseState = base.SaveViewState();
state[0] = baseState;
// Now, see if we even need to save the view state
bool itemHasAttributes = false;
for (int i = 0; i < this.Items.Count; i++)
{
if (this.Items[i].Attributes.Count > 0)
{
itemHasAttributes = true;
// Create an array of the item's Attribute's keys and values
object[] attribKV = new object[this.Items[i].Attributes.Count * 2];
int k = 0;
foreach (string key in this.Items[i].Attributes.Keys)
{
attribKV[k++] = key;
attribKV[k++] = this.Items[i].Attributes[key];
}
state[i + 1] = attribKV;
}
}
// return either baseState or state, depending on whether or not
// any ListItems had attributes
if (itemHasAttributes)
return state;
else
return baseState;
}
protected override void LoadViewState(object savedState)
{
if (savedState == null) return;
// see if savedState is an object or object array
if (savedState is object[])
{
// we have an array of items with attributes
object[] state = (object[])savedState;
base.LoadViewState(state[0]); // load the base state
for (int i = 1; i < state.Length; i++)
{
if (state[i] != null)
{
// Load back in the attributes
object[] attribKV = (object[])state[i];
for (int k = 0; k < attribKV.Length; k += 2)
this.Items[i - 1].Attributes.Add(attribKV[k].ToString(), attribKV[k + 1].ToString());
}
}
}
else
// we have just the base state
base.LoadViewState(savedState);
}
Is there any complete solution for this?
|
|
|
|
|
First I have to thank the author for this post. It has helped me a lot in getting this to work. I thought I'd make a contribution to this code to make PastBacks work correctly.
public class DropDownListAdapter : System.Web.UI.WebControls.Adapters.WebControlAdapter {
public static readonly string OPTGROUPATTR = "OptionGroup";
Dictionary<string, string> optionGroups = new Dictionary<string, string>();
protected override void RenderContents(HtmlTextWriter writer) {
DropDownList list = this.Control as DropDownList;
string currentOptionGroup;
List<string> renderedOptionGroups = new List<string>();
foreach(ListItem item in list.Items) {
string keyString = item.Value + item.Text;
if(item.Attributes[OPTGROUPATTR] == null && !optionGroups.ContainsKey(keyString)) {
RenderListItem(item, writer);
} else {
if (item.Attributes[OPTGROUPATTR] != null)
currentOptionGroup = item.Attributes[OPTGROUPATTR];
else
{
currentOptionGroup = optionGroups[keyString];
}
if(renderedOptionGroups.Contains(currentOptionGroup)) {
RenderListItem(item, writer);
} else {
if(renderedOptionGroups.Count > 0) {
RenderOptionGroupEndTag(writer);
}
RenderOptionGroupBeginTag(currentOptionGroup, writer);
renderedOptionGroups.Add(currentOptionGroup);
RenderListItem(item, writer);
}
}
}
if(renderedOptionGroups.Count > 0) {
RenderOptionGroupEndTag(writer);
}
}
private void RenderOptionGroupBeginTag(string name, HtmlTextWriter writer) {
writer.WriteBeginTag("optgroup");
writer.WriteAttribute("label", name);
writer.Write(HtmlTextWriter.TagRightChar);
writer.WriteLine();
}
private void RenderOptionGroupEndTag(HtmlTextWriter writer) {
writer.WriteEndTag("optgroup");
writer.WriteLine();
}
private void RenderListItem(ListItem item, HtmlTextWriter writer) {
this.Page.ClientScript.RegisterForEventValidation(this.Control.UniqueID, item.Value);
writer.WriteBeginTag("option");
writer.WriteAttribute("value", item.Value, true);
if(item.Selected) {
writer.WriteAttribute("selected", "selected", false);
}
foreach(string key in item.Attributes.Keys) {
writer.WriteAttribute(key, item.Attributes[key]);
}
writer.Write(HtmlTextWriter.TagRightChar);
HttpUtility.HtmlEncode(item.Text, writer);
writer.WriteEndTag("option");
writer.WriteLine();
}
protected override object SaveAdapterViewState()
{
DropDownList list = this.Control as DropDownList;
string currentOptionGroup;
string keyString;
foreach (ListItem item in list.Items)
{
if (item.Attributes[OPTGROUPATTR] != null)
{
currentOptionGroup = item.Attributes[OPTGROUPATTR];
keyString = item.Value + item.Text;
optionGroups[keyString] = currentOptionGroup;
}
}
return optionGroups;
}
protected override void LoadAdapterViewState(object state)
{
optionGroups = state as Dictionary<string, string>;
}
}
|
|
|
|
|
Thanks Lamester,
This was helpful..
|
|
|
|
|
I've found both the original post and this update very helpful.
Both scripts fail in two areas though:
1. Where you want options outside the groups - they get incorporated into the last group as it's not yet closed.
2. If you want to have a repeated ListItem in separate groups. This happens, in my case, where I have an initial "shortlist" group then all possible options. Because the keys are based on value and text, the key is repeated and only one group is pulled for both options.
To fix the first problem I just added a bool (blGroupOpen) to remember if a group is open and close it before writing an ungrouped item.
The second problem is more complex. I've updated the keyString to add an asterisk (*) if the given keyString is already present. I then use a List of strings (renderedOptionGroupsClosed) to keep track of groups as they are rendered as closed. If a Value+Text keyString has already been closed, it looks for the next asterisked keyString for the next option group.
Here are the pieces of code:
In RenderContents:
<br />
if (item.Attributes[OPTGROUPATTR] == null && !optionGroups.ContainsKey(keyString))<br />
{<br />
if (blGroupOpen)<br />
{<br />
RenderOptionGroupEndTag(writer);<br />
blGroupOpen = false;<br />
renderedOptionGroupsClosed.Add(currentOptionGroup);<br />
}<br />
RenderListItem(item, writer);<br />
}<br />
else<br />
{<br />
if (item.Attributes[OPTGROUPATTR] != null)<br />
currentOptionGroup = item.Attributes[OPTGROUPATTR];<br />
else<br />
{<br />
while (renderedOptionGroupsClosed.Contains(optionGroups[keyString]))<br />
keyString += "*";<br />
currentOptionGroup = optionGroups[keyString];<br />
}<br />
.<br />
.<br />
.<br />
RenderOptionGroupBeginTag(currentOptionGroup, writer);<br />
blGroupOpen = true;<br />
.<br />
.<br />
.<br />
if (blGroupOpen)<br />
{<br />
RenderOptionGroupEndTag(writer);<br />
renderedOptionGroupsClosed.Add(currentOptionGroup);<br />
}<br />
Then to set the state with correct keyStrings:
<br />
protected override object SaveAdapterViewState()<br />
{<br />
DropDownList list = this.Control as DropDownList;<br />
foreach (ListItem item in list.Items)<br />
{<br />
if (item.Attributes[OPTGROUPATTR] != null)<br />
{<br />
string currentOptionGroup = item.Attributes[OPTGROUPATTR];<br />
string keyString = item.Value + item.Text;<br />
while (optionGroups.ContainsKey(keyString))<br />
keyString += "*";<br />
optionGroups[keyString] = currentOptionGroup;<br />
}<br />
}<br />
return optionGroups;<br />
}<br />
Hope this helps.
|
|
|
|
|
|
I want to know how do I bind this drop down list to get data from sql server
|
|
|
|
|
"OnSelectedIndexChanged" is not working.Can any one suggest what changes i have to do in my code to use this event of dropdown list.
|
|
|
|
|
Hi any one knows what gone wrong to the selected index changed event??
|
|
|
|
|
Hi
trying to use the "OnSelectedIndexChanged" event but it is not working.Can any one suggest what changes i have to do in my code to use this event of dropdown list.
|
|
|
|
|
Hi RashuD any solution for this post...?
|
|
|
|
|
Hi
If you got any solution for the event problem. Pls forward it to me.
siva@touchpointindia.com
Thanks
|
|
|
|
|
You must add on AdapterClass DropDownListAdapter.cs this to overrides :
protected override object SaveAdapterViewState()
{
DropDownList list = this.Control as DropDownList;
string currentOptionGroup;
string keyString;
foreach (ListItem item in list.Items)
{
if (item.Attributes[OPTGROUPATTR] != null)
{
currentOptionGroup = item.Attributes[OPTGROUPATTR];
keyString = item.Value + item.Text;
optionGroups[keyString] = currentOptionGroup;
}
}
return optionGroups;
}
protected override void LoadAdapterViewState(object state)
{
optionGroups = state as Dictionary<string, string>;
}
|
|
|
|
|
Is it possible to make more than one level item into the dropdown list? And user will be able to select any item from the list.
eg.,
Item1
--Item11
----Item111
----Item112
--Item12
----Item121
----Item122
--Item13
Item2
--Item21
--Item22
Item3
Item4
|
|
|
|
|
hi,
i must say that this control is excellent - i m only facing problem in changing font style to normal (not italic) of 'optgroup' element.
i.e. the OptionGroup like 'Mammals' is in italic .. i want it to change to Normal style.
please help
regards
asif
|
|
|
|
|
I've tried several ideas to get around this but it seems the default browser behaviour in IE cannot be overidden. In theory, the following CSS should do it:
select optgroup {<br />
font-style: normal;<br />
}
But this only seems to work in FireFox.
|
|
|
|
|
optgroup{font-style:normal}
is not IE compatible what will be the compatible code
|
|
|
|
|
The style can be overridden in IE7+; for IE6 and under it will always be presented in bold italic.
|
|
|
|
|
Hi,
Following my previous post, since there is no known solution for the Viewstate issue, i decided to use the list as a control.
I did find out do that the items have to be added in grouping order, this cause a problem to me.
So below is a fix so you can add items in any order, note that items without a group will always be added on top.
<br />
[DefaultValue(true)]<br />
public bool SortItems<br />
{<br />
get<br />
{<br />
return ViewState["SortItems"] == null ? true : (bool)ViewState["SortItems"];<br />
}<br />
set<br />
{<br />
ViewState["SortItems"] = value;<br />
}<br />
}<br />
protected override void RenderContents(HtmlTextWriter writer)<br />
{<br />
SortedDictionary<string, List<ListItem>> sortedItems = new SortedDictionary<string, List<ListItem>>();<br />
<br />
foreach (ListItem item in this.Items)<br />
{<br />
if (item.Attributes[OptionGroupAttribute] == null)<br />
{<br />
if (sortedItems.ContainsKey("_"))<br />
{<br />
sortedItems["_"].Add(item);<br />
}<br />
else<br />
{<br />
sortedItems.Add("_", new List<ListItem>());<br />
sortedItems["_"].Add(item);<br />
}<br />
}<br />
else<br />
{<br />
string optionGroup = item.Attributes[OptionGroupAttribute];<br />
<br />
if (sortedItems.ContainsKey(optionGroup))<br />
{<br />
sortedItems[optionGroup].Add(item);<br />
}<br />
else<br />
{<br />
sortedItems.Add(optionGroup, new List<ListItem>());<br />
sortedItems[optionGroup].Add(item);<br />
}<br />
}<br />
}<br />
<br />
foreach (KeyValuePair<string, List<ListItem>> entry in sortedItems)<br />
{<br />
if (SortItems)<br />
{<br />
entry.Value.Sort(delegate(ListItem item1, ListItem item2)<br />
{<br />
return item1.Text.CompareTo(item2.Text);<br />
});<br />
}<br />
<br />
if (entry.Key != "_")<br />
RenderOptionGroupBeginTag(entry.Key, writer);<br />
<br />
foreach (ListItem item in entry.Value)<br />
{<br />
Page.ClientScript.RegisterForEventValidation(this.UniqueID, item.Value);<br />
<br />
RenderListItem(item, writer);<br />
}<br />
<br />
if (entry.Key != "_")<br />
RenderOptionGroupEndTag(writer);<br />
}<br />
}<br />
And also here is a fix for the viewstate issues:
<br />
protected override object SaveViewState()<br />
{<br />
this.ViewState["-1Saved"] = (this.SelectedIndex == -1);<br />
<br />
for (int i = 0; i < this.Items.Count; i++)<br />
{<br />
if (this.Items[i].Attributes[OptionGroupAttribute] != null)<br />
this.ViewState["ListItemGroup" + i] = this.Items[i].Attributes[OptionGroupAttribute];<br />
}<br />
return base.SaveViewState();<br />
}<br />
<br />
<br />
protected override void LoadViewState(object state)<br />
{<br />
<br />
base.LoadViewState(state);<br />
<br />
for (int i = 0; i < this.Items.Count; i++)<br />
{<br />
if (this.ViewState["ListItemGroup" + i] != null)<br />
this.Items[i].Attributes[OptionGroupAttribute] = (string)this.ViewState["ListItemGroup" + i];<br />
}<br />
<br />
if (this.ViewState["-1Saved"] != null && (Boolean)this.ViewState["-1Saved"])<br />
{<br />
this.SelectedIndex = -1;<br />
}<br />
}<br />
|
|
|
|
|
I used Albert Weinert's code below to show my optgroups, everything works fine until the page postbacks. The list gets added again making duplicates and each time there is a post back the list grows. What could be wrong?
|
|
|
|
|
Please see my fixes on a post above!
The list is being stored in viewstate, you shouldn't have to re-populate after every postback:
For example:
<br />
protected void Page_Load(object sender, EventArgs e)<br />
{<br />
if (!IsPostBack)<br />
{<br />
PopulateListWithValues();<br />
}<br />
}<br />
|
|
|
|
|
For anyone in need, here is a VB version of the class:
For anyone in need for a VB version:
Public Class DropDownListAdapter
Inherits System.Web.UI.WebControls.Adapters.WebControlAdapter
Protected Overrides Sub RenderContents(ByVal writer As HtmlTextWriter)
Dim list As DropDownList = Me.Control
Dim currentOptionGroup As String
Dim renderedOptionGroups As New ArrayList
Dim item As ListItem
For Each item In list.Items
If item.Attributes("OptionGroup") Is Nothing Then
RenderListItem(item, writer)
Else
currentOptionGroup = item.Attributes("OptionGroup")
If renderedOptionGroups.Contains(currentOptionGroup) Then
RenderListItem(item, writer)
Else
If renderedOptionGroups.Count > 0 Then
RenderOptionGroupEndTag(writer)
End If
RenderOptionGroupBeginTag(currentOptionGroup, writer)
renderedOptionGroups.Add(currentOptionGroup)
RenderListItem(item, writer)
End If
End If
Next
If renderedOptionGroups.Count > 0 Then
RenderOptionGroupEndTag(writer)
End If
End Sub
Private Sub RenderOptionGroupBeginTag(ByVal name As String, ByVal writer As HtmlTextWriter)
writer.WriteBeginTag("optgroup")
writer.WriteAttribute("label", name)
writer.Write(HtmlTextWriter.TagRightChar)
writer.WriteLine()
End Sub
Private Sub RenderOptionGroupEndTag(ByVal writer As HtmlTextWriter)
writer.WriteEndTag("optgroup")
writer.WriteLine()
End Sub
Private Sub RenderListItem(ByVal item As ListItem, ByVal writer As HtmlTextWriter)
writer.WriteBeginTag("option")
writer.WriteAttribute("value", item.Value, True)
If (item.Selected) Then
writer.WriteAttribute("selected", "selected", False)
End If
Dim key As String
For Each key In item.Attributes.Keys
writer.WriteAttribute(key, item.Attributes(key))
Next key
writer.Write(HtmlTextWriter.TagRightChar)
HttpUtility.HtmlEncode(item.Text, writer)
writer.WriteEndTag("option")
writer.WriteLine()
End Sub
End Class
Jaywon
|
|
|
|
|
...and a couple of other minor alterations:
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Collections
Imports System.Collections.Generic
Public Class DropDownListAdapter
Inherits System.Web.UI.WebControls.Adapters.WebControlAdapter
Private Const m_optionGroupAttribute As String = "OptionGroup"
Private Const m_tagOptionGroup As String = "optgroup"
Private Const m_attributeLabel As String = "label"
Private m_viewstates As Object()
Protected Overrides Sub RenderContents(ByVal writer As HtmlTextWriter)
If (Not Page Is Nothing) Then
Dim l_list As DropDownList = CType(Me.Control, DropDownList)
Dim l_currentOptionGroup As String
Dim l_renderedOptionGroups As New ArrayList()
For Each l_item As ListItem In l_list.Items
Page.ClientScript.RegisterForEventValidation(l_list.UniqueID, l_item.Value)
If l_item.Attributes(m_optionGroupAttribute) Is Nothing Then
RenderListItem(l_item, writer)
Else
l_currentOptionGroup = l_item.Attributes(m_optionGroupAttribute)
If (l_renderedOptionGroups.Contains(l_currentOptionGroup)) Then
RenderListItem(l_item, writer)
Else
If (l_renderedOptionGroups.Count > 0) Then
RenderOptionGroupEndTag(writer)
End If
RenderOptionGroupBeginTag(l_currentOptionGroup, writer)
l_renderedOptionGroups.Add(l_currentOptionGroup)
RenderListItem(l_item, writer)
End If
End If
Next
If (l_renderedOptionGroups.Count > 0) Then
RenderOptionGroupEndTag(writer)
End If
Else
MyBase.RenderContents(writer)
End If
End Sub
Private Sub RenderOptionGroupBeginTag(ByVal name As String, ByVal writer As HtmlTextWriter)
writer.AddAttribute(m_attributeLabel, name)
writer.RenderBeginTag(m_tagOptionGroup)
End Sub
Private Sub RenderOptionGroupEndTag(ByVal writer As HtmlTextWriter)
writer.RenderEndTag()
End Sub
Private Sub RenderListItem(ByVal item As ListItem, ByVal writer As HtmlTextWriter)
For Each key As String In item.Attributes.Keys
If (key <> m_optionGroupAttribute) Then
writer.AddAttribute(key, item.Attributes(key))
End If
Next
writer.AddAttribute(HtmlTextWriterAttribute.Value, item.Value, True)
If (item.Selected) Then
writer.AddAttribute(HtmlTextWriterAttribute.Selected, "selected")
End If
writer.RenderBeginTag(HtmlTextWriterTag.Option)
writer.WriteEncodedText(item.Text)
writer.RenderEndTag()
End Sub
Protected Overrides Function SaveAdapterViewState() As Object
If (Not Page Is Nothing) Then
Dim l_list As DropDownList = CType(Control, DropDownList)
Dim l_viewState(l_list.Items.Count + 1) As Object
Dim i As Integer = 0
For Each item As ListItem In l_list.Items
l_viewState(i) = item.Attributes(m_optionGroupAttribute)
i += 1
Next
l_viewState(i) = MyBase.SaveAdapterViewState()
Return l_viewState
Else
Return MyBase.SaveAdapterControlState()
End If
End Function
Protected Overrides Sub LoadAdapterViewState(ByVal state As Object)
If (Not Page Is Nothing) Then
m_viewstates = CType(state, Object())
MyBase.LoadAdapterViewState(m_viewstates(m_viewstates.Length - 1))
Else
MyBase.LoadAdapterViewState(state)
End If
End Sub
Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
If (Not Page Is Nothing) Then
If (Not m_viewstates Is Nothing AndAlso m_viewstates.Length > 1) Then
Dim l_list As DropDownList = CType(Control, DropDownList)
If (Page.EnableEventValidation) Then
If (m_viewstates.Length <> l_list.Items.Count + 1) Then
Throw New ViewStateException()
End If
End If
Dim l_max As Integer = m_viewstates.Length
If (l_list.Items.Count < l_max) Then
l_max = l_list.Items.Count
End If
For i As Integer = 0 To l_max - 1
l_list.Items(i).Attributes(m_optionGroupAttribute) = CStr(m_viewstates(i))
Next
End If
End If
MyBase.OnPreRender(e)
End Sub
End Class
Marc
|
|
|
|
|
I modified the code above to make a standalone version.
Now you can add this class to a Web Control Library to use it as a web control.
Here's the code (enjoy):
Imports System.Web.UI
Imports System.Web.UI.WebControls
Public Class ComboBox
Inherits DropDownList
Private Const m_optionGroupAttribute As String = "OptionGroup"
Private Const m_tagOptionGroup As String = "optgroup"
Private Const m_attributeLabel As String = "label"
Private m_viewstates As Object()
Protected Overrides Sub RenderContents(ByVal writer As HtmlTextWriter)
If (Not Page Is Nothing) Then
Dim l_list As DropDownList = CType(Me, DropDownList)
Dim l_currentOptionGroup As String
Dim l_renderedOptionGroups As New ArrayList()
For Each l_item As ListItem In l_list.Items
Page.ClientScript.RegisterForEventValidation(l_list.UniqueID, l_item.Value)
If l_item.Attributes(m_optionGroupAttribute) Is Nothing Then
RenderListItem(l_item, writer)
Else
l_currentOptionGroup = l_item.Attributes(m_optionGroupAttribute)
If (l_renderedOptionGroups.Contains(l_currentOptionGroup)) Then
RenderListItem(l_item, writer)
Else
If (l_renderedOptionGroups.Count > 0) Then
RenderOptionGroupEndTag(writer)
End If
RenderOptionGroupBeginTag(l_currentOptionGroup, writer)
l_renderedOptionGroups.Add(l_currentOptionGroup)
RenderListItem(l_item, writer)
End If
End If
Next
If (l_renderedOptionGroups.Count > 0) Then
RenderOptionGroupEndTag(writer)
End If
Else
MyBase.RenderContents(writer)
End If
End Sub
Private Sub RenderOptionGroupBeginTag(ByVal name As String, ByVal writer As HtmlTextWriter)
writer.AddAttribute(m_attributeLabel, name)
writer.RenderBeginTag(m_tagOptionGroup)
End Sub
Private Sub RenderOptionGroupEndTag(ByVal writer As HtmlTextWriter)
writer.RenderEndTag()
End Sub
Private Sub RenderListItem(ByVal item As ListItem, ByVal writer As HtmlTextWriter)
For Each key As String In item.Attributes.Keys
If (key <> m_optionGroupAttribute) Then
writer.AddAttribute(key, item.Attributes(key))
End If
Next
writer.AddAttribute(HtmlTextWriterAttribute.Value, item.Value, True)
If (item.Selected) Then
writer.AddAttribute(HtmlTextWriterAttribute.Selected, "selected")
End If
writer.RenderBeginTag(HtmlTextWriterTag.Option)
writer.WriteEncodedText(item.Text)
writer.RenderEndTag()
End Sub
Protected Overrides Function SaveViewState() As Object
If (Not Page Is Nothing) Then
Dim l_list As DropDownList = CType(Me, DropDownList)
Dim l_viewState(l_list.Items.Count) As Object
Dim i As Integer = 0
For Each item As ListItem In l_list.Items
l_viewState(i) = item.Attributes(m_optionGroupAttribute)
i += 1
Next
l_viewState(i) = MyBase.SaveViewState()
Return l_viewState
Else
Return MyBase.SaveViewState()
End If
End Function
Protected Overrides Sub LoadViewState(ByVal state As Object)
If (Not Page Is Nothing) Then
m_viewstates = CType(state, Object())
MyBase.LoadViewState(m_viewstates(m_viewstates.Length - 1))
Else
MyBase.LoadViewState(state)
End If
End Sub
Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
If (Not Page Is Nothing) Then
If (Not m_viewstates Is Nothing AndAlso m_viewstates.Length > 1) Then
Dim l_list As DropDownList = CType(Me, DropDownList)
If (Page.EnableEventValidation) Then
If (m_viewstates.Length <> l_list.Items.Count + 1) Then
Throw New ViewStateException()
End If
End If
Dim l_max As Integer = m_viewstates.Length
If (l_list.Items.Count < l_max) Then
l_max = l_list.Items.Count
End If
For i As Integer = 0 To l_max - 1
l_list.Items(i).Attributes(m_optionGroupAttribute) = CStr(m_viewstates(i))
Next
End If
End If
MyBase.OnPreRender(e)
End Sub
End Class
Note that I modified something in the SaveViewState function.
-- modified at 13:22 Thursday 4th October, 2007
|
|
|
|
|