can code inside a Repeater's ItemTemplate modify controls in the ItemTemplate?

Discussion in 'ASP .Net' started by Bennett Haselton, Sep 24, 2004.

  1. If I have this code for repeater, it gives a compilation error saying
    "The type or namespace name 'MyValue2' could not be found":

    <asp:Repeater id="MyList" runat="server">
    <ItemTemplate>
    <%#Container.DataItem%><br>
    <% MyValue2.Text = "ghi"; %>
    <asp:TextBox ID="MyValue2" Runat="server"/>
    <br>
    </ItemTemplate>
    </asp:Repeater>

    Presumably that's because the MyValue2 control is inside an
    ItemTemplate that gets repeated multiple times, so each instance of
    MyValue2 gets a unique ID when the page is actually rendered, and the
    C# code can't find the "MyValue2" because they were given a series of
    unique names instead. (In fact I can even see their IDs in the source
    code of the rendered page -- MyList__ctl0_MyValue2,
    MyList__ctl1_MyValue2, etc.)

    So, is there a way to have inline code inside the ItemTemplate that
    can modify a control that's also repeated inside the template?

    I have four ASP.Net books and I can't find any examples in any of them
    showing how to do this... Maybe it can't be done?

    -Bennett
     
    Bennett Haselton, Sep 24, 2004
    #1
    1. Advertising

  2. Bennett Haselton

    Joe Fallon Guest

    I use the ItemDatBound in my grids to manipulate stuff.
    I assume you can do the same for a Repeater.
    ===========================================
    Sample code:
    ===========================================
    Private Sub dg1_ItemDataBound(ByVal sender As System.Object, ByVal e As
    DataGridItemEventArgs) Handles dg1.ItemDataBound
    If e.Item.ItemType = ListItemType.Header Then
    'do something to the header
    End If

    If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType =
    ListItemType.AlternatingItem Then
    'need to take paging into account!
    Dim mIndex As Integer = CType(sender, DataGrid).PageSize *
    CType(sender, DataGrid).CurrentPageIndex + e.Item.ItemIndex

    'set Status hyperlink
    Dim oHylStatus As HyperLink = CType(e.Item.FindControl("hylStatus"),
    HyperLink)
    If mCollection(mIndex).statustext = "None" Then
    oHylStatus.NavigateUrl = ""
    Else
    oHylStatus.NavigateUrl = "javascript:..;"
    End If
    oHylStatus.Text = mCollection(mIndex).statustext

    If mCanAmend=True Then
    Dim oBtnAmend As ImageButton = CType(e.Item.FindControl("btnAmend"),
    ImageButton)
    If mCollection(mIndex).status = "XYZ" Then
    oBtnAmend.Visible = True
    Else
    oBtnAmend.Visible = False
    End If
    End If

    End If
    End Sub

    --
    Joe Fallon




    "Bennett Haselton" <> wrote in message
    news:...
    > If I have this code for repeater, it gives a compilation error saying
    > "The type or namespace name 'MyValue2' could not be found":
    >
    > <asp:Repeater id="MyList" runat="server">
    > <ItemTemplate>
    > <%#Container.DataItem%><br>
    > <% MyValue2.Text = "ghi"; %>
    > <asp:TextBox ID="MyValue2" Runat="server"/>
    > <br>
    > </ItemTemplate>
    > </asp:Repeater>
    >
    > Presumably that's because the MyValue2 control is inside an
    > ItemTemplate that gets repeated multiple times, so each instance of
    > MyValue2 gets a unique ID when the page is actually rendered, and the
    > C# code can't find the "MyValue2" because they were given a series of
    > unique names instead. (In fact I can even see their IDs in the source
    > code of the rendered page -- MyList__ctl0_MyValue2,
    > MyList__ctl1_MyValue2, etc.)
    >
    > So, is there a way to have inline code inside the ItemTemplate that
    > can modify a control that's also repeated inside the template?
    >
    > I have four ASP.Net books and I can't find any examples in any of them
    > showing how to do this... Maybe it can't be done?
    >
    > -Bennett
     
    Joe Fallon, Sep 24, 2004
    #2
    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. AC [MVP MOSS]
    Replies:
    2
    Views:
    554
    AC [MVP MOSS]
    Feb 13, 2007
  2. Nathan Sokalski
    Replies:
    2
    Views:
    1,166
    Coskun SUNALI [MVP]
    Dec 26, 2007
  3. Oleg
    Replies:
    1
    Views:
    7,501
    Cowboy \(Gregory A. Beamer\)
    Mar 7, 2008
  4. Andy B
    Replies:
    1
    Views:
    1,015
    bruce barker
    Nov 15, 2008
  5. Replies:
    0
    Views:
    207
Loading...

Share This Page