GridView的asp:HiddenField的等价性

时间:2021-11-25 16:53:22

There is no asp:HiddenField that can be used in a GridView so I was wondering what would work similar to this.

没有asp:HiddenField可以在GridView中使用,所以我想知道什么会与此类似。

My reasoning for wanting this is I have a ButtonField that triggers an OnRowCommand. From there I can figure out which row was selected, but I cannot retrieve the text value from the ButtonField to see the data that was bound to it (through DataTextField).

我想要这个的理由是我有一个触发OnRowCommand的ButtonField。从那里我可以找出选择了哪一行,但是我无法从ButtonField中检索文本值以查看绑定到它的数据(通过DataTextField)。

My solution to this was to have a BoundField and just retrieve the text value from it instead, since I already knew which row was selected. This worked, but I need it to be hidden.

我的解决方案是拥有一个BoundField,只是从中检索文本值,因为我已经知道选择了哪一行。这有效,但我需要隐藏它。

Someone suggested using a HiddenField nested within a TemplateField, but I had troubles retrieving the text value from that HiddenField. Is there some way to access the control within the TemplateField to get the text value of the HiddenField?

有人建议使用嵌套在TemplateField中的HiddenField,但是我从HiddenField中检索文本值时遇到了麻烦。有没有办法访问TemplateField中的控件以获取HiddenField的文本值?

If anyone has any suggestions for alternatives that would be great as well.

如果有人对替代品有任何建议也会很好。

Thanks,
Matt

3 个解决方案

#1


You can use the DataKeyNames property on the Gridview.

您可以在Gridview上使用DataKeyNames属性。

gridView.DataKeyNames = { "values", "you", "want "};

and then you can access them like this:

然后你可以像这样访问它们:

gridView.DataKeys[rowId].Values["value"];

#2


You can use a template field with the commandArgument equal to the the ID of the record/row.

您可以使用模板字段,commandArgument等于记录/行的ID。

<ItemTemplate>
  <asp:Button ID="Button1" runat="server" CausesValidation="false" 
      CommandName="SomeCommand" Text="Button" CommandArgument='<%# Eval("SomeID") %'></asp:LinkButton>
</ItemTemplate>

Then, in the row command handling event you can get that value by e.CommandArgument

然后,在行命令处理事件中,您可以通过e.CommandArgument获取该值

#3


is it possible to add another ID column, as a unique representative for a record(row) ? That particular ID column can be set with style: display=none in order to hide it, but still the ID is inside the HTML form.

是否可以添加另一个ID列,作为记录(行)的唯一代表?可以使用style:display = none设置该特定ID列以隐藏它,但ID仍在HTML表单中。

and then you can retrieve the value of that particular ID column to be processed further.

然后您可以检索要进一步处理的特定ID列的值。

hope this helps,

希望这可以帮助,

hadi

#1


You can use the DataKeyNames property on the Gridview.

您可以在Gridview上使用DataKeyNames属性。

gridView.DataKeyNames = { "values", "you", "want "};

and then you can access them like this:

然后你可以像这样访问它们:

gridView.DataKeys[rowId].Values["value"];

#2


You can use a template field with the commandArgument equal to the the ID of the record/row.

您可以使用模板字段,commandArgument等于记录/行的ID。

<ItemTemplate>
  <asp:Button ID="Button1" runat="server" CausesValidation="false" 
      CommandName="SomeCommand" Text="Button" CommandArgument='<%# Eval("SomeID") %'></asp:LinkButton>
</ItemTemplate>

Then, in the row command handling event you can get that value by e.CommandArgument

然后,在行命令处理事件中,您可以通过e.CommandArgument获取该值

#3


is it possible to add another ID column, as a unique representative for a record(row) ? That particular ID column can be set with style: display=none in order to hide it, but still the ID is inside the HTML form.

是否可以添加另一个ID列,作为记录(行)的唯一代表?可以使用style:display = none设置该特定ID列以隐藏它,但ID仍在HTML表单中。

and then you can retrieve the value of that particular ID column to be processed further.

然后您可以检索要进一步处理的特定ID列的值。

hope this helps,

希望这可以帮助,

hadi