Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Professional
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 8th, 2008, 05:56 AM
Friend of Wrox
 
Join Date: Apr 2006
Location: , , .
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.

  #2 (permalink)  
Old March 8th, 2008, 01:35 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
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
================================================== =========
  #3 (permalink)  
Old March 8th, 2008, 05:48 PM
Friend of Wrox
 
Join Date: Apr 2006
Location: , , .
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?

  #4 (permalink)  
Old March 8th, 2008, 06:21 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
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
================================================== =========
  #5 (permalink)  
Old March 9th, 2008, 06:06 AM
Friend of Wrox
 
Join Date: Apr 2006
Location: , , .
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?



  #6 (permalink)  
Old March 9th, 2008, 07:02 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 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.
  #7 (permalink)  
Old March 9th, 2008, 11:00 AM
Friend of Wrox
 
Join Date: Apr 2006
Location: , , .
Posts: 160
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks

 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 04:47 PM.


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