如何获取gridview上隐藏字段的值?

时间:2013-06-07 15:37:45

标签: asp.net vb.net

现在这真的令人沮丧。

我在gridview标记上有这个隐藏字段:

<asp:TemplateField>
 <ItemTemplate>  
  <asp:HiddenField ID="dhide" Value='<%# Eval("shipDates","{0:M/dd/yyyy}") %>' runat="server" />
 </ItemTemplate>
</asp:TemplateField>

然后在codebehind上,我试图检索dhide的值:

Sub cancelIt_Click(ByVal sender As Object,ByVal e As System.EventArgs)     Dim objConnection As SqlConnection

Dim DLdates As HiddenField = DirectCast(GridView1.FindControl("dhide"), HiddenField)
Response.write (DLdates)

我做错了什么?

1 个答案:

答案 0 :(得分:0)

它的NamingContainer不是GridView,而是它所在的GridViewRow

所以:

For Each row As GridViewRow In GridView1.Rows 
    Dim dhide = DirectCast(row.FindControl("dhide"), HiddenField)
    Dim shipDates = Date.ParseExact(dhide.Value, "M/dd/yyyy", Nothing)
    ' ...
Next