Как связать DropDownlist, который находится внутри повторителя?

Я хочу связать DropDownlist, который находится внутри ретранслятора. Мой код

 <asp:Repeater ID="rep_UnAssignComps" runat="server">
    <ItemTemplate><asp:DropDownList ID="drp_CompPropAddress" runat="server">
            </asp:DropDownList></itemTemplate></asp:Repeater>
12
задан Ram Singh 12 September 2011 в 13:25
поделиться

1 ответ

используйте это

<asp:Repeater ID="rptCustomers" runat="server" OnItemDataBound="OnItemDataBound">
<HeaderTemplate>
    <table cellspacing="0" rules="all" border="1">
        <tr>
            <th scope="col" style="width: 80px">
                Customer Id
            </th>
            <th scope="col" style="width: 120px">
                Name
            </th>
            <th scope="col" style="width: 100px">
                Country
            </th>
        </tr>
</HeaderTemplate>
<ItemTemplate>
    <tr>
        <td>
            <asp:Label ID="lblCustomerId" runat="server" Text='<%# Eval("CustomerId")    
              %>' />
        </td>
        <td>
            <asp:Label ID="lblName" runat="server" Text='<%# Eval("ContactName") %>' 
             />
        </td>
        <td>
            <asp:DropDownList ID="ddlCountries" runat="server">
            </asp:DropDownList>
        </td>
    </tr>
</ItemTemplate>
<FooterTemplate>
    </table>
</FooterTemplate>
</asp:Repeater>

и в codebehind:

protected void OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == 
ListItemType.AlternatingItem)
{
    //Find the DropDownList in the Repeater Item.
    DropDownList ddlCountries = (e.Item.FindControl("ddlCountries") as DropDownList);
    ddlCountries.DataSource = this.GetData("SELECT DISTINCT Country FROM Customers");
    ddlCountries.DataTextField = "Country";
    ddlCountries.DataValueField = "Country";
    ddlCountries.DataBind();

    //Add Default Item in the DropDownList.
   ddlCountries.Items.Insert(0, new ListItem("Please select"));

    //Select the Country of Customer in DropDownList.
    string country = (e.Item.DataItem as DataRowView)["Country"].ToString();
    ddlCountries.Items.FindByValue(country).Selected = true;
}
}

от https://www.aspsnippets.com/Articles/Populate-Bind-DropDownList-in-ItemTemplate-of-Repeater-Control-in-ASPNet.aspx

0
ответ дан 2 December 2019 в 06:22
поделиться
Другие вопросы по тегам:

Похожие вопросы: