ViewState for Enabled Property on CheckBox/RadioButton Controls

L

Leo J. Hart IV

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
 
H

Hermit Dave

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)
 
L

Leo J. Hart IV

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
 
H

Hermit Dave

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)
 
L

Leo J. Hart IV

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top