问题描述
我有一个查询前pression这我绑定在Page_Load中的GridView。我想在SelectedIndexChaned事件来捕获的数据是这样定义一个BoundField:
I have a query expression which I am binding to a GridView in Page_Load. The data I want to capture in the SelectedIndexChaned event is in a BoundField defined thus:
<asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True"
SortExpression="ID" Visible="False" />
如果我设置可见=真,我没有麻烦此数据。有没有一种方法来隐藏ID字段,仍然可以得到的数据?
If I set Visible="True", I have no trouble getting this data. Is there a way to hide the ID field and still get the data?
推荐答案
取决于你如何试图获取数据。如果这是一个ID字段,为每个行唯一的数据源,使用的DataKeyNames =ID
在GridView声明。然后,在code的后面,当你需要的ID,可以使用以下行:
Depends on how you are trying to get the data. If this is an ID field that is unique for each row in the datasource, use DataKeyNames = "ID"
in the GridView declaration. Then, in the code behind, whenever you need the ID, you can use the following line:
string ID = GridView1.Rows[GridRowIndex].DataKeys[0].Value.ToString();
您也可以转换的绑定列到一个TemplateField之一,并放置一个HiddenField在它来存储的ID。像这样:
You could also convert one of your BoundFields to a TemplateField, and place a HiddenField in it to store the ID. Like so:
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="someOtherDataLabel" runat="server" />
<asp:HiddenField ID="IDHiddenField" runat=server />
</ItemTemplate>
</asp:TemplateField>
然后就可以使用的FindControl()
在GridView的的RowDataBound
事件存储的ID值。
Then you could use FindControl()
in the RowDataBound
event of the GridView to store the ID value.
这篇关于可以将数据保存在一个动态绑定GridView的看不见的领域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!