setting ItemTemplate value to DataList item index?

Discussion in 'ASP .Net' started by Stimp, Sep 10, 2004.

  1. Stimp

    Stimp Guest

    I have a datalist that outputs a link to a picture in each ItemTemplate:

    e.g.
    <asp:DataList id="DataList1" ...etc>
    <ItemTemplate>
    <a href='Pic.aspx?pic=somenumber'>picture</a>
    </ItemTemplate>
    <asp:DataList>

    What I want to do is set the "pic=somenumber" querystring value
    to the current DataList item's index.

    e.g. something like:

    Pic.aspx?pic=<%=DataList1.Items.Item.ItemIndex%>

    This returns an error.

    How can I retrieve the index of the current Item as it's loading the
    template?
    I could also create my own index value, that increments as the
    DataList adds another ItemTemplate.. but not sure how to do this.

    Thanks!
    Peter
    --

    "I hear ma train a comin'
    .... hear freedom comin"
     
    Stimp, Sep 10, 2004
    #1
    1. Advertising

  2. Stimp

    Weston Weems Guest

    I dont have a development machine in front of me at the
    moment, but I do know that a lot of other .net
    databindable lists have OnItemDataBound, and OnItemCreated
    events.

    If it has a OnItemDataBound event, it'd be REAL easy.
    throw some winforms control in the template, then you can
    modify it from the code behind

    if(e.ItemType == ListItemType.ListItem){
    ((LinkButton) e.FindControl("myLinkButton")).LinkUrl =
    e.Item.ItemIndex;


    }

    Something along those lines. I know it works for DataGrid
    and I am pretty sure data list too, but I dont think it
    works with repeater.


    >-----Original Message-----
    >I have a datalist that outputs a link to a picture in

    each ItemTemplate:
    >
    >e.g.
    ><asp:DataList id="DataList1" ...etc>
    > <ItemTemplate>
    > <a href='Pic.aspx?pic=somenumber'>picture</a>
    > </ItemTemplate>
    ><asp:DataList>
    >
    >What I want to do is set the "pic=somenumber" querystring

    value
    >to the current DataList item's index.
    >
    >e.g. something like:
    >
    >Pic.aspx?pic=<%=DataList1.Items.Item.ItemIndex%>
    >
    >This returns an error.
    >
    >How can I retrieve the index of the current Item as it's

    loading the
    >template?
    >I could also create my own index value, that increments

    as the
    >DataList adds another ItemTemplate.. but not sure how to

    do this.
    >
    >Thanks!
    >Peter
    >--
    >
    >"I hear ma train a comin'
    >.... hear freedom comin"
    >.
    >
     
    Weston Weems, Sep 10, 2004
    #2
    1. Advertising

  3. P.S. Per Weston's comment, I too prefer to do most things of any complexity
    in the code-behind. What he suggested will work great.

    "Bill Borg" wrote:

    > Peter,
    >
    > Two ways:
    >
    > 1. Use '<%# container.itemindex %>'
    >
    > 2. Create a public function on the page that bumps and returns a static
    > counter, e.g. (in vb)
    >
    > Private m_Num As Integer = -1
    > Public Function NextNum() As Integer
    > m_Num += 1
    > Return m_Num
    > End Function
    >
    > then...
    >
    > '<%# NextNum() %>'
    >
    > The first is easier, the second more extensible.
    >
    > hth,
    >
    > Bill
    >
    > "Stimp" wrote:
    >
    > > I have a datalist that outputs a link to a picture in each ItemTemplate:
    > >
    > > e.g.
    > > <asp:DataList id="DataList1" ...etc>
    > > <ItemTemplate>
    > > <a href='Pic.aspx?pic=somenumber'>picture</a>
    > > </ItemTemplate>
    > > <asp:DataList>
    > >
    > > What I want to do is set the "pic=somenumber" querystring value
    > > to the current DataList item's index.
    > >
    > > e.g. something like:
    > >
    > > Pic.aspx?pic=<%=DataList1.Items.Item.ItemIndex%>
    > >
    > > This returns an error.
    > >
    > > How can I retrieve the index of the current Item as it's loading the
    > > template?
    > > I could also create my own index value, that increments as the
    > > DataList adds another ItemTemplate.. but not sure how to do this.
    > >
    > > Thanks!
    > > Peter
    > > --
    > >
    > > "I hear ma train a comin'
    > > .... hear freedom comin"
    > >
     
    =?Utf-8?B?QmlsbCBCb3Jn?=, Sep 10, 2004
    #3
  4. Stimp

    Stimp Guest

    On Fri, 10 Sep 2004 Bill Borg <> wrote:
    > Peter,
    >
    > Two ways:
    >
    > 1. Use '<%# container.itemindex %>'


    That's EXACTLY what I was looking for!

    Thanks to all who replied!
    --

    "I hear ma train a comin'
    .... hear freedom comin"
     
    Stimp, Sep 10, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vannela
    Replies:
    1
    Views:
    586
    Oleg Medyanik
    Dec 12, 2003
  2. Nevyn Twyll
    Replies:
    8
    Views:
    1,972
  3. Bennett Haselton
    Replies:
    1
    Views:
    5,411
    Joe Fallon
    Sep 24, 2004
  4. Adam Knight

    Output DataList Item Index!

    Adam Knight, Sep 28, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    2,318
    Brock Allen
    Sep 29, 2005
  5. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    360
    Tomasz Chmielewski
    Mar 4, 2008
Loading...

Share This Page