473,386 Members | 1,790 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Problem with Repeater.ItemIndex

I am trying to make a "tabbed" interface by iterating through a dataset with
a conditional statement. For example:

----------------------------------------------------------------------------------------------------------------------
| <a href="config.aspx?siteid=FIEJGIE">Site 1</a| Site 2 | <a
href="config.aspx?siteid=DFOWEMF">Site 3</a>|

In the example above site 2 is the "current" tab.

I have the following code at the top of my aspx page:

protected string GenerateTabLink(string SiteID) {
string strSiteID = SiteID;
int intSiteNum = tabs_repeater.ItemIndex;

System.Text.StringBuilder strLink = new
System.Text.StringBuilder();

if (strSiteID == Request.QueryString["siteid"]){
strLink.Append("Site ");
strLink.Append(intSiteNum + 1);
} else {
strLink.Append("<a
href=\"tabpage.aspx?siteid=");
strLink.Append(Eval("site_id"));
strLink.Append("\">Site ");
strLink.Append(intSiteNum + 1);
strLink.Append("</a>");
}
return strLink.ToString();
}

<..... Later in the page .....>

<ASP:Repeater id="tabs_repeater" DataSourceID="tabs_repeater_datasource"
runat="server">
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<li><%#GenerateTabLink(Eval("site_id"))%></li>
</ItemTemplate>
<FooterTemplate>
</FooterTemplate>
</ASP:Repeater>
I'm getting an error message indicating: The name 'tabs_repeater' does not
exist in the current context

I've also tried passing "tabs_repeater.ItemIndex" as a parameter to the
function with no luck also. Could anyone clue me in on what my problem is
and/or how I can get this working?

Thanks in advance,
Brad
Feb 25 '07 #1
5 3811
Hello Brad,

Based on your description and the code snippet, you're building a custom
function to generate some dynamic html fragment that need to embed in each
repeater item(based on the item's ItemIndex) ,correct?

The problem you met here is due to the "ItemIndex" is not a property of
Repeater control, but of RepeaterItem control. To reference ItemIndex, you
need to get the reference to the RepeaterItem (of each row). For such
scenario, I think you should use either of the following means:

1. You can add a paramter on your custom helper function and pass the
ItemIndex into the function from the databinding <%# %expression. e.g.

=====helper function==========
protected string GetTemplate(int index)
{
return "index: " + index;
}

======repeater template========
....................
<ItemTemplate>
<br /><hr /><br />
<%# GetTemplate(Container.ItemIndex) %>
</ItemTemplate>
</asp:Repeater>
====================
#you can find some other useful property on the "Container" object
2. Or you can use the Repeater.ItemCreated event to programamtically add
custom sub controls .e.g.

========repeater tempalte=============
<ItemTemplate>
<br /><hr /><br />
subcontrols:<br />
<asp:PlaceHolder ID="holder" runat="server"></asp:PlaceHolder>

</ItemTemplate>
</asp:Repeater>

========ItemCreated event===========
protected void Repeater1_ItemCreated(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
PlaceHolder holder = e.Item.FindControl("holder") as
PlaceHolder;

LiteralControl control = new LiteralControl();
control.Text = "<br/><a href=\"#\"ItemIndex: " +
e.Item.ItemIndex + "</a>" ;

holder.Controls.Add(control);

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

Hope this helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

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://msdn.microsoft.com/subscripti...t/default.aspx.

==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Feb 26 '07 #2
Steven -

Thanks for your post. I think you understand what I am trying to accomplish
but neither of your examples completely encompass what I'm trying to
achieve. I have tried to apply your examples to my code but I'm not having
much luck. (Please bear in mind I'm a ASP.net/C# novice) :-)

I am actually trying to work with two variables - SiteNum and SiteID.

SiteNum - is a numeric value between 1 and n. This value should be set to
Container.ItemIndex. This value is not stored in any databases - it's just
for display purposes to differentiate the tabs.

SiteID - is an alphanumeric value which is obtained through a database
record populated by the repeater datasource. Its used in forming the actual
hyperlink.

I need to combine both variables to produce a dynamic html code fragment.
So for instance the code might produce: "<a href="sdof3k45j4">Site 1</a>"
(sans quotes) Or it might produce: "Site 1" (without a link) It all depends
on if SiteID of the current record matches Request.QueryString["siteid"]).

In the first example you provided it looks like your only outputting the
SiteNum not the SiteID. I tried to combine the function you provided into my
existing function with no luck:

====== Generate Tab Link Function =======

protected string GenerateTabLink(string SiteID, int SiteNum) {
string strSiteID = SiteID;
int intSiteNum = SiteNum;

System.Text.StringBuilder strLink = new System.Text.StringBuilder();

if (strSiteID == Request.QueryString["siteid"]){
strLink.Append("Site ");
strLink.Append(intSiteNum + 1);
} else {
strLink.Append("<a href=\"config.aspx?siteid=");
strLink.Append(Eval("site_id"));
strLink.Append("\">Site ");
strLink.Append(intSiteNum + 1);
strLink.Append("</a>");
}

return strLink.ToString();
}
====== Repeater Template =======
<ASP:Repeater id="tabs_repeater" DataSourceID="tabs_repeater_datasource"
runat="server">
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<li><%# GenerateTabLink((Eval("site_id")),Container.ItemIn dex) %></li>
</ItemTemplate>
<FooterTemplate>
</FooterTemplate>
</ASP:Repeater>
In the second example - it seems like your trying to populate the repeater
as its created? But beyond that I'm not sure the other code does.

I'm still wrangling with your examples but if you could provide any
additional assistance I would be very grateful.

Thank You,
Brad
"Steven Cheng[MSFT]" <st*****@online.microsoft.comwrote in message
news:8X**************@TK2MSFTNGHUB02.phx.gbl...
Hello Brad,

Based on your description and the code snippet, you're building a custom
function to generate some dynamic html fragment that need to embed in each
repeater item(based on the item's ItemIndex) ,correct?

The problem you met here is due to the "ItemIndex" is not a property of
Repeater control, but of RepeaterItem control. To reference ItemIndex, you
need to get the reference to the RepeaterItem (of each row). For such
scenario, I think you should use either of the following means:

1. You can add a paramter on your custom helper function and pass the
ItemIndex into the function from the databinding <%# %expression. e.g.

=====helper function==========
protected string GetTemplate(int index)
{
return "index: " + index;
}

======repeater template========
...................
<ItemTemplate>
<br /><hr /><br />
<%# GetTemplate(Container.ItemIndex) %>
</ItemTemplate>
</asp:Repeater>
====================
#you can find some other useful property on the "Container" object
2. Or you can use the Repeater.ItemCreated event to programamtically add
custom sub controls .e.g.

========repeater tempalte=============
<ItemTemplate>
<br /><hr /><br />
subcontrols:<br />
<asp:PlaceHolder ID="holder" runat="server"></asp:PlaceHolder>

</ItemTemplate>
</asp:Repeater>

========ItemCreated event===========
protected void Repeater1_ItemCreated(object sender, RepeaterItemEventArgs
e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
PlaceHolder holder = e.Item.FindControl("holder") as
PlaceHolder;

LiteralControl control = new LiteralControl();
control.Text = "<br/><a href=\"#\"ItemIndex: " +
e.Item.ItemIndex + "</a>" ;

holder.Controls.Add(control);

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

Hope this helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

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://msdn.microsoft.com/subscripti...t/default.aspx.

==================================================

This posting is provided "AS IS" with no warranties, and confers no
rights.



Feb 27 '07 #3
Hello Brad,

Thanks for your reply.

For your further questions, here are my suggestion:

In the first example you provided it looks like your only outputting the
SiteNum not the SiteID. I tried to combine the function you provided into
my
existing function with no luck:
==============================

Sure, you can add more parameters to the helper function and pass into
additional values if you have. As for the following expression:

<li><%# GenerateTabLink((Eval("site_id")),Container.ItemIn dex) %></li>

I don't see any problem here, the only possible issue is that the "site_id"
is not a string paramter, so that it mismatch the below function:

protected string GenerateTabLink(string SiteID, int SiteNum)

If "site_id" field is not string, you may need to use "ToString()" to
output it as string .e.g.
<li><%# GenerateTabLink((Eval("site_id").ToString()),Conta iner.ItemIndex)
%></li>
BTW, what's the exact error message you got?

In the second example - it seems like your trying to populate the repeater
as its created? But beyond that I'm not sure the other code does.
===============================
The "ItemCreated" event will fire each time a RepeaterItem(a row in a
repeater) has been created. And we can do some customization at that time,
such as add some additional controls or adjust some existing controls in
each ItemTemplate.

#Repeater.ItemCreated Event
http://msdn2.microsoft.com/en-us/lib...trols.repeater.
itemcreated(VS.71).aspx

I mentioned this event is because what you want to is more like add some
additional controls into "RepeaterItem" dynamically rather than display
some text through databinding. Therefore, using <%# %expression to output
html markup is not quite recommended. If posible, you're prefered to use
ItemCreated event for adding additional controls into RepeaterItem.
Please feel free to let me know if you have any more specific questions,
I'd be glad to assist.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.


Feb 27 '07 #4
Steven -

You were spot on - all my problems revolved around the fact that siteid was
not a string - it was a GUID containing letters, numbers and dashes. I
stripped the dashes out and my code runs perfectly now. I owe you a debit of
gratitude - thank you so much!

Best Regards,
Brad Baker
"Steven Cheng[MSFT]" <st*****@online.microsoft.comwrote in message
news:%2****************@TK2MSFTNGHUB02.phx.gbl...
Hello Brad,

Thanks for your reply.

For your further questions, here are my suggestion:

In the first example you provided it looks like your only outputting the
SiteNum not the SiteID. I tried to combine the function you provided into
my
existing function with no luck:
==============================

Sure, you can add more parameters to the helper function and pass into
additional values if you have. As for the following expression:

<li><%# GenerateTabLink((Eval("site_id")),Container.ItemIn dex) %></li>

I don't see any problem here, the only possible issue is that the
"site_id"
is not a string paramter, so that it mismatch the below function:

protected string GenerateTabLink(string SiteID, int SiteNum)

If "site_id" field is not string, you may need to use "ToString()" to
output it as string .e.g.
<li><%# GenerateTabLink((Eval("site_id").ToString()),Conta iner.ItemIndex)
%></li>
BTW, what's the exact error message you got?

In the second example - it seems like your trying to populate the repeater
as its created? But beyond that I'm not sure the other code does.
===============================
The "ItemCreated" event will fire each time a RepeaterItem(a row in a
repeater) has been created. And we can do some customization at that time,
such as add some additional controls or adjust some existing controls in
each ItemTemplate.

#Repeater.ItemCreated Event
http://msdn2.microsoft.com/en-us/lib...trols.repeater.
itemcreated(VS.71).aspx

I mentioned this event is because what you want to is more like add some
additional controls into "RepeaterItem" dynamically rather than display
some text through databinding. Therefore, using <%# %expression to
output
html markup is not quite recommended. If posible, you're prefered to use
ItemCreated event for adding additional controls into RepeaterItem.
Please feel free to let me know if you have any more specific questions,
I'd be glad to assist.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no
rights.


Mar 1 '07 #5
You're welcome :-)

Have a good day!

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
Mar 1 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Stephen Miller | last post by:
I am using the OnItemDataBound event of Repeater control to nest a DataGrid within the Repeater. When I attempt to bind to the DataGrid using the DataSource method I get the error message "Object...
1
by: Fraggle | last post by:
I have a repeater with controls added at run time. the <template> also contains a <asp:textbox that is made visible on some repeater elements. when I come to read the text info out it has...
2
by: News | last post by:
Hi I need help to display and edit data in a data grid within a repeater. The code is below: Sub dgrdEvents_EditCommand(sender As Object, e As DataGridCommandEventArgs)...
1
by: Bryan | last post by:
I trying to figure out out to validate a textbox inside a repeater once a person presses a button on a repeater. Here's the validation Code behind: Please be aware that line e.Item.ItemIndex...
3
by: Roshawn Dawson | last post by:
Hi, I have a repeater control. This control will render a number of textboxes on the page. Each textbox has its onblur event wired to a JScript function. Here's a sample: <asp:textbox...
7
by: charliewest | last post by:
Hello - I'm using a Repeater control to render information in a very customized grid-like table. The Repeater control is binded to a DataSet with several records of information. Within the...
5
by: RC- | last post by:
Hi everyone, I have been searching and searching for an answer to this question using Google and what not; I have not been able to find a "clear cut" answer. OK, now the question: I have a...
7
by: Brad Baker | last post by:
I am trying to programmatically set a placeholder control in csharp which is nested inside a repeater control between <ItemTemplateand </ItemTemplate> tags, however I am running into problems. I've...
2
by: dm3281 | last post by:
Hi all -- I have a strange issue. I have obtained a scripted database and compiled ASP.NET 2.0 application from a sister site that I'm trying to implement locally. I have successfully...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.