?
Solved

JavaScript modifications not restored by ViewState

Posted on 2005-03-07
9
Medium Priority
?
399 Views
Last Modified: 2008-01-09
Hi All,

On a particular client action i have to put in a value (say "BLANK" in the text box) and disable it. As my textbox is dynamically generated I need to create it and initialise it on page_init.

Now if I modify the value and does not disable the textbox, the value is correctly restored after the page_init, where as if I disable it the value is not restored (the default value is taken).

can any one help...

DefaultUser


0
Comment
Question by:DefaultUser
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
9 Comments
 
LVL 17

Expert Comment

by:Jesse Houwing
ID: 13476156
Disabled textboxes are not posted back to the server by a webbrowser.  

You should make the value read-only and use style properties to make it appear like it's disabled if needed.
0
 
LVL 17

Expert Comment

by:Jesse Houwing
ID: 13476208
You can also disable the textboxes when the javascript unload event triggers, the request has been sent to the brower at that point if I'm not mistaking.
0
 
LVL 1

Author Comment

by:DefaultUser
ID: 13476461
ToAoM, can you please elaborate more over this:
                        Disabled textboxes are not posted back to the server by a webbrowser.

- I cann't use style or make it appear like disabled, need it Disabled, as a lot of other functionality depends upon the text box being disabled.
- Cann't disable the textbox at unload also (though, it didn't solved the problem any how), as we need it disabled in reponse to a certain even.

I'm posting the code for a sample application, that should make things more clear:

[CODE]

WebForm1.aspx.vb
----------------------

Public Class WebForm1
   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
   Protected WithEvents Button1 As System.Web.UI.WebControls.Button

   '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()
      If IsPostBack Then
         'Put user code to initialize the page here
         Dim x As New TextBox
         x.ID = "NewTextBox"
         x.Text = "Page_Init"
         x.Attributes.Add("onClick", "this.value='Disabled';this.disabled=true;")
         Response.Write("<B>In Page_Init : " & x.Text & "</B><br>")
         Me.Controls(1).Controls.Add(x)
      End If
   End Sub

#End Region

   Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

      If Not IsPostBack Then
         'Put user code to initialize the page here
         Dim x As New TextBox
         x.ID = "NewTextBox"
         x.Text = "Page_Load"
         x.Attributes.Add("onClick", "this.value='PageLoadDisabled';this.disabled=true;")
         Me.Controls(1).Controls.Add(x)
      End If
      Dim y As TextBox
      y = CType(Me.Controls(1).FindControl("NewTextBox"), TextBox)
      Response.Write("<br><B>In Page_Load : " & y.Text & "</B><br>")
   End Sub

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      'If the value is changed by JScript and the button disabled afterwords, value is not
      'reloaded from the view state; otherwise it's loaded
      Dim y As TextBox
      y = CType(Me.Controls(1).FindControl("NewTextBox"), TextBox)
      Response.Write("<br><B>In Button1_Click : " & y.Text & "</B><br>")
   End Sub
End Class


WebForm1.aspx
-------------------

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="WebApplication1.WebForm1"%>
<!doctype HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
  <head>
    <title>WebForm1</title>
<meta content="Microsoft Visual Studio .NET 7.1" name=GENERATOR>
<meta content="Visual Basic .NET 7.1" name=CODE_LANGUAGE>
<meta content=JavaScript name=vs_defaultClientScript>
<meta content=http://schemas.microsoft.com/intellisense/ie5 name=vs_targetSchema>
  </head>
   <body MS_POSITIONING="GridLayout">
      <form id=Form1 method=post runat="server">
         <asp:button id=Button1 style="Z-INDEX: 101; LEFT: 184px; POSITION: absolute; TOP: 216px" runat="server" Text="Button"></asp:button>
      </FORM>
  </body>
</html>

[/Code]
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 17

Expert Comment

by:Jesse Houwing
ID: 13476596
If you set a textbox to disabled, the client (webbrowser of the user) will think it isn't important to send it over (since the user can't change it's value).

If you need the changed value at any cost, you could add a <input type="hidden" runat="server" id="sameasinput_hidden"> to the page and whenever the textbox on the screen changes automatically change the value in the hidden field using the javascript onChange event. That way you always have access to the value through the hidden field.

It isn't the most elegant solution, but it will probably work.
0
 
LVL 1

Author Comment

by:DefaultUser
ID: 13476677
Ya that's a work around, but problem is, i have to create these text boxes at run time dynamically (probably around 50-60 per page), and this text box would be accessed by atleast 3 different modules, so adding a hidden type would require a lot of changes. Also i need it disabled so that user cann't enter any thing and no event is triggered for that text box..

BTW the value is already being changed and then the text box is disabled so the browser cann't assume we don't need its changed value.

Now is there any way we can force the client to always send the value back, wether it _thinks_ its important or not. I need the value back without making any change to the functionality. :'-(

- Default User
0
 
LVL 17

Accepted Solution

by:
Jesse Houwing earned 1500 total points
ID: 13476683
Just to be more clear, the following fields are posted back by a webbrowser:

- hidden fields
- visible field that are enabled.

The readonly property of a field has no impact on whether it is posted back by the client.
0
 
LVL 17

Expert Comment

by:Jesse Houwing
ID: 13477753
You could re-enable just before you post them back to the server, other than that, there is no way to force the browser to post the values. It's specified as such in the standards.
0
 
LVL 1

Author Comment

by:DefaultUser
ID: 13477791
Thanks...I think I got it...
thanks a lot dear....will just verify your suggestions and then accept your response :)
thanks a lot....

-DefaultUser
0
 
LVL 17

Expert Comment

by:Jesse Houwing
ID: 13478052
You're very welcome!
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question