2014年7月13日 星期日

[RESOLVED] form controls are not saving the values


I have a form that has calendar control and text area control.


When I save the form as to be completed later, I can read the value from controls and save it onto the database.


Then I reload the form later to complete the remaining values, this time , I add to the text of the textarea and select different dates on the calendar.


Then I call the save method again that reads values of all the controls.


It still reads the old values and do not consider the changes to the controls.


What am I doing wrong.


Please help.



Use Update Statement. Make sure the id is correct.

Could you please post your code aspx and aspx.cs bothe?



Using an update or insert statement comes at a later point 


I open page abc.aspx and fill in the values in text box and calendar


string s = txtabc.text


date d = cldabc.selecteddate


Then I call SaveForm function ()


Then I reopen the same page abc.aspx


change the date and change the text and click Submit button again


string s = txtabc.text


date d = cldabc.selecteddate


At this point txtabc.text and cldabc.selecteddate are still the old values, they do not change when I enter new values in it. 


Is there any event that needs to triggered .... like on textbox click/select/value changed


How does the control reads the new changed value.





hi nikhssssssss,


Can you post the HTML markup ? in order to help us to understand your issue better.


i suggest you try to use updatepanel control or use session to store .


 


 


 


 



You bind the form data in


if (!ispostback)


{


}



same data will be saved if you  dont put bind data in if (!ispostback)



Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Imports System.Globalization

Partial Class OverPaymentCode_Unprocessed
Inherits System.Web.UI.Page


Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load


strAssignmentNo = Request.QueryString("AssignmentNo")
FormType = Request.QueryString("Type")
If FormType Is Nothing Then
Using clsEA500 As New EA500Data
Using clsOPData As New OPData()
Dim dt As New DataTable
dt = clsEA500.GetEA500Data(strAssignmentNo)
If dt.Rows.Count > 0 Then
For Each dr As DataRow In dt.Rows
clsOPData.FullName = dr.Item("FullName")
If CInt(dr.Item("Amount")) < 0 Then
clsOPData.Tax = Math.Abs(dr.Item("Amount") + clsOPData.Tax)
Else
clsOPData.GrossAmount = dr.Item("Amount") + clsOPData.GrossAmount
End If
clsOPData.In500Period = dr.Item("In500Period")
Next
End If
clsOPData.Nett = clsOPData.GrossAmount - clsOPData.Tax
clsOPData.GetEmployeeDetails(strAssignmentNo)

' populate the form with the values gathered
lblEmployeeName.Text = "Overpayments for: " + clsOPData.FullName + " - " + strAssignmentNo
lblAssignmentNo.Text = strAssignmentNo
lblEmpName.Text = clsOPData.FullName
lblIN500.Text = clsOPData.In500Period
lblLHD.Text = clsOPData.LHD
lblPayCycle.Text = clsOPData.PayCycle
lblGross.Text = clsOPData.GrossAmount
lblTax.Text = clsOPData.Tax
lblNett.Text = clsOPData.Nett
End Using
End Using
ElseIf FormType = "incomplete" Then
LoadOPData()
ElseIf FormType = "review" Then
LoadOPData()
ElseIf FormType = "complete" Then
LoadOPData()
End If
End Sub

Private Sub LoadOPData()
'Loads the OP record when the record is being reviewed
If Not IsPostBack Then
Using clsOPData As New OPData(strAssignmentNo)
lblEmployeeName.Text = "Overpayments for: " + clsOPData.FullName + " - " + strAssignmentNo
lblAssignmentNo.Text = strAssignmentNo
lblEmpName.Text = clsOPData.FullName
lblIN500.Text = clsOPData.In500Period
lblLHD.Text = clsOPData.LHD
lblPayCycle.Text = clsOPData.PayCycle
lblGross.Text = clsOPData.GrossAmount
lblTax.Text = clsOPData.Tax
lblNett.Text = clsOPData.Nett
txtOPDateFrom.Text = clsOPData.OPFrom
txtOPDateTo.Text = clsOPData.OPTo
dplReasonCode.SelectedValue = clsOPData.ReasonCode
txtDescription.Value = clsOPData.Description
chkEIT.Checked = clsOPData.EITNoted
chkIN500.Checked = clsOPData.ReasonCodeIn500
chkPaid.Checked = clsOPData.Paid
ReadFiles(clsOPData.OPID)
End Using
End If
End Sub



Protected Function SaveFormValues() As Boolean
Dim recordID As Integer
Dim success As Boolean = False
Try
If (IsPostBack) Then
Using clsOPData As New OPData()
clsOPData.AssignmentNo = lblAssignmentNo.Text
clsOPData.FullName = lblEmpName.Text
clsOPData.In500Period = lblIN500.Text
clsOPData.LHD = lblLHD.Text
clsOPData.PayCycle = lblPayCycle.Text
clsOPData.GrossAmount = lblGross.Text
clsOPData.Tax = lblTax.Text
clsOPData.Nett = lblNett.Text
clsOPData.OPFrom = DateTime.ParseExact(txtOPDateFrom.Text, "MM/dd/yyyy", CultureInfo.InvariantCulture).ToString("dd/MM/yyyy", CultureInfo.InvariantCulture)
clsOPData.OPTo = DateTime.ParseExact(txtOPDateTo.Text, "MM/dd/yyyy", CultureInfo.InvariantCulture).ToString("dd/MM/yyyy", CultureInfo.InvariantCulture)
clsOPData.ReasonCode = dplReasonCode.SelectedValue
clsOPData.Description = txtDescription.Value
clsOPData.EITNoted = chkEIT.Checked
clsOPData.ReasonCodeIn500 = chkIN500.Checked
clsOPData.Paid = chkPaid.Checked
clsOPData.Status = s
recordID = clsOPData.UploadOPData()
End Using

If supportingFiles1.HasFile Then
SaveFiles(supportingFiles1.PostedFile, recordID)
End If

If supportingFiles2.HasFile Then
SaveFiles(supportingFiles2.PostedFile, recordID)
End If

If supportingFiles3.HasFile Then
SaveFiles(supportingFiles3.PostedFile, recordID)
End If

If recordID > 0 Then
DeleteEA500Record()
End If
Return success
End If
Catch ex As Exception
Return success
End Try
End Function



Protected Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
s = Status.Incomplete
SaveFormValues()
Response.Redirect("overpayment.aspx")
End Sub


Protected Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
Response.Redirect("overpayment.aspx")
End Sub

Protected Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
Select Case FormType
Case Nothing
s = Status.ToBeReviewed
SaveFormValues()
Case "review"
s = Status.Complete
UpdateStatus()
Case "incomplete"
s = Status.ToBeReviewed
SaveFormValues()
End Select
Response.Redirect("overpayment.aspx")
End Sub




End Class

Please find the code of my aspx.vb page.


The function that needs to be looked is SaveFormValues(). When the form is loaded for the first time and I call the function SaveFormValues() correct values are saved, when I reopen the same record and form loads the controls with the values from the database
using LoadOPData(), I make changes to the controls and it is not able to save the new values. WHen I read txtDescription.Text = it gives the original value that was loaded with the form's page load event.


How to fix this please ?




hi nikhssssssss,


I readed your code, i found your issue.


Your LoadOPData() method contains Not IsPostBack,so dplReasonCode.SelectedValue, txtDescription.Value etc. value always the original value(it is always the values when the page is first loaded).


Solution:


1.You only need to reset value for your controls after you excute every SaveFormValues().


2.You alse can leave the page after you excute every SaveFormValues(), and then go back the page again.




What does that mean ?


1. If not Postback LoadOPDate() loads the data from the database


2. Then I change the value of controls and call SaveFormValues()


this Save form values should save the changed values, you are asking me to rest the values after I call SaveFormValues() method. How will it help me ?



hi nikhssssssss,


As Happy said,  when not Postback LoadOPDate() loads the data from the database, the first page load  dplReasonCode.SelectedValue, txtDescription.Value etc. value always the original value.


i suggest you try to check(debug) the code below:


strAssignmentNo = Request.QueryString("AssignmentNo")

Using clsOPData As New OPData(strAssignmentNo)


you need to ensure strAssignmentNo and clsOPData  object correctly.


 how do you  define strAssignmentNo and OPData class .


沒有留言:

張貼留言