codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  JavaScript modifications to a textbox not restored by ViewState  InitialUser at 10:06 on Monday, March 07, 2005
 

Hi All,

My aim is to:
• Create a textbox at first pageload and fill in some value (basically from database)
• Empty the textbox, and disable it from client side script, on certain event. Otherwise let the user enter value into the textbox.
• On the click event of the submit button I have to save this value (either the value entered by the user; or the empty value) back to the database.

What I’m doing:
• Creating the textbox on first pageload; and filling the value.
• On postback I create the textbox in page_init so that the view state can be initialized for the textbox before the page_load (here also I have to assign the value)
• On click event of the textbox, it should get the value ‘Disabled’

Problem I’m facing is:
• If I change the value of the text box and don’t disable it; its correctly restored after the init method from the view state
• But if I disable the textbox from java script; the value is not restored from viewstate. And the value that I initialize the text box with is retained.

Can any one tell me why exactly this is happening and whats the soln. to it.

The code for the same is given below :


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 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">
</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>



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
Response.Write("<B>In Page_Load : " & CType(Me.Controls(1).Controls(2), TextBox).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
CType(Me.Controls(1).Controls(2), TextBox).Enabled = True
Response.Write("<B>In Button1_Click : " & CType(Me.Controls(1).Controls(2), TextBox).Text & "</B><br>")
End Sub
End Class



Thanks a lot in advance,
Prateek Jaiswal

“deserve before you desireâ€










CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  Re: C# and EXCEL question saving a file saving the the first column as read only
•  Iexplore -
•  Iframe and Div Layer Scrollbar Problem
•  Left margin increases as page fills.....
•  Re: Parse error that is slowly driving me insane
•  simple javascript/dhtml drop-down menu
•  notify an object that the user interface has changed one of its properties
•  Re: Print and print preview file on the website without using the File - Print on the IE
•  how we convert image into byte array in rmi


Recent Articles
Communicating with the Database (Using ADO)
MagicGrid
Simple Thumbnail Browsing Solution
Type Anywhere
A Better Moustrap: FmtDate to replace FormatDateTime
ASP.NET Forum Source Code
Internal Search Engine
Javascript Growing Window
Simple date validation
Search engine friendly URLs using ASP.NET (C#.NET)


Site Survey
Help us serve you better. Take a five minute survey. Click here!

© Copyright codetoad.com 2001-2005