By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 764 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

ViewState for Enabled Property on CheckBox/RadioButton Controls

P: n/a
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
Nov 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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" <le******@fmr.com> wrote in message
news:b6**************************@posting.google.c om...
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

Nov 18 '05 #2

P: n/a
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!
Nov 18 '05 #3

P: n/a
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" <le******@fmr.com> wrote in message
news:OD**************@tk2msftngp13.phx.gbl...
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!

Nov 18 '05 #4

P: n/a
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" <le******@fmr.com> wrote in message
news:OD**************@tk2msftngp13.phx.gbl...
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!

Nov 18 '05 #5

P: n/a
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!
Nov 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.