How do I programatically reference controls inside a control template?

M

Mark Micallef

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
 
K

KyleK

Mark,

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

Mark Micallef

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
 
M

Mark Micallef

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top