Problem with Repeater.ItemIndex

B

Brad Baker

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
 
S

Steven Cheng[MSFT]

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/subscriptions/managednewsgroups/default.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/subscriptions/support/default.aspx.

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



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

Brad Baker

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.ItemIndex) %></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
 
S

Steven Cheng[MSFT]

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.ItemIndex) %></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()),Container.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/library/system.web.ui.webcontrols.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.
 
B

Brad Baker

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
 
S

Steven Cheng[MSFT]

You're welcome :)

Have a good day!

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top