views:

4423

answers:

4

hi,

Can someone tell me how I can get this to work. I want to distinguish dropdown controls inside a repeater control. I understand now about the lifecyle and how the buffer is already writen, but what are my alternatives? Here is what happens

Code File

    Dim repeatTimes((TotalAdInsured - 1)) As Integer

    myRepeater.DataSource = repeatTimes
    myRepeater.DataBind()

Aspfile

<asp:Repeater ID="myRepeater" runat="server">
    <ItemTemplate>
         <asp:DropDownList ID="AdTitle<%# Container.ItemIndex %>" runat="server">
             <asp:ListItem Selected="True" Value="" Text=""/>
             <asp:ListItem Selected="False" Value="Miss" Text="Miss"/>
             <asp:ListItem Selected="False" Value="Ms" Text="Ms"/>
             <asp:ListItem Selected="False" Value="Mrs" Text="Mrs"/>
             <asp:ListItem Selected="False" Value="Mr" Text="Mr"/>
             <asp:ListItem Selected="False" Value="Other" Text="Other"/>
         </asp:DropDownList>
    </ItemTemplate>
</asp:Repeater>

Returns this error

Parser Error Message: 'AdTitle<%# Container.ItemIndex %>' is not a valid identifier.

A: 

You could add an event handler to Repeater.ItemDataBound and create the control within the handler using the reference to the item container.

BC
+1  A: 

Ok... in a nutshell you can't do what you're trying to do in the manner you are currently doing it.

The ID property of a control can only be set using the ID attribute in the tag and a simple value. Example: <asp:Button runat="server" id="Button1" />

I can see what your trying to do, but I don't really understand Why...

A repeater control will contain 1 item, per item in your datasource, so it's perfectly fine to just call your DropDownList ID="AdTitle" as that will be a different 'AdTitle' from the one in the next row.

To get them back server side, you would just iterate through the Items in your Repeater & FindControl("AdTitle") to get the specific DropDownList for that item.

If it's absolutely necessary to have the IDs incrementing, you'll need to do this programmatically (probably on the ItemDataBound event to create a DropDownList and add it to your ItemTemplate.

Eoin Campbell
+2  A: 

You cannot create a Unique ID inside a repeater through the markup like that. You can however retrieve the dropdown in the codebehind by using FindControl on the Repeater.

Dim adTitle As DropDownList = MyRepeater.FindControl("AdTitle")
If (Not adTitle Is Nothing) Then
  ''Do something here
End If
ichiban
+4  A: 

I want to distinguish dropdown controls inside a repeater control.

You don't need to. Here's some sample code that might help you out:

Markup:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label runat="server" ID="lblMsg" Text="Click a button" />
        <hr />
        <asp:Repeater runat="server" ID="rptAwesome" onitemcommand="rptAwesome_ItemCommand"
            >
            <ItemTemplate>
                <asp:Button runat="server" ID="btnAwesome"
                    Text='<%# "Button #" + Container.ItemIndex %>'
                    CommandArgument='<%# Container.DataItem %>'/><br />
            </ItemTemplate>
        </asp:Repeater>
    </div>
    </form>
</body>
</html>

Codebehind:

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            var x = new int[] { 1, 2, 3, 4, 5 };
            rptAwesome.DataSource = x;
            rptAwesome.DataBind();
        }
    }
    protected void rptAwesome_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        Button btnAwesome = (Button)e.CommandSource;
        lblMsg.Text = string.Format("btnAwesome.ID = {0}, e.CommandArgument = {1}", btnAwesome.ID, e.CommandArgument);
    }
}
Juliet