Repeater data key

skbohler
skbohler used Ask the Experts™
on
Hello,

I have a Repeater object set up like this:

Checkbox | Title | Image (see included code)

In the code behind, in VB, how do I access the hidden field data key in the code behind?
<ItemTemplate>
                            <tr>
                            <td><input size="3" type=checkbox id="QuestionID" value='1' runat="server" />
                               <asp:HiddenField ID="ItemID" value='<%# Container.DataItem("Items_ID") %>' runat="server" />
                            </td>
                              <td><%#DataBinder.Eval(Container.DataItem, "ItemText")%>
                            </td>
                            </tr>
                            <tr><td><asp:LinkButton ID="ViewButton" runat="server" CausesValidation="False" OnClick="ViewItem_Click">
									<asp:Image ID="ViewImg" runat="server" imageUrl="~/images/view.png" ToolTip="View" />
								</asp:LinkButton>
								</td></tr>
                            </ItemTemplate>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2013

Commented:
Depends where you are trying to get the value. Here is one way:

for (int i = 0; i < Repeater1.Items.Count; i++)
{
            HiddenField hf = (HiddenField )Repeater1.Items[i].FindControl("ItemID");
            if (hf != null)
            {
               string id = hf.Value;
            }
}

Open in new window

Author

Commented:
Thanks for your reply.

However, I'm not just checking to see which checkbox is checked. I want the image field, when clicked, to know the itemid for that row in the repeater.

With your code, all of the hf's are coming back as not null, so

string id = hf.value is being executed for each loop through the repeater

Top Expert 2013

Commented:
In that case why don't you use ImageButton like this:

 <asp:ImageButton ID="ImageButton1" runat="server" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "Items_ID") %>' CommandName="ViewItem" />

Open in new window


Now handle ItemCommand of Repeater like:

protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "ViewItem")
        {
            //Here I have considered Item_Id as a string
            string itemid= (string)e.CommandArgument;
            Response.Write(itemid);
        }
    }

Open in new window


Note:
1: With the above code you don't need HiddenField at all.
2: If you want to keep using LinkButton then apply the CommandName and CommanArgument in above to LinkButton.

OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

Author

Commented:
Any chance you have that code in VB?
Top Expert 2013
Commented:
Protected Sub Repeater1_ItemCommand(source As Object, e As RepeaterCommandEventArgs) Handles Repeater1.ItemCommand
	If e.CommandName = "ViewItem" Then
		'Here I have considered Item_Id as a string
		Dim itemid As String = DirectCast(e.CommandArgument, String)
		Response.Write(itemid)
	End If
End Sub

Open in new window


Markup remains same.
FYI:
C# To VB.NET Converter

Author

Commented:
Thanks for the link to the converter site!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial