Re: Determining hidden key values with the Repeater control

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Yes, I tested it. I've used the technique several times, so I was confused
why wouldn't it work in this case. Definately no pun intended.

--
Teemu Keiski
AspInsider, ASP.NET MVP
http://blogs.aspadvice.com/joteke
http://teemukeiski.net


"Eliyahu Goldin" <REMOVEALLCAPITALSeEgGoldDinN@xxxxxxxxxxxx> wrote in
message news:ueugCAj$HHA.1204@xxxxxxxxxxxxxxxxxxxxxxx
I presume you tested your sample and it worked. If so, it is a useful piece
of knowledge, thank you.

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin


"Teemu Keiski" <joteke@xxxxxxxxxxxxxxx> wrote in message
news:eOI1GUe$HHA.3848@xxxxxxxxxxxxxxxxxxxxxxx
I'm wondering what you mean. It's not that hard...

Where is it said that client visibility is key factor in this case?
Second:
Label uses ViewState, it doesn't use postbacks a) itself nor b) does it
need
it, since it relies totally and entirely on ViewState to keep the state.
In
fact it works fine with hidden form field too. In this case postback data
is
not interesting, so ViewState works it out.

So don't confuse server-side workings with client-side. Control being
hidden
doesn't mean it is unavailable. For client yes, since it's not rendered
but
if it even doesn't post anything back to the server, it's meaningless is
it
hidden or not, especially on server, where it is 100% living control
instance. Despite visibility

So, let me put you a sample, which works fine. And I believe it solves
the
OP's question.

<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";>

<script runat="server">

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs)
If Not Page.IsPostBack Then
Dim dt As New DataTable
dt.Columns.Add("Key1", GetType(String))
dt.Columns.Add("Key2", GetType(String))

Dim dr As DataRow

dr = dt.NewRow()
dr("Key1") = "Key 1 on row 1"
dr("Key2") = "Key 2 on row 1"
dt.Rows.Add(dr)

dr = dt.NewRow()
dr("Key1") = "Key 1 on row 2"
dr("Key2") = "Key 2 on row 2"
dt.Rows.Add(dr)

dr = dt.NewRow()
dr("Key1") = "Key 1 on row 3"
dr("Key2") = "Key 2 on row 3"
dt.Rows.Add(dr)

Repeater1.DataSource = dt
Repeater1.DataBind()


End If
End Sub


Protected Sub Repeater1_ItemCommand(ByVal source As Object, ByVal e
As
System.Web.UI.WebControls.RepeaterCommandEventArgs)
If e.CommandName = "CheckValues" Then
Dim lblOnRow As Label =
DirectCast(e.Item.FindControl("Label1"),
Label)
Dim hiddenFieldOnRow As HtmlInputHidden =
DirectCast(e.Item.FindControl("hidden1"), HtmlInputHidden)

Response.Write("label1.Text = " & lblOnRow.Text & ",
hidden1.Value = " & hiddenFieldOnRow.Value)


End If
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml"; >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:repeater id="Repeater1" runat="server"
OnItemCommand="Repeater1_ItemCommand">
<ItemTemplate>
Row <%#Container.ItemIndex + 1%>
<asp:Label id="Label1" runat="server" Visible="false"
Text='<%#Eval("Key1") %>' />
<input type="hidden" id="hidden1" runat="server"
visible="false"
value='<%#Eval("Key2") %>' />

<asp:LinkButton ID="LinkButton1" runat="server"
CommandName="CheckValues" Text="Click me" />
<br />
</ItemTemplate>
</asp:repeater>
</div>
</form>
</body>
</html>




--
Teemu Keiski
AspInsider, ASP.NET MVP
http://blogs.aspadvice.com/joteke
http://teemukeiski.net





"Eliyahu Goldin" <REMOVEALLCAPITALSeEgGoldDinN@xxxxxxxxxxxx> wrote in
message news:OCTjK4b$HHA.5464@xxxxxxxxxxxxxxxxxxxxxxx
It is not only visibility. The fact that a control is not rendered to
the
client means 2 things:

1. It is not available on client.
2. It is not available on server on postbacks since the client doesn't
have the control to send it back.

It won't be found in ItemCommand.

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


"Teemu Keiski" <joteke@xxxxxxxxxxxxxxx> wrote in message
news:u1Tn6ub$HHA.4476@xxxxxxxxxxxxxxxxxxxxxxx
E.g I'm understanding that he needs the value in ItemCommand when
client-side visibility isn't needed for the Label. Point would be just
grab the value in ItemCommand from the Label.


"Teemu Keiski" <joteke@xxxxxxxxxxxxxxx> wrote in message
news:uOwl9sb$HHA.4656@xxxxxxxxxxxxxxxxxxxxxxx
If you use Label, then visibility doesn't matter. I pointed to using
hidden Label.

--
Teemu Keiski
AspInsider, ASP.NET MVP
http://blogs.aspadvice.com/joteke
http://teemukeiski.net


"Eliyahu Goldin" <REMOVEALLCAPITALSeEgGoldDinN@xxxxxxxxxxxx> wrote in
message news:OyHA0hb$HHA.4584@xxxxxxxxxxxxxxxxxxxxxxx
Visible=false won't work since it won't be rendered to client in the
first place.

You can use any suitable control as long as you set css rule
display:none.

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


"Teemu Keiski" <joteke@xxxxxxxxxxxxxxx> wrote in message
news:uY11n3a$HHA.3916@xxxxxxxxxxxxxxxxxxxxxxx
You could use hidden form field

e.g <input type="hidden" id="hdValue" runat="server" />

or just Label contro, which you set Visisble="false"

in <ItemTemplate>. Bind the value to it (with databinding
expression)
or set in code with ItemDataBound. Then in ItemCommand you could
get
the value by FindControling the hdValue control on the current
Item.

I've explained some background for this type of scenarios:

Understanding the naming container hierarchy of ASP.NET databound
controls

http://aspadvice.com/blogs/joteke/archive/2007/02/25/Understanding-the-naming-container-hierarchy-of-ASP.NET-databound-controls.aspx


--
Teemu Keiski
AspInsider, ASP.NET MVP
http://blogs.aspadvice.com/joteke
http://teemukeiski.net



"SandpointGuy" <SandpointGuy@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in
message news:94BF0107-A096-43DF-9825-25B0BDE0CBD8@xxxxxxxxxxxxxxxx
In the datagrid I would put key values (that I didnt want to
display)
in
columns marked not visible, in the gridview I would put them in
datakeys.
Im need to apply greater control over my html, so Im using the
Repeater for
the first time. Ive added a linkbutton and am looking in its
ItemCommand
event. But so far Im unable to figure out how to hide a key
value,
and then
determine it in ItemCommand.
Any help is greatly appreciated!
Thanks, Mark
















.


Quantcast