find control inside a repeater

G

Guest

I am trying to access a DropDownList control inside a repeater using
ItemCommand as shown below but for some reason i can't access the
DropDownList. When i step through the debug i get <undefine value> for the
DropDownList

What am i doing wrong?

<asp:Repeater ID="Repeater1" Runat="server" OnItemDataBound="create_ddl"
OnItemCommand="Repeater1_ItemCommand">
<HeaderTemplate>
<table>
<tr>
Edit Details
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:DropDownList ID="ddl" Runat="server"></asp:DropDownList>
</td>
</tr>

<tr>
<td>
<asp:TextBox ID="StaffComments" Runat="server"
TextMode="MultiLine" Text='<%# DataBinder.Eval(Container.DataItem,
"StaffPriority")%>' Width="400" Height="40"></asp:TextBox>
</td>
</tr>

</ItemTemplate>
<FooterTemplate>
<tr>
<td colspan="2"><asp:Button runat="server" Text="Save
Changes"></asp:Button></td>
</tr>
</table>
</FooterTemplate>
</asp:Repeater>

public void Repeater1_ItemCommand(object s, RepeaterCommandEventArgs e)
{
DropDownList ddl1 = (DropDownList)e.Item.FindControl("ddl");

if(ddl1 !=null)
{
Response.Write(ddl1.SelectedIndex.ToString());
}
else
{
Response.Write("ddl is null");
}
}
 
S

Scott Allen

In this case, the button is in the footer template, so I'm guessing
e.Item refers to the footer template instead of the item where the
drop down list lives. If you do not know which item the user was just
editing you might have to loop through all the items in the repeater
and get the updated DropDownList values.
 
G

Guest

Scott, Thank you so much.
I moved to button inside the ItemTemplate and its working now.. :)

Can you please explain whats the reason i couldn't find the control when i
had the button inside the FooterTemplate?

Is there any tutorials which explains repeater in great details?

many thanks again
 
S

Scott Allen

Inside the event handler the runtime gives a RepeaterCommandEventArgs
parameter (e). The e.Item property represents the Repeater Item where
the event occured (the footer item). When the code says
e.Item.FindControl(<controlname>), it is saying "Go to the
RepeaterItem where the event occured (the footer) and look for the
control with this name. Since the footer doesn't have that control the
call returns Nothing. Make any more sense?

I have a couple relavent articles here:

In Search of ASP.NET Controls
http://odetocode.com/Articles/116.aspx

DropDownList Controls In an ASP.Net DataGrid
(using a DataGrid, but the concepts are the same)
http://odetocode.com/Articles/231.aspx
 
G

Gos

You have the button in Footer template, not in the item template. For
the ItemCommand to work, you should have the button as part of your
Item template.
 

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

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top