How do I programatically reference controls inside a control template?

Discussion in 'ASP .Net Web Controls' started by Mark Micallef, Apr 10, 2007.

  1. I have a set of controls inside a template like this:

    <ajax:ReorderList ID="rolList" ... >

    ....

    <InsertItemTemplate>
    <asp:TextBox ID="txtText" ... />
    ...
    </InsertItemTemplate>
    </ajax:ReorderList>

    In my codebehind, I want to get at that textbox and do some stuff with
    it in code, but I just can't get a handle to it! I've tried using
    FindControl() but I always returns null. For example:

    TextBox txtText = rolList.FindControl("txtText");

    And variations thereof. Any ideas on this one? It's really starting to
    hurt...

    Thanks,
    Mark
     
    Mark Micallef, Apr 10, 2007
    #1
    1. Advertising

  2. Mark Micallef

    KyleK Guest

    RE: How do I programatically reference controls inside a control templ

    Mark,

    What event are you working in? I usually cast the EventArg to a control then
    do find control on it.

    --
    Kyle Kelin
    ..NET Developer


    "Mark Micallef" wrote:

    > I have a set of controls inside a template like this:
    >
    > <ajax:ReorderList ID="rolList" ... >
    >
    > ....
    >
    > <InsertItemTemplate>
    > <asp:TextBox ID="txtText" ... />
    > ...
    > </InsertItemTemplate>
    > </ajax:ReorderList>
    >
    > In my codebehind, I want to get at that textbox and do some stuff with
    > it in code, but I just can't get a handle to it! I've tried using
    > FindControl() but I always returns null. For example:
    >
    > TextBox txtText = rolList.FindControl("txtText");
    >
    > And variations thereof. Any ideas on this one? It's really starting to
    > hurt...
    >
    > Thanks,
    > Mark
    >
    >
     
    KyleK, Apr 10, 2007
    #2
    1. Advertising

  3. Re: How do I programatically reference controls inside a control templ

    Hey Kyle, sorry for not getting back sooner. I'm working off the
    OnInsertCommand event, which points to my method. Based on my testing
    and fiddling, it almost seems that the OnInsertCommand is not firing
    for reasons I can't determine.

    Here's a really stripped down version of my control definition with
    the event attribute settings:

    <ajax:ReorderList ...
    OnInsertCommand="onInsert"
    OnUpdateCommand="onUpdate">

    <EditItemTemplate>
    <div class="editArea">
    <asp:Button ID="cmdUpdate" runat="server"
    Text="Update" CommandName="update" />
    <asp:DropDownList ID="lstX" runat="server" ... />
    ...
    </div>
    </EditItemTemplate>

    <InsertItemTemplate>
    <div class="insertArea">
    <asp:DropDownList ID="lstX" runat="server" ... />
    <asp:Button ID="cmdAdd" runat="server" Text="Add"
    CommandName="insert" />
    </div>
    </InsertItemTemplate>

    ...

    </ajax:ReorderList>

    Now, the funny this is that the update handler works fine, but the
    insert handler does not. Here's what I mean. I have the following
    little debug handler for the update command which writes the value
    from one of the controls inside the reorderlist template to a label on
    the form:

    protected void onUpdate(object sender, ReorderListCommandEventArgs e)
    {
    DropDownList lstX = (DropDownList)e.Item.FindControl("lstX");
    DebugLabel.Text = "Update: xID: " + lstX.SelectedValue;
    }

    When I click add, the DebugLabel get's the value from lstX, plus the
    database stuff at the back end happens perfectly. Now, here's my
    insert method:

    protected void onInsert(object sender, ReorderListCommandEventArgs e)
    {
    DropDownList lstX = (DropDownList)e.Item.FindControl("lstX");
    DebugLabel.Text = "Insert: UserID: " + lstX.Value;
    }

    As you can see, this is pretty much identical to the update method,
    but it does not work at all. As far as I can tell, it does not even
    run, because I tried setting a breakpoint on the code.

    I know this issue has morphed a little from my original question
    (since I've been investivating it), but I need to be able to access
    the controls at runtime and manipulate their data programatically,
    which is the point of all this.

    Hope you can provide some insights!

    Thanks,
    Mark

    KyleK wrote:
    > Mark,
    >
    > What event are you working in? I usually cast the EventArg to a control then
    > do find control on it.
    >
    > --
    > Kyle Kelin
    > .NET Developer
    >
    >
    > "Mark Micallef" wrote:
    >
    > > I have a set of controls inside a template like this:
    > >
    > > <ajax:ReorderList ID="rolList" ... >
    > >
    > > ....
    > >
    > > <InsertItemTemplate>
    > > <asp:TextBox ID="txtText" ... />
    > > ...
    > > </InsertItemTemplate>
    > > </ajax:ReorderList>
    > >
    > > In my codebehind, I want to get at that textbox and do some stuff with
    > > it in code, but I just can't get a handle to it! I've tried using
    > > FindControl() but I always returns null. For example:
    > >
    > > TextBox txtText = rolList.FindControl("txtText");
    > >
    > > And variations thereof. Any ideas on this one? It's really starting to
    > > hurt...
    > >
    > > Thanks,
    > > Mark
    > >
    > >
     
    Mark Micallef, Apr 12, 2007
    #3
  4. Re: How do I programatically reference controls inside a control templ

    My previous reply to this has not shown up on usenet, so I'm
    repositing it. My apologies if you get this twice!

    Hey Kyle, sorry for not getting back sooner. I'm working off the
    OnInsertCommand event, which points to my method. Based on my testing
    and fiddling, it almost seems that the OnInsertCommand is not firing
    for reasons I can't determine.

    Here's a really stripped down version of my control definition with
    the event attribute settings:

    <ajax:ReorderList ...
    OnInsertCommand="onInsert"
    OnUpdateCommand="onUpdate">

    <EditItemTemplate>
    <div class="editArea">
    <asp:Button ID="cmdUpdate" runat="server"
    Text="Update" CommandName="update" />
    <asp:DropDownList ID="lstX" runat="server" ... />
    ...
    </div>
    </EditItemTemplate>

    <InsertItemTemplate>
    <div class="insertArea">
    <asp:DropDownList ID="lstX" runat="server" ... />
    <asp:Button ID="cmdAdd" runat="server" Text="Add"
    CommandName="insert" />
    </div>
    </InsertItemTemplate>

    ...

    </ajax:ReorderList>

    Now, the funny this is that the update handler works fine, but the
    insert handler does not. Here's what I mean. I have the following
    little debug handler for the update command which writes the value
    from one of the controls inside the reorderlist template to a label on
    the form:

    protected void onUpdate(object sender, ReorderListCommandEventArgs e)
    {
    DropDownList lstX = (DropDownList)e.Item.FindControl("lstX");
    DebugLabel.Text = "Update: xID: " + lstX.SelectedValue;
    }

    When I click add, the DebugLabel get's the value from lstX, plus the
    database stuff at the back end happens perfectly. Now, here's my
    insert method:

    protected void onInsert(object sender, ReorderListCommandEventArgs e)
    {
    DropDownList lstX = (DropDownList)e.Item.FindControl("lstX");
    DebugLabel.Text = "Insert: UserID: " + lstX.Value;
    }

    As you can see, this is pretty much identical to the update method,
    but it does not work at all. As far as I can tell, it does not even
    run, because I tried setting a breakpoint on the code.

    I know this issue has morphed a little from my original question
    (since I've been investivating it), but I need to be able to access
    the controls at runtime and manipulate their data programatically,
    which is the point of all this.

    Hope you can provide some insights!

    Thanks,
    Mark

    On Apr 11, 6:14 am, KyleK <> wrote:
    > Mark,
    >
    > What event are you working in? I usually cast the EventArg to a control then
    > do find control on it.
    >
    > --
    > Kyle Kelin
    > .NET Developer
     
    Mark Micallef, Apr 12, 2007
    #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. Neil
    Replies:
    1
    Views:
    1,600
    =?Utf-8?B?U3JpZGhhcg==?=
    Nov 16, 2005
  2. Ned Balzer
    Replies:
    2
    Views:
    10,383
    Ned Balzer
    Jul 18, 2006
  3. Dave
    Replies:
    1
    Views:
    469
  4. Gary Graham

    Programatically created controls in an ascx user control misbehaving

    Gary Graham, Jun 2, 2004, in forum: ASP .Net Building Controls
    Replies:
    3
    Views:
    151
    John Saunders
    Jun 3, 2004
  5. Roman
    Replies:
    2
    Views:
    265
    Roman
    Jan 4, 2005
Loading...

Share This Page