Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Repeater Help

Status
Not open for further replies.

baden100

Programmer
Apr 26, 2006
10
US
I have a repeater that displays an alphabetized list.

When the first letter changes, I want to add something like
<br /> <b>[LETTER]</b> <br />

thus:

A
aword
anotherword
anothernotherword

B
bent
bendover

...



I was thinking of some type of OnItemCreated event handler:

Code:
    protected void Repeater1_ItemCreated(object source, RepeaterItemEventArgs e)
    {
        firstLetter = e.Item.DataItem.ToString().Substring(0, 1);
        //Response.Write("Letter: " + firstLetter + "<br>");
        
    }

But I can't quite get this working.

I was looking at - but that's too complex of a solution for this.


Suggestions?
 
declare a global variable that will track the change.



String oldFirstLetter;

protected void Repeater1_ItemCreated(object source, RepeaterItemEventArgs e)
{
firstLetter = e.Item.DataItem.ToString().Substring(0, 1);
if(oldFirstLetter!=firstLetter)
{
oldFirstLetter=firstLetter;
//Write code here to add a new literalcontrol
LiteralControl newCtrl=new LiteralControl("Letter: " + firstLetter + "<br>");
e.Item.Controls.AddAt(0,newCtrl);
}
}


Note:
I am not used to C#, so the code may cotain errors...

Known is handfull, Unknown is worldfull
 
Ah,

With a little fandangling, it's working perfectly:

Code:
 string oldFirstLetter;

   protected void Repeater1_ItemCreated(object source, RepeaterItemEventArgs e)
    {
        string firstLetter = Convert.ToString(DataBinder.Eval(e.Item.DataItem, "Term")).Substring(0,1);

        if (oldFirstLetter != firstLetter)
        {
            oldFirstLetter = firstLetter;
            //Write code here to add a new literalcontrol
            LiteralControl newCtrl = new LiteralControl("<b>Letter: " + firstLetter + "</b><br>");
            e.Item.Controls.AddAt(0, newCtrl);            
        }
    }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top