By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,309 Members | 1,371 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,309 IT Pros & Developers. It's quick & easy.

LinkButton in Repeater control

P: n/a
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>
...


Aug 21 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Peter Larsen [CPH] wrote:
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
Aug 21 '08 #2

P: n/a
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:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subs...#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/...tance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
>From: "Peter Larsen [CPH]" <Pe*********@community.nospam>
Subject: LinkButton in Repeater control
Date: Thu, 21 Aug 2008 23:11:03 +0200
>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>
...


Aug 22 '08 #3

P: n/a
Hi Steven,

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

BR
Peter
"Steven Cheng [MSFT]" <st*****@online.microsoft.comwrote in message
news:Yg**************@TK2MSFTNGHUB02.phx.gbl...
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

Aug 22 '08 #4

P: n/a
Hei Lloyd,

Thank you for your answer to my question.

BR
Peter.

"Lloyd Sheen" <a@b.cwrote in message
news:%2******************@TK2MSFTNGP05.phx.gbl...
>
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

Aug 22 '08 #5

P: n/a
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:
ms****@microsoft.com.

--------------------
>From: "Peter Larsen [CPH]" <Pe*********@community.nospam>
References: <uZ**************@TK2MSFTNGP04.phx.gbl>
<Yg**************@TK2MSFTNGHUB02.phx.gbl>
>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 [MSFT]" <st*****@online.microsoft.comwrote in message
news:Yg**************@TK2MSFTNGHUB02.phx.gbl...
>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


Aug 25 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.