Hidden Field asp.net
I want to hide columns in asp.net in GridView then access the values in GridViewSelectIndexChanged using vb.net. I am using hidden fields in the GridView. When I try to access gives me an error object reference not set to an instance here is the code
<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 I开发者_C百科D=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
You may want to consider using datakeys instead of hiddenfields
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.datakeys.aspx
Using datakeys will allow you to do this...
<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
Try only searching in the selected row, like so:
Dim instanceID As String =
CType(GridView1.SelectedRow.FindControl("lblInstanceID"), HiddenField).Value
Also, are the columns in which these hidden fields exist invisible? If they are, FindControl will not find them.
As another option, assuming you don't need access to the value on the client side, you could use a label instead of a hidden field and set its visibility to false.
精彩评论