Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 如何在gridview中获取编辑文本框的ID_Asp.net_Vb.net_Gridview - Fatal编程技术网

Asp.net 如何在gridview中获取编辑文本框的ID

Asp.net 如何在gridview中获取编辑文本框的ID,asp.net,vb.net,gridview,Asp.net,Vb.net,Gridview,我想获得一个文本框的ID,如下面所示,以添加验证程序,客户端ID包含生成的字符串,也是UniqueID,但只有ID不包含任何内容,为什么 Protected Sub GridView1_RowDataBound(ByVal sender As GridView, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound 'Manipulate only editin

我想获得一个文本框的ID,如下面所示,以添加验证程序,客户端ID包含生成的字符串,也是UniqueID,但只有ID不包含任何内容,为什么

Protected Sub GridView1_RowDataBound(ByVal sender As GridView, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound

    'Manipulate only editing row.
    If e.Row.RowType = DataControlRowType.DataRow Then
        If sender.EditIndex = e.Row.RowIndex Then

            'Search textbox and add validators.
            For Each cell As TableCell In e.Row.Cells                
                If cell.Controls.Count = 1 AndAlso TypeOf (cell.Controls(0)) Is TextBox Then
                    Dim txt As TextBox = DirectCast(cell.Controls(0), System.Web.UI.WebControls.TextBox)

                    'txt.ID is nothing...why?
                    SetValidators(cell.Controls, txt.ID)
                End If
            Next
        End If
    End If
End Sub

如果适用于您的应用程序,这里有一个变通方法

Dim txt As TextBox = DirectCast(cell.Controls(0), System.Web.UI.WebControls.TextBox)

                //'txt.ID is nothing...why?
                 // Here you can assign new ID to your control as per your logic

                    txt.ID = "newID"; 
                   SetValidators(cell.Controls, txt.ID)

您可以尝试以下代码将验证程序控件分配给gridview控件。 我不确定您如何使用
SetValidators()
函数

Protected Sub GridView1_RowDataBound(ByVal sender As GridView, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound

    'Manipulate only editing row.
    If e.Row.RowType = DataControlRowType.DataRow Then
        If sender.EditIndex = e.Row.RowIndex Then

            'Search textbox and add validators.
            For Each cell As TableCell In e.Row.Cells                
                If cell.Controls.Count = 1 AndAlso TypeOf (cell.Controls(0)) Is TextBox Then
                    Dim txt As TextBox = DirectCast(cell.Controls(0), System.Web.UI.WebControls.TextBox)

                    'txt.ID is nothing...why?
                    SetValidators(cell.Controls, txt.ClientID)
                End If
            Next
        End If
    End If
End Sub

最后,我通过应用下面的变通方法解决了这个问题

            Dim txt As TextBox = DirectCast(cell.Controls(0), System.Web.UI.WebControls.TextBox)

            'The ID is generated by to refer ClientID.
            Dim foo = txt.ClientID

            'Therefore, already txt.ID is not nothing.
            SetValidators(cell.Controls, txt.ID)

谢谢您的合作。

为什么要在代码隐藏中这样做?您可以在表单视图中执行这些操作。\n大约有20个目标表要编辑,所以我想自动生成。您确定要在更新模式下执行代码吗?你确定你给文本框分配了ID吗?不,我没有给文本框分配ID。因为GridView会自动生成它们。所以在文本框生成块中添加验证器。谢谢。但我已经尝试过这种解决方法,如果适用,所有列都将更新为null…@JumpeiTanaka您是说您正在分配
RequiredFieldValidators
它不起作用。不,我是说输入值将消失,并更新为null。@JumpeiTanaka Ok!是的,这将导致分配新id,并且您可能正在使用旧值进行更新。它太长,无法显示,但仅添加验证程序。我想将txt.ID设置为
RegularExpressionValidator.ControlToValidate
。在上面应用之后,发生了此异常。找不到在“VldtBytesgvMainte_ctl02_6”的“ControlToValidate”属性中引用的控件ID“gvMainte_ctl02_6”。