Attributes lost during postback

  • Thread starter Thread starter Raymond Du
  • Start date Start date
R

Raymond Du

Hi,

I am having problem RadioButtonList class, I have a page that populates a
RadiobuttonList during non-postback, but after postback, all attributes I
added disppear. Here is my code snippet;

//Inside Page_Load, I have:
{
/* some codes go here */
if (!isPostback)
RenderForm();
}

function void RenderForm()
{
/* some codes go here */

int i = 0;
foreach (VoteItem vi in voteItemList)
{
ListItem l = new ListItem();
l.Value = vi.Vote;
l.Text = vi.DisplayText;
// the following 2 lines add attributes to each list item
// but they disappear after postback
l.Attributes.Add("class", "VoteItem");
l.Attributes.Add("id", "span_" + i.ToString());
//vote is a RadioButtonList
vote.Items.Add(l);
i++;
}

/* some codes go here */
}


Is this the way it's supposed to be? How do I keep the attribute values
during postback/

TIA
 
Raymond Du said:
Hi,

I am having problem RadioButtonList class, I have a page that populates a
RadiobuttonList during non-postback, but after postback, all attributes I
added disppear. Here is my code snippet;

//Inside Page_Load, I have:
{
/* some codes go here */
if (!isPostback)
RenderForm();
}

function void RenderForm()
{
/* some codes go here */

int i = 0;
foreach (VoteItem vi in voteItemList)
{
ListItem l = new ListItem();
l.Value = vi.Vote;
l.Text = vi.DisplayText;
// the following 2 lines add attributes to each list item
// but they disappear after postback
l.Attributes.Add("class", "VoteItem");
l.Attributes.Add("id", "span_" + i.ToString());
//vote is a RadioButtonList
vote.Items.Add(l);
i++;
}

/* some codes go here */
}


Is this the way it's supposed to be? How do I keep the attribute values
during postback/

TIA

Hello,

Looks like a threading problem to me.
Means, if you modify the GUI from an other thread, you have to invoke the
method.
This can be done over the control member Invoke(), which is a synchron call,
or BeginInvoke, which is a asynchron call.
Both methods require a delegate as parameter.

Hope it helps!

All the best,

Martin
 
Martin# said:
Looks like a threading problem to me.
Means, if you modify the GUI from an other thread, you have to invoke the
method.
This can be done over the control member Invoke(), which is a synchron call,
or BeginInvoke, which is a asynchron call.
Both methods require a delegate as parameter.

I don't see any indication that threading is involved here at all - it
looks like perfectly normal ASP.NET to me...
 
Raymond Du said:
I am having problem RadioButtonList class, I have a page that populates a
RadiobuttonList during non-postback, but after postback, all attributes I
added disppear. Here is my code snippet;
[...]
l.Attributes.Add("class", "VoteItem");
[...]
Is this the way it's supposed to be?

The web controls retain the values of their properties during postback
because the code in the class of the control stores those values in the
ViewState and then recovers them after postback. The RadiobuttonList does
not store the Attributes of its Items in the ViewState, so they get lost
during postback. You will need to preserve them yourself by adding some code
to your form to store and later recover the relevant information in the
ViewState collection.
 
Jon Skeet said:
I don't see any indication that threading is involved here at all - it
looks like perfectly normal ASP.NET to me...

Hello,

Was just a guess, as the discriped effects would perfectly fit to the
scenario I discriped.
But as I'm only doing WinForms application, I haven't recognized it is
ASP.NET.

So sorry for my unqualified statment!

All the best,

Martin
 
Back
Top