隐藏的字段asp.net

时间:2010-04-30 14:42:04

标签: asp.net templates hidden-field

我想在GridView中隐藏asp.net中的列,然后使用vb.net访问GridViewSelectIndexChanged中的值。我在GridView中使用隐藏字段。当我尝试访问时给我一个错误对象引用未设置为实例这里是代码

<asp:GridView ID="GridView1" runat="server" OnSorting="GridView1_OnSorting" AllowPaging="True" AllowSorting="True" 
            AutoGenerateColumns="False"  
           BorderStyle="Outset" CellPadding="4" DataSourceID="odsA02_Tracking"
            ForeColor="#333333" GridLines="Vertical" Style="border-right: #0000ff thin solid;
            table-layout: auto; border-top: #0000ff thin solid; font-size: x-small; border-left: #0000ff thin solid;
            border-bottom: #0000ff thin solid; font-family: Arial; border-collapse: separate" Font-Size="Small" PageSize="30">
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <RowStyle BackColor="#EFF3FB" />
            <EditRowStyle BackColor="#2461BF" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="White" />
            <Columns>
                <asp:CommandField ShowSelectButton="True" />
                <asp:boundfield datafield="Since" HeaderText="Submit Date" ReadOnly=true SortExpression="Since" />

                <asp:BoundField DataField="Started_By" HeaderText="Submitted By" SortExpression="Started_By" />
                <asp:BoundField DataField="Client_FullName" HeaderText="Client Name" ReadOnly="True" SortExpression="Client_FullName" />
                <asp:BoundField DataField="Product_Desc" HeaderText="Product" ReadOnly="True" SortExpression="Product_Desc" />
                <asp:BoundField DataField="Branch_List" HeaderText="Branch" ReadOnly="True" SortExpression="Branch_List" />
                <asp:BoundField DataField="Event_AssignedID" HeaderText="Assigned To" ReadOnly="True"
                    SortExpression="Event_AssignedID" />
                <asp:BoundField DataField="DaysElapsed" HeaderText="Days Open" ReadOnly="True"  SortExpression="DaysElapsed" />
                <asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status" /> 
                <asp:TemplateField HeaderText=Instance_ID >
                <ItemTemplate>
                <asp:HiddenField ID=lblInstanceID Value='<%#Eval("Instance_ID") %>' runat=server>
                </asp:HiddenField>
                 </ItemTemplate>
                 </asp:TemplateField>
                <asp:TemplateField HeaderText=Seq_ID>
                <ItemTemplate>
                <asp:HiddenField ID=lblSeqID Value='<%#Eval("Seq_ID") %>' runat=server/>                 
                </ItemTemplate>             

                </asp:TemplateField>

                <asp:TemplateField HeaderText=Form_Code>
                <ItemTemplate>
                <asp:HiddenField ID=lblFormCode Value='<%#Eval("Form_Code") %>' runat=server/>                 
                </ItemTemplate>             

                </asp:TemplateField>



            </Columns>
        </asp:GridView>

 Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged
        Dim Instance_ID As String
        Dim Seq_ID As String
        Dim Form_Code As String
        Dim PARMS As String
        Dim DestinationURL As String
        Dim DestinationParms As String
        Dim instanceID As String = CType(GridView1.FindControl("lblInstanceID"), HiddenField).Value
End sub

3 个答案:

答案 0 :(得分:2)

您可能需要考虑使用datakeys而不是hiddenfields

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.datakeys.aspx

使用datakeys可以执行此操作...

<asp:GridView ID="GridView1" runat="server" datakeynames="instanceID" .../>

Protected Sub GridView1_SelectedIndexChanges(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged 
    Dim index As Integer = CustomersGridView.SelectedIndex
    Dim instanceID As String = GridView1.DataKeys(index).Value.ToString()
End Sub

答案 1 :(得分:0)

尝试仅搜索所选行,如下所示:

Dim instanceID As String = 
CType(GridView1.SelectedRow.FindControl("lblInstanceID"), HiddenField).Value

此外,这些隐藏字段存在的列是否不可见?如果是,FindControl将找不到它们。

答案 2 :(得分:0)

作为另一种选择,假设您不需要访问客户端上的值,您可以使用标签而不是隐藏字段,并将其可见性设置为false。