|
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â€
|
|
|
|
|
|
|
|