Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / March 2008

Tip: Looking for answers? Try searching our database.

why does this not work at postback?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Vincent - 09 Mar 2008 12:21 GMT
Hi,

the dropdownlist is fed by code-behind with property 'autopostback'="true".
What i want is to put a color to the items of the dropdownlist. I tried two
ways: with Attributes.Add("style", "color:red")  and with
DropDownList1.ForeColor.
EnabledViewState is set to "true".

The first way doesn't work: the first time, the items in the dd are red, but
at the first postback, the red color has gone.
The second way works: the items in the dd are red and remains red after the
next postbacks.

This is the aspx-code:

<%@ Page Language="VB" ... EnabledViewState="true"  %@>
<asp:DropDownList id="DropDownList1" runat="server" AutoPostBack="true">
</asp:DropDownList>

code-behind:

first way: this doesn't work
---------------------------
Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
.......
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
........

second way: this works
------------------------
Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
....
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
........

Why does the first way not work?

Meanwhile I found a solution:
-----------------------------
....
If Page.IsPostBack Then
dd = DropDownList1.SelectedValue
DropDownList1.Items.Clear()
End If

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

But still want to know why first way doesn't work.

Thanks
Vincent.
Anthony Jones - 09 Mar 2008 15:48 GMT
> Hi,
>
[quoted text clipped - 66 lines]
>
> But still want to know why first way doesn't work.

The purpose of the control attributes collection to allow rendering of
markup attributes found in the page that do not map to control properties.
Importantly the attributes collection is not stored in the controls
viewstate (since its purpose is merely to be filled from the existing
markup).

Your first chunk of code fails because on postback the attributes collection
isn't restored and therefore your style attribute is not included.

The second chunk works because you are using the propery designed for the
purpose (which ultimately results in the style attribute being added to the
element).  Since you are modifying a known property of the control its value
is stored in the viewstate.  On postback this value is restore hence the
control is correctly rendered.

The third chunk works because you "manually" restored ther attributes
collection on each request regardless of whether its a postback or not.

Its probably a bad idea to attempt to add the style attribute via the
attributes collection.  There are a many control properties such as
forecolor that will utlimately result in this being modified.  Thats not to
mention the WebControls style property itself.

Signature

Anthony Jones - MVP ASP/ASP.NET

Vincent - 09 Mar 2008 16:23 GMT
Hi, thanks for your explanations. I understand now.

The reason why i use Attributes.Add("style", "color:red") instead of
DropDownList1.ForeColor is that only some items must be red. I didn't
mention it in order to not complicate the problem.

The code contains this extra line:
If some_value_of_item <=  myvalue Then z.Attributes.Add("style",
"color:red").

I also tried this but here, all items become red:
If some_value_of_item <=  myvalue Then DropDownList1.ForeColor

So i couldn't find a better solution than using Attributes.Add("style",
"color:red").

If you have a better suggestion, tell me ..
Thanks

>> Hi,
>>
[quoted text clipped - 99 lines]
> to
> mention the WebControls style property itself.
Anthony Jones - 09 Mar 2008 22:58 GMT
> Hi, thanks for your explanations. I understand now.
>
[quoted text clipped - 11 lines]
> So i couldn't find a better solution than using Attributes.Add("style",
> "color:red").

Hmm.. yes I see your problem a little clearer now. You will need to use the
Attributes collection in this case after all.   The ListItem class is
actually an abstraction of a common pattern used by an number of controls
that contain a list.  Each such control may have a completely different way
of rendering the list and therefore the ListItem class itself offers no
properties that specifically address how its rendered.

In this case my approach would be to use the PreRender event of the
DropDownList control.  In the event, loop through the Items collection and
for any ListItem whose Value <= myValue add the "style" attribute to the
ListItem.  This way you need only initialise the Items collection once on
the initial page load and not on any postbacks.

Signature

Anthony Jones - MVP ASP/ASP.NET

Vincent - 10 Mar 2008 18:24 GMT
Ok, thanks, i'll try it.

>> Hi, thanks for your explanations. I understand now.
>>
[quoted text clipped - 26 lines]
> ListItem.  This way you need only initialise the Items collection once on
> the initial page load and not on any postbacks.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.