ViewState for Enabled Property on CheckBox/RadioButton Controls

Discussion in 'ASP .Net' started by Leo J. Hart IV, Oct 9, 2004.

  1. Hello,

    I'm hoping someone can help me out.

    I was wondering if the Enabled property of a CheckBox or RadioButton
    server control is stored in the ViewState. If not, is there some way
    to to add this property to ViewState? If needed, I can give you more
    info on exactly what I'm trying to do.

    Thanks,
    Leo Hart
     
    Leo J. Hart IV, Oct 9, 2004
    #1
    1. Advertising

  2. Leo J. Hart IV

    Hermit Dave Guest

    information about the state of controls is saved in viewstate.
    so say during serverside processing if the control was marked disabled then
    during next postback it will know what the state was and unless you change
    it.. it will maintain the same.

    you can choose to add your own data to viewstate using somthing like

    ViewState["myprop1"] = true;

    and read it using something like

    if(ViewState["myprop1"] != null)
    {
    bool myProperty = (bool)ViewState["myprop1"];
    }

    --

    Regards,

    Hermit Dave
    (http://hdave.blogspot.com)
    "Leo J. Hart IV" <> wrote in message
    news:...
    > Hello,
    >
    > I'm hoping someone can help me out.
    >
    > I was wondering if the Enabled property of a CheckBox or RadioButton
    > server control is stored in the ViewState. If not, is there some way
    > to to add this property to ViewState? If needed, I can give you more
    > info on exactly what I'm trying to do.
    >
    > Thanks,
    > Leo Hart
     
    Hermit Dave, Oct 9, 2004
    #2
    1. Advertising

  3. But it looks like the enabled property is not saved. For instance, I
    have a page with a checkbox, a textbox and a button on it. When a user
    clicks the checkbox off, a javascript function is executed that disables
    the textbox. When a user clicks the button, the page is submitted and
    the user is returned to the page.

    Here's the user test scenario I used: I change the value contained
    within the textbox, I click off the checkbox, disabling the text box. I
    click submit.

    What I expect to see when the page is brought back is an unchecked
    checkbox and a disabled textbox with the changed text in it. What I
    actually get is an unchecked checkbox and an ENABLED textbox with the
    original text in it. It looks like the enabled property is not saved on
    a control and, if a control is disabled, all other properties are not
    saved.

    Here's my aspx file:

    <%@ Page Language="vb" AutoEventWireup="false"
    Codebehind="ControlProperties.aspx.vb"
    Inherits="Sandbox.Web.ControlProperties"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    <head>
    <title>ControlProperties</title>
    <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema"
    content="http://schemas.microsoft.com/intellisense/ie5">
    <script language="javascript">
    <!--
    function EnableDisableTextBox(){
    var lTextBox = document.getElementById("TxtTextBox");

    lTextBox.disabled = !(lTextBox.disabled);
    }
    //-->
    </script>
    </head>
    <body MS_POSITIONING="GridLayout">
    <form id="MyForm" method="post" runat="server">
    <asp:checkbox ID="ChkCheckBox" onclick="EnableDisableTextBox();"
    runat="server"
    EnableViewState="true"/>&nbsp;&nbsp;&nbsp;&nbsp;<asp:textbox
    ID="TxtTextBox" Text="This is some text" Runat="server"
    EnableViewState="true"/>
    <asp:button ID="BtnSubmit" text="Submit" Runat="server"/>
    </form>
    </body>
    </html>

    Here's my aspx.vb file:

    Public Class ControlProperties
    Inherits System.Web.UI.Page

    #Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub
    InitializeComponent()

    End Sub

    'NOTE: The following placeholder declaration is required by
    the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e
    As System.EventArgs) Handles MyBase.Init
    'CODEGEN: This method call is required by the Web Form
    Designer
    'Do not modify it using the code editor.
    InitializeComponent()
    End Sub

    #End Region

    Protected WithEvents ChkCheckBox As
    System.Web.UI.WebControls.CheckBox
    Protected WithEvents TxtTextBox As
    System.Web.UI.WebControls.TextBox
    Protected WithEvents BtnSubmit As
    System.Web.UI.WebControls.Button


    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    'Put user code to initialize the page here
    End Sub

    Private Sub BtnSubmit_Click(ByVal sender As System.Object,
    ByVal e As System.EventArgs) Handles BtnSubmit.Click

    End Sub

    End Class

    Is there any way to change this behaviour so that I can save the enabled
    property in the view state, as well as maintain the other properties in
    the viewstate if the enabled property is set to false?

    Thanks,
    Leo Hart




    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    Leo J. Hart IV, Oct 9, 2004
    #3
  4. Leo J. Hart IV

    Hermit Dave Guest

    The state information of the textbox was the state on serverside and not
    clientside
    when you used javascript you only disabled it on clientside. the
    stateinformation has no idea about it.

    if however you set the textbox as disabled from server side then it will be
    maintain on everypostback unless you explicitly change it.

    onclick >> javascript == client side effect (has no bearing on server side)

    instead of using javascript set the checkbox's autopostback to true and
    implement checkbox's onclick as serverside event handler. and then set the
    textbox to disabled.

    --

    Regards,

    Hermit Dave
    (http://hdave.blogspot.com)
    "Leo J. Hart IV" <> wrote in message
    news:...
    > But it looks like the enabled property is not saved. For instance, I
    > have a page with a checkbox, a textbox and a button on it. When a user
    > clicks the checkbox off, a javascript function is executed that disables
    > the textbox. When a user clicks the button, the page is submitted and
    > the user is returned to the page.
    >
    > Here's the user test scenario I used: I change the value contained
    > within the textbox, I click off the checkbox, disabling the text box. I
    > click submit.
    >
    > What I expect to see when the page is brought back is an unchecked
    > checkbox and a disabled textbox with the changed text in it. What I
    > actually get is an unchecked checkbox and an ENABLED textbox with the
    > original text in it. It looks like the enabled property is not saved on
    > a control and, if a control is disabled, all other properties are not
    > saved.
    >
    > Here's my aspx file:
    >
    > <%@ Page Language="vb" AutoEventWireup="false"
    > Codebehind="ControlProperties.aspx.vb"
    > Inherits="Sandbox.Web.ControlProperties"%>
    > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    > <html>
    > <head>
    > <title>ControlProperties</title>
    > <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
    > <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
    > <meta name="vs_defaultClientScript" content="JavaScript">
    > <meta name="vs_targetSchema"
    > content="http://schemas.microsoft.com/intellisense/ie5">
    > <script language="javascript">
    > <!--
    > function EnableDisableTextBox(){
    > var lTextBox = document.getElementById("TxtTextBox");
    >
    > lTextBox.disabled = !(lTextBox.disabled);
    > }
    > //-->
    > </script>
    > </head>
    > <body MS_POSITIONING="GridLayout">
    > <form id="MyForm" method="post" runat="server">
    > <asp:checkbox ID="ChkCheckBox" onclick="EnableDisableTextBox();"
    > runat="server"
    > EnableViewState="true"/>&nbsp;&nbsp;&nbsp;&nbsp;<asp:textbox
    > ID="TxtTextBox" Text="This is some text" Runat="server"
    > EnableViewState="true"/>
    > <asp:button ID="BtnSubmit" text="Submit" Runat="server"/>
    > </form>
    > </body>
    > </html>
    >
    > Here's my aspx.vb file:
    >
    > Public Class ControlProperties
    > Inherits System.Web.UI.Page
    >
    > #Region " Web Form Designer Generated Code "
    >
    > 'This call is required by the Web Form Designer.
    > <System.Diagnostics.DebuggerStepThrough()> Private Sub
    > InitializeComponent()
    >
    > End Sub
    >
    > 'NOTE: The following placeholder declaration is required by
    > the Web Form Designer.
    > 'Do not delete or move it.
    > Private designerPlaceholderDeclaration As System.Object
    >
    > Private Sub Page_Init(ByVal sender As System.Object, ByVal e
    > As System.EventArgs) Handles MyBase.Init
    > 'CODEGEN: This method call is required by the Web Form
    > Designer
    > 'Do not modify it using the code editor.
    > InitializeComponent()
    > End Sub
    >
    > #End Region
    >
    > Protected WithEvents ChkCheckBox As
    > System.Web.UI.WebControls.CheckBox
    > Protected WithEvents TxtTextBox As
    > System.Web.UI.WebControls.TextBox
    > Protected WithEvents BtnSubmit As
    > System.Web.UI.WebControls.Button
    >
    >
    > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles MyBase.Load
    > 'Put user code to initialize the page here
    > End Sub
    >
    > Private Sub BtnSubmit_Click(ByVal sender As System.Object,
    > ByVal e As System.EventArgs) Handles BtnSubmit.Click
    >
    > End Sub
    >
    > End Class
    >
    > Is there any way to change this behaviour so that I can save the enabled
    > property in the view state, as well as maintain the other properties in
    > the viewstate if the enabled property is set to false?
    >
    > Thanks,
    > Leo Hart
    >
    >
    >
    >
    > *** Sent via Developersdex http://www.developersdex.com ***
    > Don't just participate in USENET...get rewarded for it!
     
    Hermit Dave, Oct 9, 2004
    #4
  5. Leo J. Hart IV

    Hermit Dave Guest

    or have a onload javascript which essentially marks the textbox enabled or
    not depending on whether checkbox is checked or not

    --

    Regards,

    Hermit Dave
    (http://hdave.blogspot.com)
    "Leo J. Hart IV" <> wrote in message
    news:...
    > But it looks like the enabled property is not saved. For instance, I
    > have a page with a checkbox, a textbox and a button on it. When a user
    > clicks the checkbox off, a javascript function is executed that disables
    > the textbox. When a user clicks the button, the page is submitted and
    > the user is returned to the page.
    >
    > Here's the user test scenario I used: I change the value contained
    > within the textbox, I click off the checkbox, disabling the text box. I
    > click submit.
    >
    > What I expect to see when the page is brought back is an unchecked
    > checkbox and a disabled textbox with the changed text in it. What I
    > actually get is an unchecked checkbox and an ENABLED textbox with the
    > original text in it. It looks like the enabled property is not saved on
    > a control and, if a control is disabled, all other properties are not
    > saved.
    >
    > Here's my aspx file:
    >
    > <%@ Page Language="vb" AutoEventWireup="false"
    > Codebehind="ControlProperties.aspx.vb"
    > Inherits="Sandbox.Web.ControlProperties"%>
    > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    > <html>
    > <head>
    > <title>ControlProperties</title>
    > <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
    > <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
    > <meta name="vs_defaultClientScript" content="JavaScript">
    > <meta name="vs_targetSchema"
    > content="http://schemas.microsoft.com/intellisense/ie5">
    > <script language="javascript">
    > <!--
    > function EnableDisableTextBox(){
    > var lTextBox = document.getElementById("TxtTextBox");
    >
    > lTextBox.disabled = !(lTextBox.disabled);
    > }
    > //-->
    > </script>
    > </head>
    > <body MS_POSITIONING="GridLayout">
    > <form id="MyForm" method="post" runat="server">
    > <asp:checkbox ID="ChkCheckBox" onclick="EnableDisableTextBox();"
    > runat="server"
    > EnableViewState="true"/>&nbsp;&nbsp;&nbsp;&nbsp;<asp:textbox
    > ID="TxtTextBox" Text="This is some text" Runat="server"
    > EnableViewState="true"/>
    > <asp:button ID="BtnSubmit" text="Submit" Runat="server"/>
    > </form>
    > </body>
    > </html>
    >
    > Here's my aspx.vb file:
    >
    > Public Class ControlProperties
    > Inherits System.Web.UI.Page
    >
    > #Region " Web Form Designer Generated Code "
    >
    > 'This call is required by the Web Form Designer.
    > <System.Diagnostics.DebuggerStepThrough()> Private Sub
    > InitializeComponent()
    >
    > End Sub
    >
    > 'NOTE: The following placeholder declaration is required by
    > the Web Form Designer.
    > 'Do not delete or move it.
    > Private designerPlaceholderDeclaration As System.Object
    >
    > Private Sub Page_Init(ByVal sender As System.Object, ByVal e
    > As System.EventArgs) Handles MyBase.Init
    > 'CODEGEN: This method call is required by the Web Form
    > Designer
    > 'Do not modify it using the code editor.
    > InitializeComponent()
    > End Sub
    >
    > #End Region
    >
    > Protected WithEvents ChkCheckBox As
    > System.Web.UI.WebControls.CheckBox
    > Protected WithEvents TxtTextBox As
    > System.Web.UI.WebControls.TextBox
    > Protected WithEvents BtnSubmit As
    > System.Web.UI.WebControls.Button
    >
    >
    > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles MyBase.Load
    > 'Put user code to initialize the page here
    > End Sub
    >
    > Private Sub BtnSubmit_Click(ByVal sender As System.Object,
    > ByVal e As System.EventArgs) Handles BtnSubmit.Click
    >
    > End Sub
    >
    > End Class
    >
    > Is there any way to change this behaviour so that I can save the enabled
    > property in the view state, as well as maintain the other properties in
    > the viewstate if the enabled property is set to false?
    >
    > Thanks,
    > Leo Hart
    >
    >
    >
    >
    > *** Sent via Developersdex http://www.developersdex.com ***
    > Don't just participate in USENET...get rewarded for it!
     
    Hermit Dave, Oct 9, 2004
    #5
  6. Thanks for all of your help everyone! I went with the AutoPostBack
    approach and that worked perfectly. I actually have another WebForms
    question related to this project, but I'll start a new thread for that.

    Thanks again,
    Leo Hart



    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    Leo J. Hart IV, Oct 10, 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. Simon Brown
    Replies:
    1
    Views:
    4,347
    DWinter
    Aug 8, 2003
  2. Cipher
    Replies:
    3
    Views:
    1,523
    coollzh
    Apr 30, 2004
  3. Nathan Sokalski
    Replies:
    1
    Views:
    432
    Teemu Keiski
    Nov 11, 2007
  4. Andrew Backer

    Composite & issues with Viewstate/Enabled property

    Andrew Backer, Jul 18, 2005, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    165
    Andrew Backer
    Jul 19, 2005
  5. Nathan Sokalski

    RadioButton and CheckBox Controls: Where is the value attribute?

    Nathan Sokalski, Nov 10, 2007, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    245
    Teemu Keiski
    Nov 11, 2007
Loading...

Share This Page