Textbox values disappear on postback - asp.net

I have an asp.net page with a button that adds an additional row to a gridview for input to the database. The gridview consists of 3 textboxes(template fields), when I add a row the information already entered somehow disappears on the postback. I want the button to add additional rows without disspelling the data in the other rows, until I hit the submit button. Here's my code
Private Sub AddNewRowToGrid()
Dim rowIndex As Integer = 0
If ViewState("CurrentTable") IsNot Nothing Then
Dim dtCurrentTable As DataTable = DirectCast(ViewState("CurrentTable"), DataTable)
Dim drCurrentRow As DataRow = Nothing
If dtCurrentTable.Rows.Count > 0 Then
For i As Integer = 1 To dtCurrentTable.Rows.Count
'extract the TextBox values
Dim box1 As TextBox = DirectCast(gvOLIAdj.Rows(rowIndex).Cells(1).FindControl("txtAdjAppr"), TextBox)
Dim box2 As TextBox = DirectCast(gvOLIAdj.Rows(rowIndex).Cells(2).FindControl("txtAdjAmt"), TextBox)
Dim box3 As TextBox = DirectCast(gvOLIAdj.Rows(rowIndex).Cells(3).FindControl("txtCmmts"), TextBox)
drCurrentRow = dtCurrentTable.NewRow()
drCurrentRow("Approval Date") = box1.ToString
dtCurrentTable.Rows(i - 1)("Total Amount") = box2.ToString
dtCurrentTable.Rows(i - 1)("Comments") = box3.ToString
'dtCurrentTable.Rows(i - 1)("Initials") =
rowIndex += 1
Next
dtCurrentTable.Rows.Add(drCurrentRow)
ViewState("CurrentTable") = dtCurrentTable
gvOLIAdj.DataSource = dtCurrentTable
gvOLIAdj.DataBind()
End If
Else
Response.Write("ViewState is null")
End If
'Set Previous Data on Postbacks
'SetPreviousData()
End Sub
Private Sub SetPreviousData()
Dim rowIndex As Integer = 0
If ViewState("CurrentTable") IsNot Nothing Then
Dim dats As DataTable = DirectCast(ViewState("CurrentTable"), DataTable)
If dats.Rows.Count > 0 Then
For i As Integer = 0 To dats.Rows.Count - 1
Dim box1 As TextBox = DirectCast(gvOLIAdj.Rows(rowIndex).Cells(1).FindControl("txtAdjAppr"), TextBox)
Dim box2 As TextBox = DirectCast(gvOLIAdj.Rows(rowIndex).Cells(2).FindControl("txtAdjAmt"), TextBox)
Dim box3 As TextBox = DirectCast(gvOLIAdj.Rows(rowIndex).Cells(3).FindControl("txtCmmts"), TextBox)
box1.Text = dats.Rows(i)("Approval Date").ToString()
box2.Text = dats.Rows(i)("Total Amount").ToString()
box3.Text = dats.Rows(i)("Comments").ToString()
rowIndex += 1
Next
End If
End If
End Sub

I can think of two reasons why this may be happening:
When you create controls dynamically it is VERY difficult to
retrieve the values in the code behind after postback
Your controls have the read-only attribute set in the raw HTML
You may want to try old-fashioned methods such as Request.Form http://msdn.microsoft.com/en-us/library/ms525985%28v=vs.90%29.aspx

Related

Loop through checkbox ID's to see if any are checked

I have a ballot for a election system and I need to loop through all the checkboxes on the ballot to see if they are checked or unchecked. You can only select (for example) 1 of the 5 boxes available. I am stuck and can not for the life of me figure this out. The following code is my function that runs when the user clicks the submit button.
This code works and submit my ballot but does not check the number of checkboxes checked.
For Each row As Object In candidatesTable.Rows
If row(1) = ballot_ID Then
Dim checkBox_ID = row(0)
Dim CB As New CheckBox()
CB = mainBallotDiv.FindControl(checkBox_ID)
If CB.Checked Then
Dim addVote As Integer = row("votes")
addVote += 1
candidatesAdapter.addVoteToCandidate(addVote, row(0))
Dim section_ID As Integer = row(2)
Dim voter As String = userGnumber
Dim vote As Integer = checkBox_ID
Dim hasVoted As Boolean = True
votesAdapter.InsertVotes(ballot_ID, section_ID, voter, vote, hasVoted)
End If
End If
Next
Response.Redirect("~/voting/voted.aspx")
I have added a couple things to try and get this to run correctly but no luck, my code currently is the following.
Dim checkedCount As Integer
For Each row As Object In candidatesTable.Rows
If row(1) = ballot_ID Then
Dim checkBox_ID = row(0)
Dim CB As New CheckBox()
CB = mainBallotDiv.FindControl(checkBox_ID)
Dim section_idFromCB As Integer = candidatesAdapter.getsectionIDfromcandidateID(CB.ID)
Dim voteLimit As Integer = sectionsAdapter.votesbysectionid(section_idFromCB)
If CB.Checked Then
checkedCount += 1
Debug.Write(checkedCount)
If checkedCount > voteLimit Then
' error
Response.Write("<script language=""javascript"">alert('You can not select that many check boxes.');</script>")
Response.Redirect(Request.RawUrl)
Else
' pass
For Each Nrow As Object In candidatesTable.Rows
If Nrow(1) = ballot_ID Then
Dim NcheckBox_ID = row(0)
Dim NCB As New CheckBox()
NCB = mainBallotDiv.FindControl(NcheckBox_ID)
If NCB.Checked Then
Dim addVote As Integer = row("votes")
addVote += 1
candidatesAdapter.addVoteToCandidate(addVote, row(0))
Dim section_ID As Integer = row(2)
Dim voter As String = userGnumber
Dim vote As Integer = checkBox_ID
Dim hasVoted As Boolean = True
votesAdapter.InsertVotes(ballot_ID, section_ID, voter, vote, hasVoted)
End If
End If
Next
Response.Redirect("~/voting/voted.aspx")
End If
End If
End If
Next
Any help would be appreciated, and thanks in advance.
Here is my recommendation...
You can put them in a List(Of CheckBox) then you can access them anytime as needed as well as get any property you would need.
Dim lstChecked As New List(Of CheckBox)
lstChecked = divcontrol.Controls.OfType(Of CheckBox).Where(Function(ch) ch.Checked = True).ToList
lstChecked would be any CheckBox that would be checked...
For Each checkBox In Me.Controls.OfType(Of CheckBox)
' do something
Next

Dropdown not firing event on usercontrol

I have a dropdown list which is inside a user control on my page.
When I select an item in a gridview it fills some data including this dropdown list selecting the correspondent value to the gridview row.
Then I can change the selection, but when i try to do that the Selected Index Changed event isn't firing. At least not at first. If i select again it fires.
I'm using asp.net with VB.
I have put AutoPostback = true, I've set the Handles in the codebehind method, I've set a default empty line and set it to default selection, but I can't get it to work.
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
If Page.IsPostBack() Then
fillProd()
Dim cert As String = Session("var")
isFilled = Session("isFilled")
If Not IsNothing(cert) And Not isFilled Then
FillFields(cert)
End If
End If
End Sub
Private Sub fillProd()
dt = HttpContext.Current.Session("dt")
Dim row() As DataRow = dt.Select()
Dim dtProductos As New DataTable()
dtProductos.Columns.Add(New DataColumn("valorcombo", GetType(String)))
dtProductos.Columns.Add(New DataColumn("textocombo", GetType(String)))
Dim firstrow As DataRow = dtProductos.NewRow()
firstrow (0) = 0
firstrow (1) = ""
dtProductos.Rows.Add(firstrow)
For i As Integer = 0 To row.Length - 1
Dim fila As DataRow = dtProductos.NewRow()
fila(0) = row(i)("cod")
fila(1) = row(i)("text")
dtProductos.Rows.Add(fila)
Next
ddlproductos.DataSource = dtProductos
ddlproductos.DataTextField = "textocombo"
ddlproductos.DataValueField = "valorcombo"
ddlproductos.DataBind()
ddlproductos.SelectedIndex = 0
End Sub
Private Sub FillFields(cert As String)
Dim dtSubsData As DataTable = GetData(cert)
If Not IsNothing(dtSubsData) Then
Dim row As DataRow = dtSubsData.Rows(0)
ddlproductos.ClearSelection()
ddlproductos.SelectedValue = row("cod").ToString()
Session("isFilled") = True
isFilled = Session("isFilled")
End If
End Sub
Can anyone help?

Adding Rows dynamically to GridView in ASP.NET

I use the following code for adding Rows dynamically to GridView in ASP.NET.
ViewState("CourseFeesDetails") = dsGetStudentCourseFeesDetail
If ViewState("CourseFeesDetails") IsNot Nothing Then
Dim dtCourseTable As New DataTable
dtCourseTable = CType(ViewState("CourseFeesDetails"), DataTable)
Dim drCurrentRow As DataRow = Nothing
Dim txtFeesAmt As New System.Web.UI.WebControls.TextBox
If dtCourseTable.Rows.Count > 0 Then
For i = 1 To nNoOfInstallments
drCurrentRow = dtCourseTable.NewRow()
i = i + 1
dtCourseTable.Rows.Add(drCurrentRow)
Next
ViewState("CourseFeesDetails") = dtCourseTable
gvCourseFeeDisplay.DataSource = dtCourseTable
gvCourseFeeDisplay.DataBind()
End If
End If
It does add the number of rows, but adds 1 extra. What should I do?

I need to check a checkbox based on the result of a dataset. If the result is 1 then checkbox should be checked and not checked if it's a 0

When I delete a row from the gridview, it doesn't delete the row that is chosen to be deleted and it deletes the last row of the gridview. It also clears the user input of the row that is now the last row, but all of the other rows maintain their inputs. Any help on this is greatly appreciated.
Here is my RowDeleting sub:
Protected Sub gvPDetails_RowDeleting(sender As Object, e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvPDetails.RowDeleting
If ViewState("CurrentTable") IsNot Nothing Then
Dim dt As DataTable = DirectCast(ViewState("CurrentTable"), DataTable)
Dim delRowIndex As Integer = Convert.ToInt32(e.RowIndex)
Dim delRow As DataRow = dt.Rows(delRowIndex)
dt.Rows.Remove(delRow)
SetRowData()
ViewState("CurrentTable") = dt
End If
End Sub
Here is the SetRowData()
Private Sub SetRowData()
Dim rowIndex As Integer = 0
Dim arrl1 As New ArrayList()
Dim arrl2 As New ArrayList()
Dim arrl3 As New ArrayList()
If ViewState("CurrentTable") IsNot Nothing Then
Dim dtCurrentTable As DataTable = DirectCast(ViewState("CurrentTable"), DataTable)
Dim drCurrentRow As DataRow = Nothing
If dtCurrentTable.Rows.Count > 0 Then
For i As Integer = 0 To dtCurrentTable.Rows.Count - 1
//extract the TextBox values
Dim box1 As TextBox = DirectCast(gvPDetails.Rows(rowIndex).FindControl("txtStore"), TextBox)
//extract the DropDownList Selected Items
Dim ddl1 As DropDownList = DirectCast(gvPDetails.Rows(rowIndex).FindControl("ddlStatus"), DropDownList)
Dim ddl2 As DropDownList = DirectCast(gvPDetails.Rows(rowIndex).FindControl("ddlReason"), DropDownList)
Dim ddl3 As DropDownList = DirectCast(gvPDetails.Rows(rowIndex).FindControl("ddlPldDetail"), DropDownList)
Dim box2 As TextBox = DirectCast(gvPDetails.Rows(rowIndex).FindControl("txtInfo"), TextBox)
//Update the DataRow with the DDL Selected Items
dtCurrentTable.Rows(rowIndex)("Store") = box1.Text
dtCurrentTable.Rows(rowIndex)("Status") = ddl1.SelectedItem.Text
myDD1 = ddl1.SelectedItem.Text
dtCurrentTable.Rows(rowIndex)("Reason") = ddl2.SelectedItem.Text
myDD2 = ddl2.SelectedItem.Text
If ddl3 IsNot Nothing And ddl3.Enabled = True Then
dtCurrentTable.Rows(rowIndex)("Detail") = ddl3.SelectedItem.Text
End If
myDD3 = ddl3.SelectedItem.Text
dtCurrentTable.Rows(rowIndex)("Information") = box2.Text
rowIndex += 1
Dim MemErrList As New ArrayList()
Dim index As Integer = -1
Dim row As GridViewRow = gvPDetails.Rows(i)
index = CInt(gvPDetails.DataKeys(row.RowIndex).Value)
Dim result As Boolean = DirectCast(gvPDetails.Rows(i).FindControl("ckbMemErr"), CheckBox).Checked
//Check in the Session
If Session("CHECKED_ITEMS") IsNot Nothing Then
MemErrList = DirectCast(Session("CHECKED_ITEMS"), ArrayList)
End If
If result Then
If Not MemErrList.Contains(index) Then
MemErrList.Add(index)
End If
Else
MemErrList.Remove(index)
End If
//Next
If MemErrList IsNot Nothing AndAlso MemErrList.Count > 0 Then
Session("CHECKED_ITEMS") = MemErrList
End If
//----SLS File Val Checkbox Array Builder-----------------------------
Dim DetailList As New ArrayList()
Dim index1 As Integer = -1
Dim row1 As GridViewRow = gvPDetails.Rows(i)
index1 = CInt(gvPDetails.DataKeys(row1.RowIndex).Value)
Dim result1 As Boolean = DirectCast(gvPDetails.Rows(i).FindControl("cbSLSFileVal"), CheckBox).Checked
//Check in the Session
If Session("DETAIL1_ITEMS") IsNot Nothing Then
DetailList = DirectCast(Session("DETAIL1_ITEMS"), ArrayList)
End If
If result1 Then
If Not DetailList.Contains(index1) Then
DetailList.Add(index1)
End If
Else
DetailList.Remove(index1)
End If
//Next
If DetailList IsNot Nothing AndAlso DetailList.Count > 0 Then
Session("DETAIL1_ITEMS") = DetailList
End If
//----Router Checkbox Array Builder-----------------------------
Dim DetailList2 As New ArrayList()
Dim index2 As Integer = -1
Dim row2 As GridViewRow = gvPDetails.Rows(i)
index2 = CInt(gvPDetails.DataKeys(row2.RowIndex).Value)
Dim result2 As Boolean = DirectCast(gvPDetails.Rows(i).FindControl("cbRouter"), CheckBox).Checked
//Check in the Session
If Session("DETAIL2_ITEMS") IsNot Nothing Then
DetailList2 = DirectCast(Session("DETAIL2_ITEMS"), ArrayList)
End If
If result2 Then
If Not DetailList2.Contains(index2) Then
DetailList2.Add(index2)
End If
Else
DetailList2.Remove(index2)
End If
//Next
If DetailList2 IsNot Nothing AndAlso DetailList2.Count > 0 Then
Session("DETAIL2_ITEMS") = DetailList2
End If
//----Register1 Checkbox Array Builder-----------------------------
Dim DetailList3 As New ArrayList()
Dim index3 As Integer = -1
Dim row3 As GridViewRow = gvPDetails.Rows(i)
index3 = CInt(gvPDetails.DataKeys(row3.RowIndex).Value)
Dim result3 As Boolean = DirectCast(gvPDetails.Rows(i).FindControl("cbRegister1"), CheckBox).Checked
//Check in the Session
If Session("DETAIL3_ITEMS") IsNot Nothing Then
DetailList3 = DirectCast(Session("DETAIL3_ITEMS"), ArrayList)
End If
If result3 Then
If Not DetailList3.Contains(index3) Then
DetailList3.Add(index3)
End If
Else
DetailList3.Remove(index3)
End If
//Next
If DetailList3 IsNot Nothing AndAlso DetailList3.Count > 0 Then
Session("DETAIL3_ITEMS") = DetailList3
End If
//----Register2 Checkbox Array Builder-----------------------------
Dim DetailList4 As New ArrayList()
Dim index4 As Integer = -1
Dim row4 As GridViewRow = gvPDetails.Rows(i)
index4 = CInt(gvPDetails.DataKeys(row4.RowIndex).Value)
Dim result4 As Boolean = DirectCast(gvPDetails.Rows(i).FindControl("cbRegister2"), CheckBox).Checked
//Check in the Session
If Session("DETAIL4_ITEMS") IsNot Nothing Then
DetailList4 = DirectCast(Session("DETAIL4_ITEMS"), ArrayList)
End If
If result4 Then
If Not DetailList4.Contains(index4) Then
DetailList4.Add(index4)
End If
Else
DetailList4.Remove(index4)
End If
//Next
If DetailList4 IsNot Nothing AndAlso DetailList4.Count > 0 Then
Session("DETAIL4_ITEMS") = DetailList4
End If
arrl1.Add(myDD1)
arrl2.Add(myDD2)
arrl3.Add(myDD3)
Next
//dtCurrentTable.Rows.Add(drCurrentRow)
ViewState("CurrentTable") = dtCurrentTable
//Rebind the Grid with the current data to reflect changes
gvPDetails.DataSource = dtCurrentTable
gvPDetails.DataBind()
End If
Else
End If
//Set Previous Data on Postbacks
SetPrevDataOnDelete(arrl1, arrl2, arrl3)
End Sub
And here is the SetPrevDataOnDelete(arrl1, arrl2, arrl3)
Private Sub SetPrevDataOnDelete(ByVal arrl1 As ArrayList, ByVal arrl2 As ArrayList, ByVal arrl3 As ArrayList)
Dim rowIndex As Integer = 0
myGridViewRow = 0
If ViewState("CurrentTable") IsNot Nothing Then
Dim dt As DataTable = DirectCast(ViewState("CurrentTable"), DataTable)
If dt.Rows.Count > 0 Then
Dim arr1 As ArrayList = arrl1
Dim arr2 As ArrayList = arrl2
Dim arr3 As ArrayList = arrl3
For i As Integer = 0 To dt.Rows.Count - 1
Dim box1 As TextBox = DirectCast(gvPDetails.Rows(rowIndex).FindControl("txtStore"), TextBox)
Dim ddl1 As DropDownList = DirectCast(gvPDetails.Rows(rowIndex).FindControl("ddlStatus"), DropDownList)
Dim ddl2 As DropDownList = DirectCast(gvPDetails.Rows(rowIndex).FindControl("ddlReason"), DropDownList)
Dim ddl3 As DropDownList = DirectCast(gvPDetails.Rows(rowIndex).FindControl("ddlPldDetail"), DropDownList)
Dim box2 As TextBox = DirectCast(gvPDetails.Rows(rowIndex).FindControl("txtInfo"), TextBox)
Dim cbResult As CheckBox = DirectCast(gvPDetails.Rows(rowIndex).FindControl("ckbMemErr"), CheckBox)
//Fill the DropDownList with Data
myGridViewRow = i
//FillDropDownList(ddl1)
FillDDL2OnDel(arrl1, rowIndex)
FillDDL3OnDel(arrl1, arrl2, rowIndex)
If i < dt.Rows.Count - 1 Then
//Assign the value from DataTable to the TextBox
//gvPDetails.Rows(i).Cells(0).Text = Convert.ToString(i + 1)
box1.Text = dt.Rows(rowIndex)("Store").ToString()
box2.Text = dt.Rows(rowIndex)("Information").ToString()
ddl1.Items.FindByText(dt.Rows(rowIndex)("Status").ToString()).Selected = True
If ddl2 Is Nothing Then
ElseIf ddl2 Is Nothing Then
ElseIf ddl2 IsNot Nothing And ddl2.Enabled = True Then
ddl2.Items.FindByText(dt.Rows(rowIndex)("Reason").ToString()).Selected = True
End If
If ddl3 IsNot Nothing And ddl3.Enabled = True Then
ddl3.Items.FindByText(dt.Rows(rowIndex)("Detail").ToString()).Selected = True
End If
Dim MemErrList As ArrayList = DirectCast(Session("CHECKED_ITEMS"), ArrayList)
If MemErrList IsNot Nothing AndAlso MemErrList.Count > 0 Then
Dim row As GridViewRow = gvPDetails.Rows(i)
Dim index As Integer = CInt(gvPDetails.DataKeys(row.RowIndex).Value)
If MemErrList.Contains(index) Then
Dim myCheckBox As CheckBox = DirectCast(row.FindControl("ckbMemErr"), CheckBox)
myCheckBox.Checked = True
End If
//Next
End If
Dim DetailList As ArrayList = DirectCast(Session("DETAIL1_ITEMS"), ArrayList)
If DetailList IsNot Nothing AndAlso DetailList.Count > 0 Then
Dim row As GridViewRow = gvPDetails.Rows(i)
Dim index1 As Integer = CInt(gvPDetails.DataKeys(row.RowIndex).Value)
If DetailList.Contains(index1) Then
Dim myCheckBox1 As CheckBox = DirectCast(row.FindControl("cbSLSFileVal"), CheckBox)
myCheckBox1.Checked = True
End If
//Next
End If
Dim DetailList2 As ArrayList = DirectCast(Session("DETAIL2_ITEMS"), ArrayList)
If DetailList2 IsNot Nothing AndAlso DetailList2.Count > 0 Then
Dim row As GridViewRow = gvPDetails.Rows(i)
Dim index2 As Integer = CInt(gvPDetails.DataKeys(row.RowIndex).Value)
If DetailList2.Contains(index2) Then
Dim myCheckBox2 As CheckBox = DirectCast(row.FindControl("cbRouter"), CheckBox)
myCheckBox2.Checked = True
End If
//Next
End If
Dim DetailList3 As ArrayList = DirectCast(Session("DETAIL3_ITEMS"), ArrayList)
If DetailList3 IsNot Nothing AndAlso DetailList3.Count > 0 Then
Dim row As GridViewRow = gvPDetails.Rows(i)
Dim index3 As Integer = CInt(gvPDetails.DataKeys(row.RowIndex).Value)
If DetailList3.Contains(index3) Then
Dim myCheckBox3 As CheckBox = DirectCast(row.FindControl("cbRegister1"), CheckBox)
myCheckBox3.Checked = True
End If
//Next
End If
Dim DetailList4 As ArrayList = DirectCast(Session("DETAIL4_ITEMS"), ArrayList)
If DetailList4 IsNot Nothing AndAlso DetailList4.Count > 0 Then
Dim row As GridViewRow = gvPDetails.Rows(i)
Dim index4 As Integer = CInt(gvPDetails.DataKeys(row.RowIndex).Value)
If DetailList4.Contains(index4) Then
Dim myCheckBox4 As CheckBox = DirectCast(row.FindControl("cbRegister2"), CheckBox)
myCheckBox4.Checked = True
End If
//Next
End If
End If
rowIndex += 1
Next
End If
End If
End Sub
Thanks again for any help.
Is e.RowIndex returning the correct row number that you are expecting it to?

gridview loop issue

I have a gridview with 4 columns, three of them are template fields where the user can enter the information needed and click a button to submit it to the database. I have 2 issues:
When I click my button to add a second row... the data in the first row posts
disappears. I want to be able to add the row without the first row of information
disappearing for multiple record entry situations. My code follows:
Private Sub SetInitialRow()
Dim dts As New DataTable()
Dim drs As DataRow = Nothing
dts.Columns.Add(New DataColumn("Approval Date", GetType(String)))
dts.Columns.Add(New DataColumn("Total Amount", GetType(String)))
dts.Columns.Add(New DataColumn("Comments", GetType(String)))
dts.Columns.Add(New DataColumn("Initials", GetType(String)))
drs = dts.NewRow()
drs("Approval Date") = String.Empty
drs("Total Amount") = String.Empty
drs("Comments") = String.Empty
drs("Initials") = String.Empty
dts.Rows.Add(drs)
ViewState("CurrentTable") = dts
gvOLIAdj.DataSource = dts
gvOLIAdj.DataBind()
End Sub
Private Sub AddNewRowToGrid()
Dim rowIndex As Integer = 0
If ViewState("CurrentTable") IsNot Nothing Then
Dim dtCurrentTable As DataTable = DirectCast(ViewState("CurrentTable"), DataTable)
Dim drCurrentRow As DataRow = Nothing
If dtCurrentTable.Rows.Count > 0 Then
For i As Integer = 1 To dtCurrentTable.Rows.Count
'extract the TextBox values
Dim box1 As TextBox = DirectCast(gvOLIAdj.Rows(rowIndex).Cells(1).FindControl("txtAdjAppr"), TextBox)
Dim box2 As TextBox = DirectCast(gvOLIAdj.Rows(rowIndex).Cells(2).FindControl("txtAdjAmt"), TextBox)
Dim box3 As TextBox = DirectCast(gvOLIAdj.Rows(rowIndex).Cells(3).FindControl("txtCmmts"), TextBox)
drCurrentRow = dtCurrentTable.NewRow()
drCurrentRow("Approval Date") = box1.Text
dtCurrentTable.Rows(i - 1)("Total Amount") = box2.Text
dtCurrentTable.Rows(i - 1)("Comments") = box3.Text
'dtCurrentTable.Rows(i - 1)("Initials") =
rowIndex += 1
Next
dtCurrentTable.Rows.Add(drCurrentRow)
ViewState("CurrentTable") = dtCurrentTable
gvOLIAdj.DataSource = dtCurrentTable
gvOLIAdj.DataBind()
End If
Else
Response.Write("ViewState is null")
End If
'Set Previous Data on Postbacks
SetPreviousData()
End Sub
Private Sub SetPreviousData()
Dim rowIndex As Integer = 0
If ViewState("CurrentTable") IsNot Nothing Then
Dim dats As DataTable = DirectCast(ViewState("CurrentTable"), DataTable)
If dats.Rows.Count > 0 Then
For i As Integer = 0 To dats.Rows.Count - 1
Dim box1 As TextBox = DirectCast(gvOLIAdj.Rows(rowIndex).Cells(1).FindControl("txtAdjAppr"), TextBox)
Dim box2 As TextBox = DirectCast(gvOLIAdj.Rows(rowIndex).Cells(2).FindControl("txtAdjAmt"), TextBox)
Dim box3 As TextBox = DirectCast(gvOLIAdj.Rows(rowIndex).Cells(3).FindControl("txtCmmts"), TextBox)
box1.Text = dats.Rows(i)("Approval Date").ToString()
box2.Text = dats.Rows(i)("Total Amount").ToString()
box3.Text = dats.Rows(i)("Comments").ToString()
rowIndex += 1
Next
End If
End If
End Sub
Protected Sub btnAddNewRow_Click(sender As Object, e As EventArgs) Handles btnAddNewRow.Click
AddNewRowToGrid()
End Sub
When I try to write a loop that will loop through each of the template fields to grab
the data and put it into my database it doesn't recognize that I have data in the
template fields? This is what I have tried thus far to no avail...
Protected Sub btn_Update_Click(sender As Object, e As EventArgs) Handles btn_Update.Click
For Each row As GridViewRow In gvOLIAdj.Rows
For Each gv As GridViewRow In gvOLIAdj.Rows
Dim appDt As String = (Rows(rowIndex).Cells(1).FindControl("txtAdjAppr")), TextBox)
Dim approvalDt As String = CType(gv.FindControl("txtAdjAppr"), TextBox).Text
Dim totalAmt As String = CType(gv.FindControl("txtAdjAmt"), TextBox).Text
Dim comments As String = CType(gv.FindControl("txtcmmts"), TextBox).Text
Dim intitials As String = DirectCast(gv.FindControl("total"), TextBox).Text
Next
End Sub
In your AddNewRowToGrid method you are doing the following:
dtCurrentTable.Rows(i - 1)("Total Amount") = box2.Text
dtCurrentTable.Rows(i - 1)("Comments") = box3.Text
What's wrong here is the updating of the row in dtCurrentTable.Rows(i - 1). You are updating the values in the row above that which you wish to update.

Resources