LinkButton in Repeater control

P

Peter Larsen [CPH]

Hi,

The following sample shows a LinkButton in the HeaderTemplate of a Repeater
control.

The problem is that i'm not able to access the linkbutton in code (in the cs
file) as long as the linkbutton stays in the
repeater control - see the following line:
testLink.CommandArgument = "just testing"; or testLink.Text = "Some new
text";

How do i dynamically change the properties of the LinkButton in runtime ??

Thank you in advance.
BR
Peter



Sample code:

<asp:Repeater id="dataRepeater" runat="server">
<HeaderTemplate>
Header data
<table class="repeat_table">
<tr id="rowHeadLine">
<td id="fieldHeadLine">
<asp:LinkButton
ID="testLink"
runat="server"
OnClick="testfunc"
Text="hit me">
</asp:LinkButton>
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
...
 
L

Lloyd Sheen

Peter said:
Hi,

The following sample shows a LinkButton in the HeaderTemplate of a Repeater
control.

The problem is that i'm not able to access the linkbutton in code (in the cs
file) as long as the linkbutton stays in the
repeater control - see the following line:
testLink.CommandArgument = "just testing"; or testLink.Text = "Some new
text";

How do i dynamically change the properties of the LinkButton in runtime ??

Thank you in advance.
BR
Peter



Sample code:

<asp:Repeater id="dataRepeater" runat="server">
<HeaderTemplate>
Header data
<table class="repeat_table">
<tr id="rowHeadLine">
<td id="fieldHeadLine">
<asp:LinkButton
ID="testLink"
runat="server"
OnClick="testfunc"
Text="hit me">
</asp:LinkButton>
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
...

You want to handle the ItemDataBound event for dataRepeater.

In the event handler check e.Item.ItemType and when it is Header you can
do a e.Item.FindControl("testLink") to get a reference to the
LinkButton. From there change what you need.

Hope this helps
LS
 
S

Steven Cheng [MSFT]

Hi Peter,

As other member suggested, you can register the "ItemCreated" event of
Repeater control and then use "FindControl" to locate the linkbutton. Here
is a complete example (with aspx and codebehind) which demonstrate this:

============aspx =================
<form id="form1" runat="server">
<div>

<asp:Repeater id="dataRepeater" runat="server"
onitemcreated="dataRepeater_ItemCreated">
<HeaderTemplate>
Header data
<table class="repeat_table">
<tr id="rowHeadLine">
<td id="fieldHeadLine">
<asp:LinkButton
ID="testLink"
runat="server"

Text="hit me">
</asp:LinkButton>
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><hr />item</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>

</asp:Repeater>


</div>
</form>

==============code behind=============
public partial class RepeaterPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string[] items = new string[] { "aaa", "bbb", "ccc", "ddd" };

dataRepeater.DataSource = items;
dataRepeater.DataBind();
}
}
protected void dataRepeater_ItemCreated(object sender,
RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Header)
{
LinkButton lbtn = e.Item.FindControl("testLink") as LinkButton;

if (lbtn != null)
{
lbtn.CommandName = "Display"; lbtn.CommandArgument =
"argument";
lbtn.Click += delegate
{
Response.Write("<br/>LinkButton clicked");
};

}
}
}
}
==========================

Hope this helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.


--------------------
From: "Peter Larsen [CPH]" <[email protected]>
Subject: LinkButton in Repeater control
Date: Thu, 21 Aug 2008 23:11:03 +0200
 
P

Peter Larsen [CPH]

Hi Steven,

Thank you for the solution.
It seems to be what i need.

BR
Peter
 
S

Steven Cheng [MSFT]

You're welcome Peter,

I'm glad that it helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

--------------------
From: "Peter Larsen [CPH]" <[email protected]>
References: <[email protected]>
Subject: Re: LinkButton in Repeater control
Date: Fri, 22 Aug 2008 09:37:46 +0200
Hi Steven,

Thank you for the solution.
It seems to be what i need.

BR
Peter


Steven Cheng said:
Hi Peter,

As other member suggested, you can register the "ItemCreated" event of
Repeater control and then use "FindControl" to locate the linkbutton. Here
is a complete example (with aspx and codebehind) which demonstrate this:

============aspx =================
<form id="form1" runat="server">
<div>

<asp:Repeater id="dataRepeater" runat="server"
onitemcreated="dataRepeater_ItemCreated">
<HeaderTemplate>
Header data
<table class="repeat_table">
<tr id="rowHeadLine">
<td id="fieldHeadLine">
<asp:LinkButton
ID="testLink"
runat="server"

Text="hit me">
</asp:LinkButton>
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><hr />item</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>

</asp:Repeater>


</div>
</form>

==============code behind=============
public partial class RepeaterPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string[] items = new string[] { "aaa", "bbb", "ccc", "ddd" };

dataRepeater.DataSource = items;
dataRepeater.DataBind();
}
}
protected void dataRepeater_ItemCreated(object sender,
RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Header)
{
LinkButton lbtn = e.Item.FindControl("testLink") as LinkButton;

if (lbtn != null)
{
lbtn.CommandName = "Display"; lbtn.CommandArgument =
"argument";
lbtn.Click += delegate
{
Response.Write("<br/>LinkButton clicked");
};

}
}
}
}
==========================

Hope this helps.

Sincerely,

Steven Cheng
 

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
474,056
Messages
2,570,439
Members
47,101
Latest member
DoloresHol

Latest Threads

Top