why does this not work at postback?

V

Vincent

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.
 
A

Anthony Jones

Vincent said:
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.


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.
 
V

Vincent

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
 
A

Anthony Jones

Vincent said:
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").


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.
 
V

Vincent

Ok, thanks, i'll try it.

Anthony Jones said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top