 |
|
 |
Helo All,
I am using ASP.NET 3.5. I have a form with a DropDownList contain the User Code. At Selected Index Change the form shows the respecting data. But at times it shows the data but the following error Message is generate.
"Microsoft JScript runtime error: 'this.get_element().style' is null or not an object"
I am using master Page. There is an update panel in the master page.
How can i solve not to generate this error message when selected index change at DropDownList.
Thanks in advance
|
|
|
|
 |
|
 |
The original example loses the listitem.value's when sorted.
Here's a version that retains them...
Public Shared Sub SortDropDownList(ByVal ddl As DropDownList)
Dim arl As ArrayList = Nothing
If (ddl.Items.Count > 0) Then
arl = New ArrayList(ddl.Items.Count)
For Each li As ListItem In ddl.Items
arl.Add(li)
Next
End If
arl.Sort(New e4LCMS.listfunctions.ListItemComparer)
ddl.Items.Clear()
For Each l As ListItem In arl
ddl.Items.Add(l)
Next
End Sub
Private Class ListItemComparer
Implements IComparer
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
Dim li_x As ListItem = CType(x, ListItem)
Dim li_y As ListItem = CType(y, ListItem)
Dim c As CaseInsensitiveComparer = New CaseInsensitiveComparer
Return c.Compare(li_x.Text, li_y.Text)
End Function
End Class
|
|
|
|
 |
|
 |
I put your code to use in a User Control, then registered the control in the web.config file so I can add it directly to any page like so:
<MyTag:SortableList ID="MyList" runat="server" ListType="DropDownList" />
The user control simply adds whatever type of list control you need and provides the means to sort the items by Text or by Value, ascending or descending. There's also a method to add an item to the top of the list (AddFirstItem) if you want to add a default item to the top of the list after you sort the items.
Just add a new User Control to your project and put the code below in the codebehind - no other controls are necessary unless you want to do more with it.
Partial Class Controls_SortableListControl
Inherits System.Web.UI.UserControl
Public Enum ListTypes
DropDownList
ListBox
RadioButtonList
CheckBoxList
BulletedList
End Enum
Private _ListType As ListTypes
Public Property ListType() As ListTypes
Get
If _ListType = Nothing Then
_ListType = ListTypes.DropDownList
End If
Return _ListType
End Get
Set(ByVal value As ListTypes)
_ListType = value
ListSetup()
End Set
End Property
Public Property ListControl() As Object
Get
If Me.FindControl("ListControl") Is Nothing Then
ListSetup()
End If
Return Me.FindControl("ListControl")
End Get
Private Set(ByVal value As Object)
If value IsNot Nothing Then
If Me.FindControl("ListControl") Is Nothing Then
value.ID = "ListControl"
Me.Controls.Add(value)
End If
End If
End Set
End Property
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub ListSetup()
Select Case ListType
Case ListTypes.BulletedList
ListControl = New BulletedList
Case ListTypes.CheckBoxList
ListControl = New CheckBoxList
Case ListTypes.DropDownList
ListControl = New DropDownList
Case ListTypes.ListBox
ListControl = New ListBox
Case ListTypes.RadioButtonList
ListControl = New RadioButtonList
Case Else
ListControl = Nothing
End Select
End Sub
Public Sub AddFirstItem(ByVal li As ListItem)
ListControl.Items.Insert(0, li)
End Sub
Public Sub SortList(ByVal SortByText As Boolean, ByVal SortAscending As Boolean)
Dim ar(ListControl.Items.Count - 1) As ListItem
ListControl.Items.CopyTo(ar, 0)
If SortByText Then
Array.Sort(ar, New ListItemCompareText)
Else
Array.Sort(ar, New ListItemCompareValue)
End If
If Not SortAscending Then
Array.Reverse(ar)
End If
ListControl.Items.Clear()
ListControl.Items.AddRange(ar)
End Sub
Private Class ListItemCompareText
Implements IComparer
Public Function Compare(ByVal x As Object, _
ByVal y As Object) As Integer _
Implements System.Collections.IComparer.Compare
Dim a As ListItem = x
Dim b As ListItem = y
Dim c As New CaseInsensitiveComparer
Return c.Compare(a.Text, b.Text)
End Function
End Class
Private Class ListItemCompareValue
Implements IComparer
Public Function Compare(ByVal x As Object, _
ByVal y As Object) As Integer _
Implements System.Collections.IComparer.Compare
Dim a As ListItem = x
Dim b As ListItem = y
Dim c As New CaseInsensitiveComparer
Return c.Compare(a.Value, b.Value)
End Function
End Class
End Class
|
|
|
|
 |
|
 |
I'm having problem with this code...
Private Class ListItemComparer _
Implements IComparer
Public Function Compare(ByVal x As Object, _
ByVal y As Object) As Integer _
Implements System.Collections.IComparer.Compare
Is the "IComparer" a reference?
|
|
|
|
 |
|
 |
put imports System.Collections at the top of your file before the class declaration. IComarer is part of this namespace. You could alternatively use the fully qualified name of:
Private Class ListItemCompater Implements System.Collections.IComparer
|
|
|
|
 |
|
 |
Still not working to me....kindly please send me a sample project for this i possible. Thank you in advance.
|
|
|
|
 |
|
 |
I'm sorry, but I don't have time to do that. As you can see from the posts below, this code should work as is in a VB ASP.NET application. There's truely not much to it.
|
|
|
|
 |
|
 |
the same problem is happening to me. I couldn't find IComparer in Collections Namespace. Only Comparer is there. I use VS 2003 and .NET framework 1.1. Is there something wrong or not applicable yet?
|
|
|
|
 |
|
 |
The error is here :
Private Class ListItemComparer _
Implements IComparer
You must remove the underscore "_" from the end of line
Muhammad
Muhammad Elnahrawi
www.mycodebank.com
|
|
|
|
 |
|
 |
Thanks for the idea Domenic. I made a slight tweak.
Private Sub SortDropDown(ByVal dd As DropDownList)
Dim ar(dd.Items.Count - 1) As ListItem
dd.Items.CopyTo(ar, 0)
dd.Items.Clear()
ar.Sort(ar, New ListItemComparer)
dd.Items.AddRange(ar)
End Sub
|
|
|
|
 |
|
 |
This is a better approach as ReDim Preserve ar(i) is very expensive on large lists.
|
|
|
|
 |
|
 |
Nice code. It worked immediately. However, when you have an item like "Select one" and want to have it displayed as the default selected item, it will fall down the list of items in the dropdown list. So to make this work, I added on line to your code to add "Select one" item to the top of the list after the sort. Here is your code with the one extra line of code I added:
Private Sub SortDropDown(ByVal dd As DropDownList)
Dim ar As ListItem()
Dim i As Long = 0
For Each li As ListItem In dd.Items
ReDim Preserve ar(i)
ar(i) = li
i += 1
Next
Dim ar1 As Array = ar
ar1.Sort(ar1, New ListItemComparer)
dd.Items.Clear()
'Add an item that will appear as the default selected item in the dropdown list
dd.Items.Add(New ListItem("Select one", "Select one"))
dd.Items.AddRange(ar1)
End Sub
Private Class ListItemComparer
Implements IComparer
Public Function Compare(ByVal x As Object, _
ByVal y As Object) As Integer _
Implements System.Collections.IComparer.Compare
Dim a As ListItem = x
Dim b As ListItem = y
Dim c As New CaseInsensitiveComparer
Return c.Compare(a.Text, b.Text)
End Function
End Class
|
|
|
|
 |
|
 |
Makes sense to me. Thanks for your comments and suggestion!
|
|
|
|
 |
|
 |
What balls programming.
Whoever wrote this should really work for the framework development team... their programming is little better.
|
|
|
|
 |
|
 |
Thanks!
|
|
|
|
 |
|
 |
Just wanted to let you know there's an error when i compile this code.
This code work perfectly when the "_" sign is removed in the Class declaration.
Public Class ListItemComparer
Implements IComparer _
Thanks for the tip.
Jean-Francois Chatelain
|
|
|
|
 |
|
 |
Public Class ListItemComparer
Implements IComparer _
remove the _
Then it will work.
|
|
|
|
 |
|
 |
Thanks for the sample. I needed this in c#, so I wrapped it in a SortableDropDownList
public class SortableDropDownList : DropDownList {
class ListItemComparer : IComparer{
public int Compare(object a, object b){
return new CaseInsensitiveComparer().Compare(((ListItem)a).Text, ((ListItem)b).Text);
}
}
public void SortItems(){
ListItem[] items = new ListItem[Items.Count];
int i = 0;
foreach(ListItem item in Items)
items[i++] = item;
Array.Sort(items, new ListItemComparer());
Items.Clear();
Items.AddRange(items);
}
}
|
|
|
|
 |
|
|
 |
|
 |
Your code returns a null array of listitems, here it the correct version
public void SortDropDown(ref DropDownList ddl)
{
ListItem[] liArray = new ListItem[ddl.Items.Count];
ddl.Items.CopyTo(liArray, 0);
Array.Sort(liArray, new ListItemComparer());
ddl.Items.Clear();
ddl.Items.AddRange(liArray );
}
public class ListItemComparer : IComparer
{
public int Compare(object a, object b)
{
return new CaseInsensitiveComparer().Compare(((ListItem)a).Text, ((ListItem)b).Text);
}
}
|
|
|
|
 |
|
 |
Works great. Minor nit - "ref" is not necessary.
|
|
|
|
 |
|
 |
Thanks a lot. That was helpful
|
|
|
|
 |
|
 |
I try to improve on the SortMethod.
public void SortListItem(DropDownList dropDownList, Enum sortType)
{
ListItem[] listItem = new ListItem[dropDownList.Items.Count];
dropDownList.Items.CopyTo(listItem, 0);
if (sortType.ToString().Equals("Text"))
{
Array.Sort(listItem, new ListItemTextComparer());
}
else
{
Array.Sort(listItem, new ListItemValueComparer());
}
dropDownList.Items.Clear();
dropDownList.Items.AddRange(listItem);
}
internal class ListItemTextComparer : IComparer
{
public int Compare(object x, object y)
{
return new CaseInsensitiveComparer().Compare((x as ListItem).Text, (y as ListItem).Text);
}
}
internal class ListItemValueComparer : IComparer
{
public int Compare(object x, object y)
{
return new CaseInsensitiveComparer().Compare((x as ListItem).Value, (y as ListItem).Value);
}
}
my Blog : ruilei.cnblogs.com
fgfdgfdgdfgdfgfdgdfgdfgfgfdgfdgdfgdfgfdgdfgdfgfgfdgfdgdfgdfgfdgdfgdfgfgfdgfdgdfgdfgfdgdfgdfgfgfdgfdgdfgdfgfdgdfgdfgfgfdgfdgdfgdfgfdgdfgdfgfgfdgfdgdfgdfgfdgdfgdfgfgfdgfdgdfgdfgfdgdfgdfgfgfdgfdgdfgdfgfdgdfgdfgfgfdgfdgdfgdfgfdgdfgdfgfgfdgfdgdfgdfgfdgdfgdfgfgfdgfdgdfgdfgfdgdfgdfgfgfdgfdgdfgdfgfdgdfgdfgfgfdgfdgdfgdfgfdgdfgdfgfgfdgfdgdfgdfgfdgdfgdfgfgfdgfdgdfgdfgfdgdfgdfgfgfdgfdgdfgdfgfdgdfgdfgfgfdgfdgdfgdfgfdgdfgdfg
|
|
|
|
 |
|
 |
ListItem's Forecolor is "Black"
I want turn the forecolor of one of ListItems in the DropDownList to "Red",and others are "Black", How can I do this?
|
|
|
|
 |
|
|
 |