Variables gone during grid insert command
-
Please help me.. I am trying to store variable in a class however during grid insert all variable gone. Below is my code
Dim StateTemplate As New StateTemplate
Private Sub btnLoad_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLoad.Click
Try
Dim StateTemplate As New StateTemplate
With StateTemplate
.YearCode = ddlPlanYears.SelectedValue
.State = ddlState.SelectedItem.ToString
End With
...
Private Sub rgExclusions_InsertCommand...
StateTemplate is empty here -
Please help me.. I am trying to store variable in a class however during grid insert all variable gone. Below is my code
Dim StateTemplate As New StateTemplate
Private Sub btnLoad_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLoad.Click
Try
Dim StateTemplate As New StateTemplate
With StateTemplate
.YearCode = ddlPlanYears.SelectedValue
.State = ddlState.SelectedItem.ToString
End With
...
Private Sub rgExclusions_InsertCommand...
StateTemplate is empty here -
byka wrote:
StateTemplate is empty here
Of course it is, because you used a different
StateTemplate
object within your subroutine. Delete the declaration between theTry
andWith
statements. -
I delete it. and it still not working
Private Sub btnLoad_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLoad.Click
With StateTemplate
.YearCode = ddlPlanYears.SelectedValue
.State = ddlState.SelectedItem.ToString
End With -
Where have you declared your
StateTemplate
object? What happens to it after you return from thebtnLoad_Click
event? Please expand on the expression "it still not working"; we cannot guess what that means. -
I'm sorry, but there is really no point in posting these vague statements and expecting us to guess what your application is doing. Show where the variable is set up , and where you try to use it, and explain the exact sequence of events that should make this happen. If you need to collect further information then use your debugger during a test run of your code.
-
I delete it. and it still not working
Private Sub btnLoad_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLoad.Click
With StateTemplate
.YearCode = ddlPlanYears.SelectedValue
.State = ddlState.SelectedItem.ToString
End WithI'm guessing that there is a post-back between the
btnLoad_Click
event handler and thergExclusions_InsertCommand
event handler? When an ASP.NET page posts back, the request is served by a new instance of the page class. Once the page has been rendered, that instance of the class is discarded, along with the value of any fields. That is why yourStateTemplate
field is not persisting between requests. If you want to save the values between requests, then you'll need to store them in ViewState: Beginner's Guide To View State [^] Understanding ASP.NET View State[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer