【问题标题】:How to get the ID of a editing textbox in gridview如何在gridview中获取编辑文本框的ID
【发布时间】:2013-12-06 08:50:50
【问题描述】:

我想获取如下文本框的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

【问题讨论】:

  • 你为什么在代码后面这样做?您可以在表单视图中执行此操作。\
  • 大概有20个目标表要编辑,所以想自动生成。
  • 您确定在更新模式下执行代码吗?你确定你给TextBox分配了ID??
  • 不,我没有将 ID 分配给 TextBoxes。因为 GridView 会自动生成它们。
  • 所以在文本框生成块中添加验证器。

标签: asp.net vb.net gridview


【解决方案1】:

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

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)

【讨论】:

  • 谢谢。但我已经尝试过这种解决方法,如果适用,所有列都会更新为 null...
  • @JumpeiTanaka 你的意思是你正在分配RequiredFieldValidators 它不起作用。
  • 不,我的意思是输入的值消失了,它们被更新为空。
  • @JumpeiTanaka 好的!是的,它会导致分配新的 id',并且您可能正在使用旧值进行更新。
【解决方案2】:

您可以尝试以下代码将验证器控件分配给 gridview 控件。 我不确定你用SetValidators()function 做什么。

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

【讨论】:

  • 显示时间过长,只是添加了验证器。我想将 txt.ID 设置为 RegularExpressionValidator.ControlToValidate
  • 以上应用后,出现此异常。找不到在“VldtBytesgvMainte_ctl02_6”的“ControlToValidate”属性中引用的控件 ID“gvMainte_ctl02_6”。
【解决方案3】:

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

            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)

感谢您的合作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-23
    • 2013-07-16
    • 1970-01-01
    • 1970-01-01
    • 2014-04-25
    • 1970-01-01
    • 2011-04-27
    相关资源
    最近更新 更多