• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 557
  • Last Modified:

ListBox style attribute not saved

lItem = new ListItem
litem.Value = "Microsoft"
lItem.Text = "Broken by design"
lItem.Attributes.Add("style", "color: green")

OnPostBack of any element (not just the listbox where the above item is housed) causes the 'Attribute' "style = color: green" to be removed. The screen is correctly redrawn with all text and values as previously loaded but the style is lost. Any ideas how this can be preserved? I'd rather not have to track which items I have colored (and which colors) and have to reload it OnPostBack.

Thanks in advance...
0
rite_eh
Asked:
rite_eh
  • 7
  • 5
1 Solution
 
rite_ehAuthor Commented:
Anyone with any suggestions?
0
 
meomarCommented:
Where are you putting this code in which event?

normaly u would put it in the Page_load event with in if(!isPostBack){ }

0
 
rite_ehAuthor Commented:
That is what I am doing however attributes are lost onPostBack. This is the problem I am having. Unless there is another way to color individual listbox items without using attributes...
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
meomarCommented:
I have tried this with following code with autopostback = true(Do you have your viewstate enabled on this control and parent controls?) and it worked and maintained the style. Please post the complete code if you can.  
private void Page_Load(object sender, System.EventArgs e)
              {
                  // Put user code to initialize the page here

                  if(!IsPostBack)
                  {
                        Label1.Text = "Hello world";
                        lItem.Items.Add( new ListItem("hello","world"));
                        lItem.Items.Add( new ListItem("hello","world"));
                        lItem.Attributes.Add("style", "color: green");
                  }

      }

            private void lItem_SelectedIndexChanged(object sender, System.EventArgs e)
            {
            
            }
0
 
rite_ehAuthor Commented:
Hello,

Thanks for your continued help. I'll clip the code I'm having trouble with shortly, but my routines that populate the listbox are being called from page_load and are not being run again on postback. I'm not too sure what you mean by ViewState (I'm programming in ASP.NET / VB backend) but autopostback is true on all controls having this problem.
0
 
meomarCommented:
What I meant is the  control property  EnableViewState = true, make sure to enforce this.
0
 
rite_ehAuthor Commented:
OK yes EnableViewState is true (default value) on the control in question.

I have created a new page in my solution and written a very basic routine that is also losing the color styles. Here it is:

---------------------- text.txt ----------------------
chg 0123456789
mov QQQQQQQQQQ
get AAAAAAAAAA
new ZZZZZZZZZZ
new WWWWWWWWWW
chg SSSSSSSSSS
chg XXXXXXXXXX
out EEEEEEEEEE
out DDDDDDDDDD
mov CCCCCCCCCC
mov RRRRRRRRRR
get FFFFFFFFFF
get VVVVVVVVVV
chg 0123456789
---------------------- text.txt ----------------------

lstQueue - listbox, autopostback = true, enableviewstate = true
cmdEdit - button, see _click routine below, enableviewstate = true
lblInfo - label for outputting errors, enableviewstate = true
txtEdit - textbox, enableviewstate = true

Also note that I lost the colors (style) if I change the selected index or use the button. Both work incorrectly.

-------------------------------------------------------


Partial Class webzTEST
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsPostBack = False Then
            Dim lItem As ListItem
            Dim strTemp As String
            Try
                FileOpen(1, "C:\test.txt", OpenMode.Input)
                While Not EOF(1)
                    strTemp = LineInput(1)
                    If strTemp.Length > 0 Then
                        lItem = New ListItem
                        lItem.Text = Mid(strTemp, 5, strTemp.Length)
                        Select Case Left(strTemp, 3)
                            Case "new"
                                lItem.Attributes.Add("style", "color: green")
                            Case "chg"
                                lItem.Attributes.Add("style", "color: blue")
                            Case "out"
                                lItem.Attributes.Add("style", "color: red")
                            Case "mov"
                                lItem.Attributes.Add("style", "color: purple")
                            Case "get"
                                lItem.Attributes.Add("style", "color: gray")
                        End Select
                        lstQueue.Items.Add(lItem)
                    End If
                End While
                FileClose(1)
            Catch ex As Exception
                lblInfo.Text = ex.Message.ToString
                Reset()
            End Try
        End If

    End Sub

    Protected Sub cmdEdit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdEdit.Click
        If lstQueue.Items.Count > 0 Then
            txtEdit.text = lstQueue.SelectedItem.Text
        End If
    End Sub

    Protected Sub lstQueue_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstQueue.SelectedIndexChanged
        If lstQueue.Items.Count > 0 Then
            txtEdit.Text = lstQueue.SelectedItem.Text
        End If
    End Sub

End Class
0
 
rite_ehAuthor Commented:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="webzTEST.aspx.vb" Inherits="webzTEST" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Test Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="lblInfo" runat="server" Font-Bold="True" ForeColor="Red" Width="680px"></asp:Label><br />
        <br />
        <asp:ListBox ID="lstQueue" runat="server" AutoPostBack="True" CssClass="textFixed"
            EnableTheming="True" Height="280px" SelectionMode="Multiple" Width="680px"></asp:ListBox><br />
        <br />
        <asp:Button ID="cmdEdit" runat="server" Text="Edit" Width="75px" />
        <asp:TextBox ID="txtEdit" runat="server"></asp:TextBox></div>
    </form>
</body>
</html>
0
 
rite_ehAuthor Commented:
I also tried this:
<%@ Page Language="VB" AutoEventWireup="false" EnableViewState="true" CodeFile="webzTEST.aspx.vb" Inherits="webzTEST" %>
0
 
meomarCommented:
Hi Rite eh

I kind figured out the problem and came to a  90 %  of the solution.

Ok the problem:

The List item that you are generating is created on the fly inside a method so technicaly it is a local object and it only gets created if it is not a post back so it would loose its inner properties and the Text fields will only be genarics. So when it is posted back the none of this is recreated again and I guess this is why it is losing the attributes on selection.

The solution:

Rewrite the page_load event as follows and link that event to the DataBound event of the list box:
(I am sorry I comming from C# forget my bad VB style)

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected lItem As ListItem

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load, lstQueue.DataBound
        If IsPostBack = False Then

            Dim strTemp As String
            Try
                FileOpen(1, "C:\test.txt", OpenMode.Input)
                While Not EOF(1)
                    strTemp = LineInput(1)
                    If strTemp.Length > 0 Then
                        lItem = New ListItem

                        lItem.Text = Mid(strTemp, 5, strTemp.Length)
                        Select Case Left(strTemp, 3)
                            Case "new"
                                lItem.Attributes.Add("style", "color: green")
                                lItem.Value = "new"
                            Case "chg"
                                lItem.Attributes.Add("style", "color: blue")
                                lItem.Value = "chg"
                            Case "out"
                                lItem.Attributes.Add("style", "color: red")
                                lItem.Value = "out"
                            Case "mov"
                                lItem.Attributes.Add("style", "color: purple")
                                lItem.Value = "mov"
                            Case "get"
                                lItem.Attributes.Add("style", "color: gray")
                                lItem.Value = "get"
                        End Select
                        lstQueue.Items.Add(lItem)
                    End If
                End While
                FileClose(1)
            Catch ex As Exception
                lblInfo.Text = ex.Message.ToString
                Reset()
            End Try
        End If

        Dim count As Integer
        count = 0
        If IsPostBack = True Then
            While count < lstQueue.Items.Count - 1

                Select Case lstQueue.Items(count).Value.ToString()
                    Case "new"
                        lstQueue.Items(count).Attributes("style") = "color: green"
                    Case "chg"
                        lstQueue.Items(count).Attributes("style") = "color: blue"
                    Case "out"
                        lstQueue.Items(count).Attributes("style") = "color: red"
                    Case "mov"
                        lstQueue.Items(count).Attributes("style") = "color: purple"
                    Case "get"
                        lstQueue.Items(count).Attributes("style") = "color: gray"


                End Select
                count = count + 1
            End While
        End If


    End Sub

    Protected Sub cmdEdit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdEdit.Click
        If lstQueue.Items.Count > 0 Then
            txtEdit.Text = lstQueue.SelectedItem.Text
        End If
    End Sub

    Protected Sub lstQueue_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstQueue.SelectedIndexChanged
        If lstQueue.Items.Count > 0 Then
            txtEdit.Text = lstQueue.SelectedItem.Text
        End If
    End Sub

End Class  


Let me know if this works for you.
0
 
rite_ehAuthor Commented:
Hi meomar,

Thanks for your solution.

I'm not entirely sure why I never thought of using the listbox to re-apply the colors onPostBack as you have done. This will be the easiest way to solve the problem though. Thanks for taking the time to help me with this. It was much appreciated.
0
 
meomarCommented:
Hey ,

Sometimes  I need another pair of eyes to see my hat that I am wearing and looking for ;-)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now