Dynamically Created RadioButtonList Items & ViewState

Discussion in 'ASP .Net Building Controls' started by jdn, Jan 20, 2004.

  1. jdn

    jdn Guest

    I'm having a variation on the typical problem with saving ViewState of dynamically created web controls.

    I have two radiobuttonlist controls (call them rbl1 and rbl2). Both are created at design time. Each has three items in their Items collection.

    When the selected item in rbl1 changes, the Items collection of rbl2 is cleared and then has new items added (depending on which item of rbl1 is selected) to it.

    I want to do various things when the selected item for rbl2 changes. Needless to say, after rbl2 has had its items cleared and then new ones added, it no longer saves any viewstate information on post-back.

    Now, if this were a 'typical' situation, I'd add some code in Page_Load to deal with it, but I'm not sure what I would add here. The control itself already exists, so I need to add something to handle the items in that control's Items collection.

    Any ideas?

    TIA
    jdn
    jdn, Jan 20, 2004
    #1
    1. Advertising

  2. Hi jdn,

    Thank you for posting in the community! My name is Jeffrey, and I will be
    assisting you on this issue.

    Based on my understanding, when you dynamic control the radiobuttonlist at
    runtime, you meet the problem of viewstate maintaining.

    ============================================================
    I am not very certain that what state of your rbl2 will persist. Actually,
    because all the items of rbl2 is cleared and new items are added, so all
    its item related state will be re-initialized.

    If you want to persist some item-irrespective properties, I think you can
    persist it through viewstate freely.(The control's predefined properties
    have already been persisted through viewstate)
    To persist your customized property through viewstate, do like this:
    public int public_property
    {
    get
    {
    return ViewState["internal_property"];
    }
    set
    {
    ViewState["internal_property"]=value;
    }
    }

    In my test project, I do your program logic like this:

    private void rbl1_SelectedIndexChanged(object sender, System.EventArgs e)
    {
    rbl2 .Items.Clear();
    for(int i=0;i<3;i++)
    {
    rbl2 .Items.Add(new ListItem(rbl1.SelectedItem.Text));
    }
    }

    If your viewstate persistance has other conern, please feel free to tell
    me. I will work with you.
    =========================================================
    Thank you for your patience and cooperation.Hope my reply makes sense to
    you.

    Best regards,
    Jeffrey Tan
    Microsoft Online Partner Support
    Get Secure! - www.microsoft.com/security
    This posting is provided "as is" with no warranties and confers no rights.
    Jeffrey Tan[MSFT], Jan 21, 2004
    #2
    1. Advertising

  3. jdn

    jdn Guest

    Hello again Jeffrey, nice to speak with you again. I appreciate your
    efforts.

    Let me describe in more detail the issue I am facing.

    rbl1 is a design time only control that has three ListItems, call them
    rbl1.1, rbl1.2 and rbl1.3. Since this control is design-time only, I
    have no problems with ViewState for rbl1 at all.

    rbl2 is also a design time control, BUT, it also has dynamic/run-time
    'issues'. Let me try to explain.

    By default, rbl2 has 3 ListItems, let's call them rbl2.1, rbl2.2 and
    rbl2.3. These are all set in design view in VS.NET IDE, just like rbl1.
    So far so good.

    However, for reasons that go beyond explaining here, the Items
    collection of rbl2 changes depending on the SelectedIndex change event
    of rbl1, and these are dynamic.

    So, for instance, if I select rbl1.1, then rbl2 is cleared of its Items,
    and then rbl2.1, rbl2.2 and rbl2.3 are re-added to it. If I select
    rbl1.2, then rbl2 is cleared of its Items, and rbl2.2 and rbl2.3 are
    re-added to it. If I select rbl1.3, then rbl2 is cleared of its Items
    and rbl2.3 is re-added to it.

    This is necessary because there is no rbl2.items[x].visible = false
    property available.

    So, I need to be able to maintain the viewstate of rbl2, not only in
    terms of what the SelectedIndex is for rbl2, but also what items are
    actually present.

    Maybe you can see my dilemma. I can do something like
    ViewState["rbl2SelectedIndex"], but that only works if the Items
    collection of rbl2 stays the same. But it doesn't. I might have 1 item
    or 2 items or 3 items in rbl2, I don't know for sure in advance. If the
    SelectedIndex of rbl1 is 0, then there are 3 items in rbl2, if the
    SelectedIndex of rbl1 is 1, then there are 2 items in rbl2, if the
    SelectedIndex of rbl1 is 2, then there is only 1 item in rbl2.

    So, I need to track the viewstate of rbl2 for both the SelectedIndex AND
    the number of items in rbl2.Items, AND tie them together.

    If there was a rbl2.Item[x].Visible property, I would just set that to
    false and not have to worry about making it run-time, but I can't do
    that, as far as I can see.

    So what do I do?

    jdn



    Jeffrey Tan[MSFT] wrote:
    > Hi jdn,
    >
    > Thank you for posting in the community! My name is Jeffrey, and I will be
    > assisting you on this issue.
    >
    > Based on my understanding, when you dynamic control the radiobuttonlist at
    > runtime, you meet the problem of viewstate maintaining.
    >
    > ============================================================
    > I am not very certain that what state of your rbl2 will persist. Actually,
    > because all the items of rbl2 is cleared and new items are added, so all
    > its item related state will be re-initialized.
    >
    > If you want to persist some item-irrespective properties, I think you can
    > persist it through viewstate freely.(The control's predefined properties
    > have already been persisted through viewstate)
    > To persist your customized property through viewstate, do like this:
    > public int public_property
    > {
    > get
    > {
    > return ViewState["internal_property"];
    > }
    > set
    > {
    > ViewState["internal_property"]=value;
    > }
    > }
    >
    > In my test project, I do your program logic like this:
    >
    > private void rbl1_SelectedIndexChanged(object sender, System.EventArgs e)
    > {
    > rbl2 .Items.Clear();
    > for(int i=0;i<3;i++)
    > {
    > rbl2 .Items.Add(new ListItem(rbl1.SelectedItem.Text));
    > }
    > }
    >
    > If your viewstate persistance has other conern, please feel free to tell
    > me. I will work with you.
    > =========================================================
    > Thank you for your patience and cooperation.Hope my reply makes sense to
    > you.
    >
    > Best regards,
    > Jeffrey Tan
    > Microsoft Online Partner Support
    > Get Secure! - www.microsoft.com/security
    > This posting is provided "as is" with no warranties and confers no rights.
    >
    jdn, Jan 21, 2004
    #3
  4. jdn

    jdn Guest

    <snip>

    Well, 'luckily' I can chalk this one up to user error.

    I took another look at my code behind and I was calling some code on the
    relevant postback that essentially overwrote the SelectedIndex change,
    thus it wasn't persisting.

    Works fine once corrected.

    jdn
    jdn, Jan 23, 2004
    #4
  5. Hi jdn,

    Please pardon the delay in answering your question. We are still
    researching and will post more information as soon as we can.

    Thank you, Mike
    Microsoft, ASP.NET Support Professional

    Microsoft highly recommends to all of our customers that they visit the
    http://www.microsoft.com/protect site and perform the three straightforward
    steps listed to improve your computer’s security.

    This posting is provided "AS IS", with no warranties, and confers no rights.

    --------------------
    > Date: Wed, 21 Jan 2004 01:59:08 -0600
    > From: jdn <>
    > User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.6)

    Gecko/20040113
    > X-Accept-Language: en-us, en
    > MIME-Version: 1.0
    > Subject: Re: Dynamically Created RadioButtonList Items & ViewState
    > References: <>

    <rucPjY#>
    > In-Reply-To: <rucPjY#>
    > Content-Type: text/plain; charset=us-ascii; format=flowed
    > Content-Transfer-Encoding: 7bit
    > Message-ID: <#sEg#R$>
    > Newsgroups: microsoft.public.dotnet.framework.aspnet.buildingcontrols
    > NNTP-Posting-Host: dsl092-132-209.chi1.dsl.speakeasy.net 66.92.132.209
    > Lines: 1
    > Path:

    cpmsftngxa07.phx.gbl!cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.
    phx.gbl
    > Xref: cpmsftngxa07.phx.gbl

    microsoft.public.dotnet.framework.aspnet.buildingcontrols:8778
    > X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.buildingcontrols
    >
    > Hello again Jeffrey, nice to speak with you again. I appreciate your
    > efforts.
    >
    > Let me describe in more detail the issue I am facing.
    >
    > rbl1 is a design time only control that has three ListItems, call them
    > rbl1.1, rbl1.2 and rbl1.3. Since this control is design-time only, I
    > have no problems with ViewState for rbl1 at all.
    >
    > rbl2 is also a design time control, BUT, it also has dynamic/run-time
    > 'issues'. Let me try to explain.
    >
    > By default, rbl2 has 3 ListItems, let's call them rbl2.1, rbl2.2 and
    > rbl2.3. These are all set in design view in VS.NET IDE, just like rbl1.
    > So far so good.
    >
    > However, for reasons that go beyond explaining here, the Items
    > collection of rbl2 changes depending on the SelectedIndex change event
    > of rbl1, and these are dynamic.
    >
    > So, for instance, if I select rbl1.1, then rbl2 is cleared of its Items,
    > and then rbl2.1, rbl2.2 and rbl2.3 are re-added to it. If I select
    > rbl1.2, then rbl2 is cleared of its Items, and rbl2.2 and rbl2.3 are
    > re-added to it. If I select rbl1.3, then rbl2 is cleared of its Items
    > and rbl2.3 is re-added to it.
    >
    > This is necessary because there is no rbl2.items[x].visible = false
    > property available.
    >
    > So, I need to be able to maintain the viewstate of rbl2, not only in
    > terms of what the SelectedIndex is for rbl2, but also what items are
    > actually present.
    >
    > Maybe you can see my dilemma. I can do something like
    > ViewState["rbl2SelectedIndex"], but that only works if the Items
    > collection of rbl2 stays the same. But it doesn't. I might have 1 item
    > or 2 items or 3 items in rbl2, I don't know for sure in advance. If the
    > SelectedIndex of rbl1 is 0, then there are 3 items in rbl2, if the
    > SelectedIndex of rbl1 is 1, then there are 2 items in rbl2, if the
    > SelectedIndex of rbl1 is 2, then there is only 1 item in rbl2.
    >
    > So, I need to track the viewstate of rbl2 for both the SelectedIndex AND
    > the number of items in rbl2.Items, AND tie them together.
    >
    > If there was a rbl2.Item[x].Visible property, I would just set that to
    > false and not have to worry about making it run-time, but I can't do
    > that, as far as I can see.
    >
    > So what do I do?
    >
    > jdn
    >
    >
    >
    > Jeffrey Tan[MSFT] wrote:
    > > Hi jdn,
    > >
    > > Thank you for posting in the community! My name is Jeffrey, and I will

    be
    > > assisting you on this issue.
    > >
    > > Based on my understanding, when you dynamic control the radiobuttonlist

    at
    > > runtime, you meet the problem of viewstate maintaining.
    > >
    > > ============================================================
    > > I am not very certain that what state of your rbl2 will persist.

    Actually,
    > > because all the items of rbl2 is cleared and new items are added, so

    all
    > > its item related state will be re-initialized.
    > >
    > > If you want to persist some item-irrespective properties, I think you

    can
    > > persist it through viewstate freely.(The control's predefined

    properties
    > > have already been persisted through viewstate)
    > > To persist your customized property through viewstate, do like this:
    > > public int public_property
    > > {
    > > get
    > > {
    > > return ViewState["internal_property"];
    > > }
    > > set
    > > {
    > > ViewState["internal_property"]=value;
    > > }
    > > }
    > >
    > > In my test project, I do your program logic like this:
    > >
    > > private void rbl1_SelectedIndexChanged(object sender, System.EventArgs

    e)
    > > {
    > > rbl2 .Items.Clear();
    > > for(int i=0;i<3;i++)
    > > {
    > > rbl2 .Items.Add(new ListItem(rbl1.SelectedItem.Text));
    > > }
    > > }
    > >
    > > If your viewstate persistance has other conern, please feel free to

    tell
    > > me. I will work with you.
    > > =========================================================
    > > Thank you for your patience and cooperation.Hope my reply makes sense

    to
    > > you.
    > >
    > > Best regards,
    > > Jeffrey Tan
    > > Microsoft Online Partner Support
    > > Get Secure! - www.microsoft.com/security
    > > This posting is provided "as is" with no warranties and confers no

    rights.
    > >

    >
    Mike Moore [MSFT], Jan 23, 2004
    #5
  6. Hi jdn,

    I am glad you figured it out.
    Usually, we should be careful of the postback, and re-initiallize. The
    common way is judge Page.IsPostBack property in Page_Load event.

    Anyway, congratulations!!

    If you have any further concern, please feel free to tell me, I will work
    with you.

    Best regards,
    Jeffrey Tan
    Microsoft Online Partner Support
    Get Secure! - www.microsoft.com/security
    This posting is provided "as is" with no warranties and confers no rights.
    Jeffrey Tan[MSFT], Jan 23, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Crowley
    Replies:
    0
    Views:
    715
    John Crowley
    Jan 14, 2004
  2. yufufi
    Replies:
    2
    Views:
    374
    Teemu Keiski
    Jul 7, 2004
  3. Replies:
    5
    Views:
    712
  4. msimmons
    Replies:
    0
    Views:
    456
    msimmons
    Jul 16, 2009
  5. Anton Sommer

    disable viewstate of dynamically created checkboxes

    Anton Sommer, Sep 29, 2003, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    242
    Teemu Keiski
    Oct 5, 2003
Loading...

Share This Page