开发者

Please explain the following code?

While going through some of the code, i found there is written like this:开发者_开发问答

ListViewDataItem dataItem = (ListViewDataItem)(((ImageButton)sender).Parent.Parent);

int noteId = Convert.ToInt32(((HiddenField)dataItem.FindControl("hidNoteId")).Value);

Please explain the meaning of these two line.


There is a control (typed to hidden field) that is being used to track the identity of item within a ListView (http://msdn.microsoft.com/en-us/library/bb398790.aspx)

And it's code that shouldn't be written, IMO. It's inefficient and brittle. Also, the identity of the item is stored in plain text in the source of the page.

This code appears to be getting the identity of a databound item when a button is clicked. A better way would be to simply set the command arguments of the button, like so:

<asp:ImageButton runat="server" CommandArgument="[Binding Expression]" />

In the event handler for the button, the CommandArgument can be retrieved and converted to an Int32. With this methodology, you shouldn't even need that hidden field.


Firstly, both the lines seem a bit crazy in their use of brackets and casting. There's probably a better way to do what they're doing, but without more code it's difficult to offer any suggestions.

Anyway, the first line is going to be in an event handler for an event raised by an ImageButton. The event handler method will have a sender argument; this is being cast to an ImageButton. Then Parent.Parent is called on this ImageButton; this will give the object two levels up in the control heirarchy. The developer is obviously quite sure this is a ListViewDataItem, so it's being cast to one of these. Therefore the variable dataItem now contains an instance of a ListViewDataItem.

In the second line, the FindControl method is being called on dataItem. There is presumably a control under this ListViewDataItem with an ID of "hidNoteId". FindControl returns a Control; but the developer knows this control is actually a HiddenField, so there's a cast. The Value property of this hidden field is then passed into the Convert.ToInt32 method to give an integer - this is then stored in the noteId variable.

So at the end of it all, there's a ListView, in which each data item contains a hidden field that contains the value of some ID. This code is getting the ID.


  1. dataItem is a bound control, which when clicked is being used to represent a placeholder. This is used to identify which line is being executed within in the control.
  2. By looking for a parent's parent of this control, it's giving the code a starting point to navigate to the correct row to extract a value from.
  3. After the row is found, the noteId is being assigned a hidden value and cast into an integer.

When items are bound to a grid/repeater, with a button as the post-back control, a way is needed to identify which row is being executed. All the code is doing above is navigating the control and extracting a value from a set of values within the row.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜