469,097 Members | 1,280 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,097 developers. It's quick & easy.

find control inside a repeater

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");
}
}
Nov 18 '05 #1
4 34224
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.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Mon, 8 Nov 2004 03:04:01 -0800, "huzz"
<hu**@discussions.microsoft.com> wrote:
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");
}
}


Nov 18 '05 #2
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
"Scott Allen" wrote:
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.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Mon, 8 Nov 2004 03:04:01 -0800, "huzz"
<hu**@discussions.microsoft.com> wrote:
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");
}
}


Nov 18 '05 #3
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

--
Scott
http://www.OdeToCode.com/blogs/scott/
On Mon, 8 Nov 2004 06:13:02 -0800, "huzz"
<hu**@discussions.microsoft.com> wrote:
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
"Scott Allen" wrote:
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.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Mon, 8 Nov 2004 03:04:01 -0800, "huzz"
<hu**@discussions.microsoft.com> wrote:
>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");
> }
> }



Nov 18 '05 #4
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.

"huzz" <hu**@discussions.microsoft.com> wrote in message news:<81**********************************@microso ft.com>...
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");
}
}

Nov 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Georges Heinesch | last post: by
2 posts views Thread by Stephen Miller | last post: by
3 posts views Thread by P.L. | last post: by
reply views Thread by John Crowley | last post: by
5 posts views Thread by Matt Jensen | last post: by
1 post views Thread by Salim | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.