2012-05-22 31 views
0

我有我的GridView以下兩個綁定列獲得一個綁定列不可見的價值

<asp:BoundField DataField="ProductId" HeaderText="ID" HeaderStyle-HorizontalAlign="Left" Visible = "false" /> 
<asp:BoundField DataField="ProductDescription" HeaderText="product Description" HeaderStyle-HorizontalAlign="Left" /> 

如果其設置爲可見的假我怎麼能獲得第一個綁定字段的值?

回答

2

您不能訪問的BoundField的價值是看不見。您必須使用TemplateField並在其中添加HiddenField控件並綁定其Value屬性。

標記,

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"> 
    <Columns> 
     <asp:TemplateField> 
      <ItemTemplate> 
       <asp:HiddenField ID="HiddenField1" runat="server" 
        Value='<%# Eval("ProductId") %>' /> 
       .... 
      </ItemTemplate> 
     </asp:TemplateField> 
    </Columns> 
</asp:GridView> 

後面的代碼,以獲得從各行的第一小區的HiddenField控件的值。

foreach (GridViewRow row in GridView1.Rows) 
    { 
    if (row.RowType == DataControlRowType.DataRow) 
     { 
     HiddenField productid = row.Cells[0].FindControl("HiddenField1") as HiddenField; 
     ... 
     } 
    } 
1

使用標識最好是在GridView上

DataKeyNames="ProductId" 

使用DataKeyNames屬性,然後在後面的代碼,你可以得到它

GridView1.DataKeys[row_index]["ProductId"]