Enabled property conflicts with ViewState?

C

Cipher

I've built a ASP.Net webpage with a Textbox that is meant to hold a date
chosen from a JavaScript calendar on the page. The default value for this
control is handled during the page load event as follows...

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
txtDate.Text = Format(Date.Now, "Long date")
End If

End Sub

Since I don't want the user to manually enter a date (ie they should always
use the calendar control), we have disabled the TextBox by setting it's
Enabled property to false. Unfortunately, this seems to cause the ViewState
to fail for the control whenever the page is posted. Specifically, the
control is always reset to the value in the code snippet above. However, If
we enable the control, its state is saved after the page is posted!

Since we always want the TextBox to maintain its state, is there a way
around this?

Thank you.
 
B

bruce barker

if you disable a textbox, the browser will not post its value back, so the
server has no way of knowing the value. you need to code a hidden field that
the client script updates.

-- bruce (sqlwork.com)
 
T

terrorix

Cipher said:
I've built a ASP.Net webpage with a Textbox that is meant to hold a date
chosen from a JavaScript calendar on the page. The default value for this
control is handled during the page load event as follows...

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
txtDate.Text = Format(Date.Now, "Long date")
End If

End Sub

Since I don't want the user to manually enter a date (ie they should always
use the calendar control), we have disabled the TextBox by setting it's
Enabled property to false. Unfortunately, this seems to cause the ViewState
to fail for the control whenever the page is posted. Specifically, the
control is always reset to the value in the code snippet above. However, If
we enable the control, its state is saved after the page is posted!

Since we always want the TextBox to maintain its state, is there a way
around this?

Thank you.

You can replace EditBox with some Label, where the date will be displayed, and then users cannot enter date manually.
 
C

coollzh

this is not soa good way!
you can try txtDateTime.Attributes.Add("Disabled","true")

terrorix said:
You can replace EditBox with some Label, where the date will be displayed,
and then users cannot enter date manually.
 

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

Similar Threads

Property 1
Enabled property 1
Page error when viewstate of list controls enabled 1
Control Enabled property 1
Composite & issues with Viewstate/Enabled property 1
viewstate 0
Confused by ViewState 5
ViewState? 1

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top