Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6126109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:19:50+00:00 2026-05-23T16:19:50+00:00

Here’s the setup. I have an ASP.NET4 Page with viewstate enabled, with a RadioButtonList

  • 0

Here’s the setup. I have an ASP.NET4 Page with viewstate enabled, with a RadioButtonList with 2 radio buttons, a CheckBox, and an included UserControl. I want to hide the CheckBox if one radio button is selected, and show it if the other is selected. I want the CheckBox to maintain it’s state when it is hidden and re-shown; if it was checked before hiding, when it is shown again I still want it to be checked.

The twist is that the code to show/hide the CheckBox, based on what radio button is chosen, lives in the OnPreRender event of the UserControl. So it does Parent.FindControl()s to get the controls, then hides/shows the CheckBox based on the state of the RadioButtonList.

The issue is, when doing this logic in OnPreRender() of the UserControl, the checkbox does not maintain its state after it is hidden, then reshown. That is, if the checkbox is checked, then I click the radio button to hide it, when I click the other radio button I want the checkbox to still be checked when it is now shown. I have a feeling I’m not understanding some of the view state mechanisms, but when I tried adding a TextBox and hiding/showing it, it did maintain it’s text value as expected.

I can move the logic to Page_Load of the UserControl and the checkbox state behaves as expected. But I’m trying to get it to work in OnPreRender(), or at least looking for an explanation as to why I’m seeing this behavior.

In default.aspx:

 <asp:RadioButtonList runat="server" ID="uxRadio" AutoPostBack="true">
        <asp:ListItem Text="choice 1" Value="1"></asp:ListItem>
        <asp:ListItem Text="choice 2" Value="2"></asp:ListItem>
        <asp:ListItem Text="choice 3" Value="3"></asp:ListItem>
    </asp:RadioButtonList>
    <asp:CheckBox runat="server" ID="uxCheck" AutoPostBack="true" />
    <uc1:Control ID="uxControl" runat="server"></uc1:Control>

UserControl.ascx.cs:

protected override void OnPreRender(EventArgs e)
{
   RadioButtonList rb = (RadioButtonList)Parent.FindControl("uxRadio");
   CheckBox cb = (CheckBox)Parent.FindControl("uxCheck");

   if (rb.SelectedValue == "2")
   {
       cb.Visible = false;
   }
   else
   {
       cb.Visible = true;
   }
   base.OnPreRender(e);
 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:19:51+00:00Added an answer on May 23, 2026 at 4:19 pm

    Ok, i could reproduce this issue but i’m not 100% sure why the ViewState is not retained.

    I assume that something like this happens:

    ASP.NET retrieves the checked-state from CheckBoxes from the posted request but only if it’s Visible on serverside(otherwise it wouldn’t even be rendered as html). If it’s invisible it gets the checked-state from ViewState.

    I think that the UserControl’s PreRender is too late in this case. ASP.NET does not know that it has to save this value in ViewState because the condition(Visible-State) is changed afterwards. Hence there is no posted value and no ViewState value and ASP.NET must use the default-state(unchecked).

    If you move this code from PreRender to Page_Load it works.

    But i would strongly recommend to move it to the Page itself because it really belongs there. A usercontrol should not insist on the existence of specific controls in it’s page because it should also work in other pages.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's the basic setup: I have a thin bar at the top of a
Here is my code (Say we have a single button on the page that
Here's the problem....I have three components...A Page that contains a User Control and a
Here is my problem...I have a page that loads a list of clients and
Here's a problem I ran into recently. I have attributes strings of the form
Here is the issue I am having: I have a large query that needs
Here's my scenario - I have an SSIS job that depends on another prior
Here we go again, the old argument still arises... Would we better have a
Here is my problem : I have a post controller with the action create.
Here is an example: I have the generic type called Account. I wish to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.