Finding a control inside an <itemtemplate>

Discussion in 'ASP .Net' started by Jim in Arizona, Jan 4, 2007.

  1. How do I find a control within a datalist itemtemplate from a sub
    procedure that isn't a normal called procedure from a datalist (like the
    update, edit, or cancel procedures)?

    For instance, normally you find a control and access it's properties and
    methods, like so:

    Sub Update(ByVal sender As Object, ByVal e As DataListCommandEventArgs)

    Dim txtPosted As New TextBox
    txtPosted = e.Item.FindControl("txtPosted")

    End Sub

    How could I do this using a different procedure, like:

    Sub AnotherProcedure()


    End Sub


    TIA,
    Jim
    Jim in Arizona, Jan 4, 2007
    #1
    1. Advertising

  2. The DataList exposes Items collection. navigate to the required item and do
    the same FindControl.

    --
    Eliyahu Goldin,
    Software Developer & Consultant
    Microsoft MVP [ASP.NET]
    http://msmvps.com/blogs/egoldin
    http://usableasp.net


    "Jim in Arizona" <> wrote in message
    news:%...
    > How do I find a control within a datalist itemtemplate from a sub
    > procedure that isn't a normal called procedure from a datalist (like the
    > update, edit, or cancel procedures)?
    >
    > For instance, normally you find a control and access it's properties and
    > methods, like so:
    >
    > Sub Update(ByVal sender As Object, ByVal e As DataListCommandEventArgs)
    >
    > Dim txtPosted As New TextBox
    > txtPosted = e.Item.FindControl("txtPosted")
    >
    > End Sub
    >
    > How could I do this using a different procedure, like:
    >
    > Sub AnotherProcedure()
    >
    >
    > End Sub
    >
    >
    > TIA,
    > Jim
    Eliyahu Goldin, Jan 4, 2007
    #2
    1. Advertising

  3. Jim in Arizona

    clintonG Guest

    Enable trace in the page. Load the page and read the trace to identify the
    control in the control tree hierarchy. We have to use the FindControl method
    on each object in that hierachy for those instances when objects are deeply
    nested into templates. Don't forget to cast the found object to the proper
    type.


    <%= Clinton Gallagher
    NET csgallagher AT metromilwaukee.com
    URL http://clintongallagher.metromilwaukee.com/
    MAP http://wikimapia.org/#y=43038073&x=-88043838&z=17&l=0&m=h


    "Jim in Arizona" <> wrote in message
    news:%...
    > How do I find a control within a datalist itemtemplate from a sub
    > procedure that isn't a normal called procedure from a datalist (like the
    > update, edit, or cancel procedures)?
    >
    > For instance, normally you find a control and access it's properties and
    > methods, like so:
    >
    > Sub Update(ByVal sender As Object, ByVal e As DataListCommandEventArgs)
    >
    > Dim txtPosted As New TextBox
    > txtPosted = e.Item.FindControl("txtPosted")
    >
    > End Sub
    >
    > How could I do this using a different procedure, like:
    >
    > Sub AnotherProcedure()
    >
    >
    > End Sub
    >
    >
    > TIA,
    > Jim
    clintonG, Jan 4, 2007
    #3
  4. Eliyahu Goldin wrote:
    > The DataList exposes Items collection. navigate to the required item and do
    > the same FindControl.
    >


    Could you please provide an example? I've looked around the net and
    can't seem to find one anywhere.

    I still consider myself a newbie when it comes to this stuff and am just
    now starting to harness the power of the datalist control.
    Jim in Arizona, Jan 4, 2007
    #4
  5. Jim in Arizona wrote:
    > How do I find a control within a datalist itemtemplate from a sub
    > procedure that isn't a normal called procedure from a datalist (like the
    > update, edit, or cancel procedures)?
    >
    > For instance, normally you find a control and access it's properties and
    > methods, like so:
    >
    > Sub Update(ByVal sender As Object, ByVal e As DataListCommandEventArgs)
    >
    > Dim txtPosted As New TextBox
    > txtPosted = e.Item.FindControl("txtPosted")
    >
    > End Sub
    >
    > How could I do this using a different procedure, like:
    >
    > Sub AnotherProcedure()
    >
    >
    > End Sub
    >
    >
    > TIA,
    > Jim


    After some experimentation and trying to follow examples at this location:
    http://quickstarts.asp.net/QuickStartv20/aspnet/doc/ctrlref/data/datalist.aspx

    I have come up this this code:

    Sub HideOrShowButton()

    Dim i As Integer
    Dim lblDate As New Label
    Dim btnUpdate As New Button
    For i = 0 To dlNotes.Items.Count - 1

    lblDate = dlNotes.Items(i).FindControl("lblDate")
    btnUpdate = dlNotes.Items(i).FindControl("btnUpdate")

    If lblDate.Text = "1/4/2007 10:15:00 AM" Then
    btnUpdate.Visible = True
    Else
    btnUpdate.Visible = False
    End If
    Next

    End Sub

    This is unrefined, but is a good start to what I'm hoping for. My goal
    is to make visible or invisible a button based on the original post time
    of the db record.

    If anyone can think of a better way of doing this that's not overly
    complicated, please post!

    Thanks to those who replied.
    Jim in Arizona, Jan 4, 2007
    #5
  6. This code is absolutely fine.

    --
    Eliyahu Goldin,
    Software Developer & Consultant
    Microsoft MVP [ASP.NET]
    http://msmvps.com/blogs/egoldin


    "Jim in Arizona" <> wrote in message
    news:...
    > Jim in Arizona wrote:
    > > How do I find a control within a datalist itemtemplate from a sub
    > > procedure that isn't a normal called procedure from a datalist (like the
    > > update, edit, or cancel procedures)?
    > >
    > > For instance, normally you find a control and access it's properties and
    > > methods, like so:
    > >
    > > Sub Update(ByVal sender As Object, ByVal e As DataListCommandEventArgs)
    > >
    > > Dim txtPosted As New TextBox
    > > txtPosted = e.Item.FindControl("txtPosted")
    > >
    > > End Sub
    > >
    > > How could I do this using a different procedure, like:
    > >
    > > Sub AnotherProcedure()
    > >
    > >
    > > End Sub
    > >
    > >
    > > TIA,
    > > Jim

    >
    > After some experimentation and trying to follow examples at this location:
    >

    http://quickstarts.asp.net/QuickStartv20/aspnet/doc/ctrlref/data/datalist.aspx
    >
    > I have come up this this code:
    >
    > Sub HideOrShowButton()
    >
    > Dim i As Integer
    > Dim lblDate As New Label
    > Dim btnUpdate As New Button
    > For i = 0 To dlNotes.Items.Count - 1
    >
    > lblDate = dlNotes.Items(i).FindControl("lblDate")
    > btnUpdate = dlNotes.Items(i).FindControl("btnUpdate")
    >
    > If lblDate.Text = "1/4/2007 10:15:00 AM" Then
    > btnUpdate.Visible = True
    > Else
    > btnUpdate.Visible = False
    > End If
    > Next
    >
    > End Sub
    >
    > This is unrefined, but is a good start to what I'm hoping for. My goal
    > is to make visible or invisible a button based on the original post time
    > of the db record.
    >
    > If anyone can think of a better way of doing this that's not overly
    > complicated, please post!
    >
    > Thanks to those who replied.
    Eliyahu Goldin, Jan 4, 2007
    #6
    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. Bennett Haselton
    Replies:
    1
    Views:
    5,332
    Joe Fallon
    Sep 24, 2004
  2. Charlie@CBFC
    Replies:
    0
    Views:
    382
    Charlie@CBFC
    May 17, 2006
  3. Charlie@CBFC
    Replies:
    2
    Views:
    535
    Charlie@CBFC
    May 18, 2006
  4. Replies:
    0
    Views:
    147
  5. Vlad
    Replies:
    3
    Views:
    176
    R. Thomas, aka Xtreme.Net
    Feb 22, 2005
Loading...

Share This Page