Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Professional
|
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 8th, 2008, 05:56 AM
Friend of Wrox
 
Join Date: Apr 2006
Posts: 160
Thanks: 0
Thanked 0 Times in 0 Posts
Default problem with color of listitems when postback

Hi,

the dropdownlist is fed by code-behind with roperty 'autopostback'="true". What i want is to put a color to the items of the dropdownlist but only for some values. This code below works: the right items are red, but only the first time. When an user clicks on an item, the postback occurs (property Autopostback) and the dropdownlist is fed again but the color of the items has disappeared. EnabledViewState is set to "true".

i can't use property "forecolor" of the dropdownlist because of the rstriction of some values.

<%@ Page Language="VB" ... EnabledViewState="true" %@>

<asp:DropDownList id="DropDownList1" runat="server" AutoPostBack="true">
</asp:DropDownList>

code-behind:

Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

.......

If Page.IsPostBack Then
dd = DropDownList1.SelectedValue
End If

For i = 1 To 10
z = New ListItem(i, i)
If i <= myvar Then z.Attributes.Add("style", "color:red")
DropDownList1.Items.Add(z)
next
........


Thanks
H.

 
Old March 8th, 2008, 01:35 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

It appears that your IF is evaluating to false on postback. I don't see where you are setting the value 'myvar' but my guess is if you step through the code your IF simply doesn't execute.

Try that.

hth.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
 
Old March 8th, 2008, 05:48 PM
Friend of Wrox
 
Join Date: Apr 2006
Posts: 160
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for replying.

I removed the IF and just put this line:
z.Attributes.Add("style", "color:red")

Now all listitems are red at the first time, but after the first postback, the red color is gone.

It must be a problem of postabck, but wht exactly?

 
Old March 8th, 2008, 06:21 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Try this on postback:

If Page.IsPostBack Then

 For Each li as ListItem in drpDown.Items
    If i <= myvar Then li.Attributes.Add("style", "color:red")
 Next
End If

obviously your If logic whould change since i is no longer used as a counter variable. Anyway my guess is that while you loop through a bunch on list items on postback you don't actually change the list items in the dropdown.

hth.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
 
Old March 9th, 2008, 06:06 AM
Friend of Wrox
 
Join Date: Apr 2006
Posts: 160
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks.
Meanwhile, i made some tests (both with EnabledViewState="true":

this first test works: (i dropped the IF): all items are red and remain red after postback.
-------------------------
If Page.IsPostBack Then
dd = DropDownList1.SelectedValue
Else
For i = 1 To 10
z = New ListItem(i, i)
DropDownList1.ForeColor = Drawing.Color.Red
DropDownList1.Items.Add(z)
next
end if

this second test doesn't work: the first time, the items are red, but at the first postback, the items lose the red color.
----------------------
If Page.IsPostBack Then
dd = DropDownList1.SelectedValue
else
For i = 1 To 10
z = New ListItem(i, i)
z.Attributes.Add("style", "color:red")
DropDownList1.Items.Add(z)
next
end if

So the unique difference between the two tests is using:
in first test: DropDownList1.ForeColor = Drawing.Color.Red
in second test: z.Attributes.Add("style", "color:red")

Why?



 
Old March 9th, 2008, 07:02 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Quote:
quote:So the unique difference between the two tests is using:
in first test: DropDownList1.ForeColor = Drawing.Color.Red
in second test: z.Attributes.Add("style", "color:red")

Why?
Because the ListItem class does not persist its Attributes in ViewState, while the DropDownList does persist values like ForeColor. To work around this, rely on ViewState for the actual items, and then loop through the Items collection on Page_Load and every postback and colorize the relevant items.

More info:

http://www.google.com/search?hl=en&q...tion+ViewState
http://aspnet.4guysfromrolla.com/articles/110205-1.aspx
http://forums.asp.net/p/893276/957231.aspx#957231

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
 
Old March 9th, 2008, 11:00 AM
Friend of Wrox
 
Join Date: Apr 2006
Posts: 160
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks






Similar Threads
Thread Thread Starter Forum Replies Last Post
Postback problem dbrook007 ASP.NET 2.0 Professional 3 May 8th, 2008 01:02 AM
Postback Problem dbrook007 ASP.NET 2.0 Professional 1 April 1st, 2008 06:16 AM
Different colors for listitems of radiobuttonlist mahulda General .NET 2 August 27th, 2004 08:17 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.