vb.net asp在按钮单击时在gridview中找到以编程方式创建的文本框控件值

时间:2015-10-01 13:41:01

标签: asp.net vb.net gridview dynamic textbox

我是vb.net aspx编程的新手。我有一个像下面的示例代码,它在同一行的gridview单元格中创建15个文本框控件。 我有一个按钮将文本框中的值保存到数据库。

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

当我单击“保存”按钮时,我想获取每个文本框的值。为此,我编写了以下代码。

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

它总是返回错误:{"Object reference not set to an instance of an object."}。 如何获取文本框的值以保存到日期库中。

谢谢..

我刚刚更改了以下代码并仍然遇到相同的错误..

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

1 个答案:

答案 0 :(得分:0)

您必须使用与以前相同的ID在每个回发上重新创建动态创建的控件。我建议使用不同的方法,将它们添加到RowCreated中,因为只有在对网格进行数据绑定时才会在每次回发时执行RowDataBound

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

请注意,如果您只想将e.Row.RowType添加到textBox而不是添加到页眉和页脚,则应检查DataItems

您无法访问RowCreated中的数据源,只能访问RowDataBound中的数据源。因此,您需要在Text中设置TextBox的{​​{1}}:

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 使用的是GridView。这不起作用,因为网格包含多个FindControl,每行包含此TextBox。实际上GridViewRowsGridViewRow。所以你必须使用:

NamingContainer