简体   繁体   中英

vb.net asp find programmatically created textbox control value in gridview on button click

I am new at vb.net aspx programming. I have a code like sample below which is creates 15 textbox controls in a gridview cells at the same row. I have a button to save the values in the textboxes to database.

Protected Sub MaliyetGrid_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles MaliyetGrid.RowDataBound
     txtPrimVergiKesinti As New TextBox() 
     Dim txtPrimVergiKesintiValue as String  
     txtPrimVergiKesinti.ID = "txtPrimVergiKesinti"  
     txtPrimVergiKesinti.Width = 45 
     txtPrimVergiKesinti.Font.Size = "10" 
     txtPrimVergiKesinti.CssClass = "auto-style3" 
     txtPrimVergiKesinti.TextMode = TextBoxMode.Number 
     txtPrimVergiKesinti.Text = TryCast(e.Row.DataItem, DataRowView).Row("primdenvergioran").ToString() 
     e.Row.Cells(14).Controls.Add(txtPrimVergiKesinti) 
End Sub

When i click the save button, i want to get the values of each textboxes. For this, sample i wrote the code below.

Protected Sub SaveButton_Click(sender As Object, e As EventArgs) Handles     SaveButton.Click 
     txtPrimVergiKesintiValue = CType(Me.MaliyetGrid.FindControl("txtPrimVergiKesinti"), TextBox).Text 
End Sub

it always returns with error : {"Object reference not set to an instance of an object."} . How can i get the values of textboxes to save into the datebase..

Thanks..

I just changed the code as below and still getting the same error..

Dim txtYear As New TextBox()
Dim txtMonth As New TextBox()
Dim txtKidemOran As New TextBox()
Dim txtMaasOran As New TextBox()
Dim txtNicelikOran As New TextBox()
Dim txtSatisPrimOran As New TextBox()
Dim txtDestekPrimOran As New TextBox()
Dim txtVergiOran As New TextBox()
Dim txtTeamLiderPrimOran As New TextBox()
Dim txtSatisciPrimOran As New TextBox()
Dim txtSatiscidanTLyePrimOran As New TextBox()
Dim txtYukTakipPrimOran As New TextBox()
Dim txtBeklenenKur As New TextBox()
Dim txtGercekKur As New TextBox()
Dim txtPrimVergiKesinti As New TextBox()

Protected Sub MaliyetGrid_RowCreated(sender As Object, e As GridViewRowEventArgs) Handles MaliyetGrid.RowCreated
    Select Case e.Row.RowType
        Case DataControlRowType.DataRow

            txtYear.ID = "txtYear"
            txtYear.Width = 45
            txtYear.Font.Size = "10"
            txtYear.CssClass = "auto-style3"
            txtYear.TextMode = TextBoxMode.Number
            e.Row.Cells(0).Controls.Add(txtYear)

            txtMonth.ID = "txtMonth"
            txtMonth.Width = 45
            txtMonth.Font.Size = "10"
            txtMonth.CssClass = "auto-style3"
            txtMonth.TextMode = TextBoxMode.Number
            e.Row.Cells(1).Controls.Add(txtMonth)

            txtKidemOran.ID = "txtKidemOran"
            txtKidemOran.Width = 45
            txtKidemOran.Font.Size = "10"
            txtKidemOran.CssClass = "auto-style3"
            txtKidemOran.TextMode = TextBoxMode.Number
            e.Row.Cells(2).Controls.Add(txtKidemOran)

            txtMaasOran.ID = "txtMaasOran"
            txtMaasOran.Width = 45
            txtMaasOran.Font.Size = "10"
            txtMaasOran.CssClass = "auto-style3"
            txtMaasOran.TextMode = TextBoxMode.Number
            e.Row.Cells(3).Controls.Add(txtMaasOran)

            txtNicelikOran.ID = "txtNicelikOran"
            txtNicelikOran.Width = 45
            txtNicelikOran.Font.Size = "10"
            txtNicelikOran.CssClass = "auto-style3"
            txtNicelikOran.TextMode = TextBoxMode.Number
            e.Row.Cells(4).Controls.Add(txtNicelikOran)

            txtSatisPrimOran.ID = "txtSatisPrimOran"
            txtSatisPrimOran.Width = 45
            txtSatisPrimOran.Font.Size = "10"
            txtSatisPrimOran.CssClass = "auto-style3"
            txtSatisPrimOran.TextMode = TextBoxMode.Number
            e.Row.Cells(5).Controls.Add(txtSatisPrimOran)

            txtDestekPrimOran.ID = "txtDestekPrimOran"
            txtDestekPrimOran.Width = 45
            txtDestekPrimOran.Font.Size = "10"
            txtDestekPrimOran.CssClass = "auto-style3"
            txtDestekPrimOran.TextMode = TextBoxMode.Number
            e.Row.Cells(6).Controls.Add(txtDestekPrimOran)

            txtVergiOran.ID = "txtVergiOran"
            txtVergiOran.Width = 45
            txtVergiOran.Font.Size = "10"
            txtVergiOran.CssClass = "auto-style3"
            txtVergiOran.TextMode = TextBoxMode.Number
            e.Row.Cells(7).Controls.Add(txtVergiOran)

            txtTeamLiderPrimOran.ID = "txtTeamLiderPrimOran"
            txtTeamLiderPrimOran.Width = 45
            txtTeamLiderPrimOran.Font.Size = "10"
            txtTeamLiderPrimOran.CssClass = "auto-style3"
            txtTeamLiderPrimOran.TextMode = TextBoxMode.Number
            e.Row.Cells(8).Controls.Add(txtTeamLiderPrimOran)

            txtSatisciPrimOran.ID = "txtSatisciPrimOran"
            txtSatisciPrimOran.Width = 45
            txtSatisciPrimOran.Font.Size = "10"
            txtSatisciPrimOran.CssClass = "auto-style3"
            txtSatisciPrimOran.TextMode = TextBoxMode.Number
            e.Row.Cells(9).Controls.Add(txtSatisciPrimOran)

            txtSatiscidanTLyePrimOran.ID = "txtSatiscidanTLyePrimOran"
            txtSatiscidanTLyePrimOran.Width = 45
            txtSatiscidanTLyePrimOran.Font.Size = "10"
            txtSatiscidanTLyePrimOran.CssClass = "auto-style3"
            txtSatiscidanTLyePrimOran.TextMode = TextBoxMode.Number
            e.Row.Cells(10).Controls.Add(txtSatiscidanTLyePrimOran)

            txtYukTakipPrimOran.ID = "txtYukTakipPrimOran"
            txtYukTakipPrimOran.Width = 45
            txtYukTakipPrimOran.Font.Size = "10"
            txtYukTakipPrimOran.CssClass = "auto-style3"
            txtYukTakipPrimOran.TextMode = TextBoxMode.Number
            e.Row.Cells(11).Controls.Add(txtYukTakipPrimOran)

            txtBeklenenKur.ID = "txtBeklenenKur"
            txtBeklenenKur.Width = 45
            txtBeklenenKur.Font.Size = "10"
            txtBeklenenKur.CssClass = "auto-style3"
            txtBeklenenKur.TextMode = TextBoxMode.Number
            e.Row.Cells(12).Controls.Add(txtBeklenenKur)

            txtGercekKur.ID = "txtGercekKur"
            txtGercekKur.Width = 45
            txtGercekKur.Font.Size = "10"
            txtGercekKur.CssClass = "auto-style3"
            txtGercekKur.TextMode = TextBoxMode.Number
            e.Row.Cells(13).Controls.Add(txtGercekKur)

            txtPrimVergiKesinti.ID = "txtPrimVergiKesinti"
            txtPrimVergiKesinti.Width = 45
            txtPrimVergiKesinti.Font.Size = "10"
            txtPrimVergiKesinti.CssClass = "auto-style3"
            txtPrimVergiKesinti.TextMode = TextBoxMode.Number
            e.Row.Cells(14).Controls.Add(txtPrimVergiKesinti)

    End Select
End Sub

   Protected Sub MaliyetGrid_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles MaliyetGrid.RowDataBound
    Try
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim txtExpectedCost As New TextBox()
            txtExpectedCost.ID = "txtExpectedCost"
            txtExpectedCost.Width = 50
            txtExpectedCost.Font.Size = "8"
            txtExpectedCost.TextMode = TextBoxMode.Number
            txtExpectedCost.CssClass = "auto-style3"
            Select Case TreeView1.SelectedValue
                Case "P1"

                    If kopyaParam = True Then
                        txtYear.Text = primYil.SelectedValue
                    Else
                        txtYear.Text = TryCast(e.Row.DataItem, DataRowView).Row("paramyear").ToString()
                    End If
                    txtYear.Enabled = False

                    If kopyaParam = True Then
                        txtMonth.Text = primAy.SelectedValue
                    Else
                        txtMonth.Text = TryCast(e.Row.DataItem, DataRowView).Row("parammonth").ToString()
                    End If
                    txtMonth.Enabled = False
                    txtKidemOran.Text = TryCast(e.Row.DataItem, DataRowView).Row("kidemoran").ToString()
                    txtMaasOran.Text = TryCast(e.Row.DataItem, DataRowView).Row("maasoran").ToString()
                    txtNicelikOran.Text = TryCast(e.Row.DataItem, DataRowView).Row("nicelikoran").ToString()
                    txtSatisPrimOran.Text = TryCast(e.Row.DataItem, DataRowView).Row("satisadagitilacakoran").ToString()
                    txtDestekPrimOran.Text = TryCast(e.Row.DataItem, DataRowView).Row("destegedagitilacakoran").ToString()
                    txtVergiOran.Text = TryCast(e.Row.DataItem, DataRowView).Row("ayrilacakvergi").ToString()
                    txtTeamLiderPrimOran.Text = TryCast(e.Row.DataItem, DataRowView).Row("takimlideriprimoran").ToString()
                    txtSatisciPrimOran.Text = TryCast(e.Row.DataItem, DataRowView).Row("satisciprimoran").ToString()
                    txtSatiscidanTLyePrimOran.Text = TryCast(e.Row.DataItem, DataRowView).Row("satistantakimliderineprimoran").ToString()
                    txtYukTakipPrimOran.Text = TryCast(e.Row.DataItem, DataRowView).Row("yuktakipprimoran").ToString()
                    If kopyaParam = True Then
                        txtBeklenenKur.Text = ""
                    Else
                        txtBeklenenKur.Text = TryCast(e.Row.DataItem, DataRowView).Row("beklenenkur").ToString()
                    End If
                    If kopyaParam = True Then
                        txtGercekKur.Text = ""
                    Else
                        txtGercekKur.Text = TryCast(e.Row.DataItem, DataRowView).Row("gerceklesenkur").ToString()
                    End If
                    txtPrimVergiKesinti.Text = TryCast(e.Row.DataItem, DataRowView).Row("primdenvergioran").ToString()


                    If txtKidemOran.Text <> "" Then
                        CopyPrevious.Visible = False
                        SaveButton.Visible = True
                    Else
                        CopyPrevious.Visible = True

                        SaveButton.Visible = False
                    End If
                    kopyaParam = False

                Case "P2", "P3"
                    txtExpectedCost.Text = "" 'TryCast(e.Row.DataItem, DataRowView).Row("GVMATRAH").ToString()
                    e.Row.Cells(9).Controls.Add(txtExpectedCost)
                Case "P4", "P5"
                    txtExpectedCost.Text = "" 'TryCast(e.Row.DataItem, DataRowView).Row("GVMATRAH").ToString()
                    e.Row.Cells(3).Controls.Add(txtExpectedCost)
            End Select

        End If
    Catch ex As Exception
        Response.Write("<script>alert('" & Err.Description & "')</script>")
    End Try

End Sub

 Protected Sub SaveButton_Click(sender As Object, e As EventArgs) Handles SaveButton.Click
    Dim txtPrimVergiKesintiValue As String
    txtPrimVergiKesintiValue = CType(Me.MaliyetGrid.FindControl("MaliyetGrid_txtPrimVergiKesinti_0"), TextBox).Text
    Response.Write("<script>alert('" & txtPrimVergiKesinti.Text & "')</script>")
End Sub

You have to recreate dynamically created controls on every postback with the same ID's as before. I suggest to use a different approach, add them in RowCreated since that is executed on every postback, RowDataBound only if you databind the grid.

Protected Sub MaliyetGrid_RowCreated(sender As Object, e As GridViewRowEventArgs) Handles MaliyetGrid.RowCreated
    Select Case e.Row.RowType
        Case DataControlRowType.DataRow
            Dim txtPrimVergiKesinti As New TextBox()
            txtPrimVergiKesinti.ID = "txtPrimVergiKesinti"
            txtPrimVergiKesinti.Width = 45
            txtPrimVergiKesinti.Font.Size = New FontUnit(10)
            txtPrimVergiKesinti.CssClass = "auto-style3"
            txtPrimVergiKesinti.TextMode = TextBoxMode.Number
            e.Row.Cells(14).Controls.Add(txtPrimVergiKesinti)
    End Select
End Sub

Note that you should check e.Row.RowType if you only want to add the textBox to the DataItems instead of also to the header and footer.

You can't access the datasource in RowCreated but only in RowDataBound . So you need to set the Text of the TextBox in RowDataBound :

Protected Sub MaliyetGrid_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles MaliyetGrid.RowDataBound
    Select Case e.Row.RowType
        Case DataControlRowType.DataRow
            Dim txtPrimVergiKesinti = DirectCast(e.Row.FindControl("txtPrimVergiKesinti"), TextBox)
            Dim row As DataRow = DirectCast(e.Row.DataItem, DataRowView).Row
            txtPrimVergiKesinti.Text = row.Field(Of String)("primdenvergioran")
    End Select
End Sub

You get the exception in the save-button click event because you are using the GridView for FindControl . That doesn't work because the grid contains multiple GridViewRows and each row contains this TextBox. Actually the GridViewRow is the NamingContainer . So you must use:

Protected Sub SaveButton_Click(sender As Object, e As EventArgs) Handles btn.Click
    For Each row As GridViewRow In Me.MaliyetGrid.Rows
        Dim txtPrimVergiKesinti = DirectCast(row.FindControl("txtPrimVergiKesinti"), TextBox)
        Dim txtPrimVergiKesintiValue As String = txtPrimVergiKesinti.Text
        ' ....
    Next
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM