rite_eh
asked on
ListBox style attribute not saved
lItem = new ListItem
litem.Value = "Microsoft"
lItem.Text = "Broken by design"
lItem.Attributes.Add("styl e", "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...
litem.Value = "Microsoft"
lItem.Text = "Broken by design"
lItem.Attributes.Add("styl
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...
Where are you putting this code in which event?
normaly u would put it in the Page_load event with in if(!isPostBack){ }
normaly u would put it in the Page_load event with in if(!isPostBack){ }
ASKER
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...
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("styl e", "color: green");
}
}
private void lItem_SelectedIndexChanged (object sender, System.EventArgs e)
{
}
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("styl
}
}
private void lItem_SelectedIndexChanged
{
}
ASKER
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.
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.
What I meant is the control property EnableViewState = true, make sure to enforce this.
ASKER
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("styl e", "color: green")
Case "chg"
lItem.Attributes.Add("styl e", "color: blue")
Case "out"
lItem.Attributes.Add("styl e", "color: red")
Case "mov"
lItem.Attributes.Add("styl e", "color: purple")
Case "get"
lItem.Attributes.Add("styl e", "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_SelectedIndexChan ged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstQueue.SelectedIndexChan ged
If lstQueue.Items.Count > 0 Then
txtEdit.Text = lstQueue.SelectedItem.Text
End If
End Sub
End Class
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("styl
Case "chg"
lItem.Attributes.Add("styl
Case "out"
lItem.Attributes.Add("styl
Case "mov"
lItem.Attributes.Add("styl
Case "get"
lItem.Attributes.Add("styl
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_SelectedIndexChan
If lstQueue.Items.Count > 0 Then
txtEdit.Text = lstQueue.SelectedItem.Text
End If
End Sub
End Class
ASKER
<%@ 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:ListBo x><br />
<br />
<asp:Button ID="cmdEdit" runat="server" Text="Edit" Width="75px" />
<asp:TextBox ID="txtEdit" runat="server"></asp:TextB ox></div>
</form>
</body>
</html>
<!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 />
<asp:ListBox ID="lstQueue" runat="server" AutoPostBack="True" CssClass="textFixed"
EnableTheming="True" Height="280px" SelectionMode="Multiple" Width="680px"></asp:ListBo
<br />
<asp:Button ID="cmdEdit" runat="server" Text="Edit" Width="75px" />
<asp:TextBox ID="txtEdit" runat="server"></asp:TextB
</form>
</body>
</html>
ASKER
I also tried this:
<%@ Page Language="VB" AutoEventWireup="false" EnableViewState="true" CodeFile="webzTEST.aspx.vb " Inherits="webzTEST" %>
<%@ Page Language="VB" AutoEventWireup="false" EnableViewState="true" CodeFile="webzTEST.aspx.vb
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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.
Hey ,
Sometimes I need another pair of eyes to see my hat that I am wearing and looking for ;-)
Sometimes I need another pair of eyes to see my hat that I am wearing and looking for ;-)
ASKER