Determining hidden key values with the Repeater control

G

Guest

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
 
T

Teemu Keiski

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/a...-hierarchy-of-ASP.NET-databound-controls.aspx
 
J

James Irvine

SandpointGuy said:
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


protected void DataList1_ItemCommand(Object sender, DataListCommandEventArgs e) // an image was clicked on
{
String key = DataList1.DataKeys[e.Item.ItemIndex].ToString();
....

}
 
T

Teemu Keiski

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 said:
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 said:
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/a...-hierarchy-of-ASP.NET-databound-controls.aspx


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

Teemu Keiski

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 said:
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 said:
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 said:
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/a...-hierarchy-of-ASP.NET-databound-controls.aspx


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



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
 
E

Eliyahu Goldin

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 said:
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 said:
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 said:
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


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/a...-hierarchy-of-ASP.NET-databound-controls.aspx


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



message 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
 
T

Teemu Keiski

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 said:
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 said:
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 said:
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


message 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


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/a...-hierarchy-of-ASP.NET-databound-controls.aspx


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



message 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
 
E

Eliyahu Goldin

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 said:
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 said:
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 said:
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.


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


message 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


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/a...-hierarchy-of-ASP.NET-databound-controls.aspx


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



message 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
 
T

Teemu Keiski

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 said:
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 said:
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 said:
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


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.


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


message 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


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/a...-hierarchy-of-ASP.NET-databound-controls.aspx


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



message 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top