2010-04-30 225 views
0

我想隱藏GridView中的asp.net中的列,然後使用vb.net訪問GridViewSelectIndexChanged中的值。我在GridView中使用隱藏的字段。當我嘗試訪問使我不要設置在這裏的實例錯誤對象引用的代碼隱藏字段asp.net

<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 

回答

0

嘗試僅僅搜索所選行中,像這樣:

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

而且,是列這些隱藏領域存在隱形?如果它們是,FindControl將不會找到它們。

+0

如何在沒有FindControl的情況下訪問它們 – shoakat 2010-04-30 18:25:59

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 
0

作爲另一個選項,假設你不需要訪問客戶端的值,您可以使用標籤而不是隱藏字段,並將其可見性設置爲false。