Are dynamically loaded controls preserved over a postback?

Discussion in 'ASP .Net' started by Alan Silver, Aug 8, 2005.

  1. Alan Silver

    Alan Silver Guest

    Hello,

    I have a placeholder, which gets a user control added in when the page
    first loads. This user control contains a dropdownlist, and I would like
    to get hold of the value of this drop down list form the calling page on
    postback. Can I do this? If I try and use FindControl on the
    placeholder, it can't find the dropdownlist.

    I tried...

    ShowCategory ctlShowCategory = (ShowCategory)plcCategoryDetails.Controls[0];

    (where ShowCategory is the user control) but this gave the error...

    System.ArgumentOutOfRangeException: Specified argument was out of the
    range of valid values. Parameter name: index

    which sounds like the user control wasn't preserved in the placeholder
    over the postback.

    Any ideas how I do this? Hope this was clear!! TIA

    --
    Alan Silver
    (anything added below this line is nothing to do with me)
    Alan Silver, Aug 8, 2005
    #1
    1. Advertising

  2. Alan Silver

    Bruce Barker Guest

    you have to recreate the dynamic comtrol on postback. there is no code in
    the placeholder for this.

    -- bruce (sqlwork.com)

    "Alan Silver" <> wrote in message
    news:kbQ$lsKW569CFwf$@nospamthankyou.spam...
    > Hello,
    >
    > I have a placeholder, which gets a user control added in when the page
    > first loads. This user control contains a dropdownlist, and I would like
    > to get hold of the value of this drop down list form the calling page on
    > postback. Can I do this? If I try and use FindControl on the placeholder,
    > it can't find the dropdownlist.
    >
    > I tried...
    >
    > ShowCategory ctlShowCategory =
    > (ShowCategory)plcCategoryDetails.Controls[0];
    >
    > (where ShowCategory is the user control) but this gave the error...
    >
    > System.ArgumentOutOfRangeException: Specified argument was out of the
    > range of valid values. Parameter name: index
    >
    > which sounds like the user control wasn't preserved in the placeholder
    > over the postback.
    >
    > Any ideas how I do this? Hope this was clear!! TIA
    >
    > --
    > Alan Silver
    > (anything added below this line is nothing to do with me)
    Bruce Barker, Aug 8, 2005
    #2
    1. Advertising

  3. Alan Silver

    Alan Silver Guest

    >you have to recreate the dynamic comtrol on postback. there is no code in
    >the placeholder for this.


    If I recreate the user control, will the child controls have their
    values preserved? The point of this is that I want to get the value of a
    dropdownlist in the user control. If the value of that dropdownlist
    isn't preserved (ie if recreating the user control resets the child
    controls), then I'm wasting my time.

    Thanks for the reply.

    >-- bruce (sqlwork.com)
    >
    >"Alan Silver" <> wrote in message
    >news:kbQ$lsKW569CFwf$@nospamthankyou.spam...
    >> Hello,
    >>
    >> I have a placeholder, which gets a user control added in when the page
    >> first loads. This user control contains a dropdownlist, and I would like
    >> to get hold of the value of this drop down list form the calling page on
    >> postback. Can I do this? If I try and use FindControl on the placeholder,
    >> it can't find the dropdownlist.
    >>
    >> I tried...
    >>
    >> ShowCategory ctlShowCategory =
    >> (ShowCategory)plcCategoryDetails.Controls[0];
    >>
    >> (where ShowCategory is the user control) but this gave the error...
    >>
    >> System.ArgumentOutOfRangeException: Specified argument was out of the
    >> range of valid values. Parameter name: index
    >>
    >> which sounds like the user control wasn't preserved in the placeholder
    >> over the postback.
    >>
    >> Any ideas how I do this? Hope this was clear!! TIA
    >>
    >> --
    >> Alan Silver
    >> (anything added below this line is nothing to do with me)

    >
    >


    --
    Alan Silver
    (anything added below this line is nothing to do with me)
    Alan Silver, Aug 8, 2005
    #3
  4. "Alan Silver" wrote:

    > >you have to recreate the dynamic comtrol on postback. there is no code in
    > >the placeholder for this.

    >
    > If I recreate the user control, will the child controls have their
    > values preserved?


    Yes and no, depending on which stage of the page's life cycle the child
    controls were created.

    The following article on the MSDN is very useful in understanding the
    ASP.NET View State:
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/viewstate.asp

    Phillip Williams
    http://www.societopia.net
    http://www.webswapp.com
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=, Aug 8, 2005
    #4
  5. Alan Silver

    Alan Silver Guest

    >> If I recreate the user control, will the child controls have their
    >> values preserved?

    >
    >Yes and no, depending on which stage of the page's life cycle the child
    >controls were created.
    >
    >The following article on the MSDN is very useful in understanding the
    >ASP.NET View State:
    >http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/
    >html/viewstate.asp


    Thanks, that's my bedtime reading sorted out tonight then!!

    --
    Alan Silver
    (anything added below this line is nothing to do with me)
    Alan Silver, Aug 8, 2005
    #5
  6. Alan Silver

    Bruce Barker Guest

    as long as you recreate all dynamic controls with the same name used on the
    render, before page load, then they will receive their postback data. don't
    overwrite their values on a postback.

    its best to recreate dynamic controls in oninit()

    -- bruce (sqlwork.com)



    "Alan Silver" <> wrote in message
    news:...
    > >you have to recreate the dynamic comtrol on postback. there is no code in
    >>the placeholder for this.

    >
    > If I recreate the user control, will the child controls have their values
    > preserved? The point of this is that I want to get the value of a
    > dropdownlist in the user control. If the value of that dropdownlist isn't
    > preserved (ie if recreating the user control resets the child controls),
    > then I'm wasting my time.
    >
    > Thanks for the reply.
    >
    >>-- bruce (sqlwork.com)
    >>
    >>"Alan Silver" <> wrote in message
    >>news:kbQ$lsKW569CFwf$@nospamthankyou.spam...
    >>> Hello,
    >>>
    >>> I have a placeholder, which gets a user control added in when the page
    >>> first loads. This user control contains a dropdownlist, and I would like
    >>> to get hold of the value of this drop down list form the calling page on
    >>> postback. Can I do this? If I try and use FindControl on the
    >>> placeholder,
    >>> it can't find the dropdownlist.
    >>>
    >>> I tried...
    >>>
    >>> ShowCategory ctlShowCategory =
    >>> (ShowCategory)plcCategoryDetails.Controls[0];
    >>>
    >>> (where ShowCategory is the user control) but this gave the error...
    >>>
    >>> System.ArgumentOutOfRangeException: Specified argument was out of the
    >>> range of valid values. Parameter name: index
    >>>
    >>> which sounds like the user control wasn't preserved in the placeholder
    >>> over the postback.
    >>>
    >>> Any ideas how I do this? Hope this was clear!! TIA
    >>>
    >>> --
    >>> Alan Silver
    >>> (anything added below this line is nothing to do with me)

    >>
    >>

    >
    > --
    > Alan Silver
    > (anything added below this line is nothing to do with me)
    Bruce Barker, Aug 9, 2005
    #6
  7. Alan Silver

    Alan Silver Guest

    >as long as you recreate all dynamic controls with the same name used on the
    >render, before page load, then they will receive their postback data. don't
    >overwrite their values on a postback.
    >
    >its best to recreate dynamic controls in oninit()


    Thanks

    >-- bruce (sqlwork.com)
    >
    >
    >
    >"Alan Silver" <> wrote in message
    >news:...
    >> >you have to recreate the dynamic comtrol on postback. there is no code in
    >>>the placeholder for this.

    >>
    >> If I recreate the user control, will the child controls have their values
    >> preserved? The point of this is that I want to get the value of a
    >> dropdownlist in the user control. If the value of that dropdownlist isn't
    >> preserved (ie if recreating the user control resets the child controls),
    >> then I'm wasting my time.
    >>
    >> Thanks for the reply.
    >>
    >>>-- bruce (sqlwork.com)
    >>>
    >>>"Alan Silver" <> wrote in message
    >>>news:kbQ$lsKW569CFwf$@nospamthankyou.spam...
    >>>> Hello,
    >>>>
    >>>> I have a placeholder, which gets a user control added in when the page
    >>>> first loads. This user control contains a dropdownlist, and I would like
    >>>> to get hold of the value of this drop down list form the calling page on
    >>>> postback. Can I do this? If I try and use FindControl on the
    >>>> placeholder,
    >>>> it can't find the dropdownlist.
    >>>>
    >>>> I tried...
    >>>>
    >>>> ShowCategory ctlShowCategory =
    >>>> (ShowCategory)plcCategoryDetails.Controls[0];
    >>>>
    >>>> (where ShowCategory is the user control) but this gave the error...
    >>>>
    >>>> System.ArgumentOutOfRangeException: Specified argument was out of the
    >>>> range of valid values. Parameter name: index
    >>>>
    >>>> which sounds like the user control wasn't preserved in the placeholder
    >>>> over the postback.
    >>>>
    >>>> Any ideas how I do this? Hope this was clear!! TIA
    >>>>
    >>>> --
    >>>> Alan Silver
    >>>> (anything added below this line is nothing to do with me)
    >>>
    >>>

    >>
    >> --
    >> Alan Silver
    >> (anything added below this line is nothing to do with me)

    >
    >


    --
    Alan Silver
    (anything added below this line is nothing to do with me)
    Alan Silver, Aug 9, 2005
    #7
    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. Peter Jaffe
    Replies:
    0
    Views:
    1,183
    Peter Jaffe
    Feb 4, 2004
  2. Oleg Ogurok
    Replies:
    1
    Views:
    406
    Teemu Keiski
    Jun 23, 2004
  3. =?Utf-8?B?Q2hyaXN0aWFuIE51bmNpYXRv?=

    Confusion Over Postback, ViewState and Dynamically Added Controls

    =?Utf-8?B?Q2hyaXN0aWFuIE51bmNpYXRv?=, May 3, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    2,330
    Brock Allen
    May 3, 2005
  4. =?Utf-8?B?V291dGVy?=

    Dynamically Loaded controls loading other controls

    =?Utf-8?B?V291dGVy?=, Feb 27, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    486
    =?Utf-8?B?V291dGVy?=
    Feb 27, 2006
  5. sonic_soul
    Replies:
    2
    Views:
    116
    Himanshu Dhami
    Aug 14, 2003
Loading...

Share This Page