如何绑定中继器内的下拉列表?

发布于 2024-12-03 21:48:22 字数 281 浏览 7 评论 0原文

我想绑定中继器内的下拉列表。我的代码是

 <asp:Repeater ID="rep_UnAssignComps" runat="server">
    <ItemTemplate><asp:DropDownList ID="drp_CompPropAddress" runat="server">
            </asp:DropDownList></itemTemplate></asp:Repeater>

I want to bind dropdownlist which is inside a repeater.my code is

 <asp:Repeater ID="rep_UnAssignComps" runat="server">
    <ItemTemplate><asp:DropDownList ID="drp_CompPropAddress" runat="server">
            </asp:DropDownList></itemTemplate></asp:Repeater>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

单身情人 2024-12-10 21:48:22

在 Repeater 的 ItemDatabound 事件中使用以下内容:

if (e.Item.ItemType == ListItemType.Item || 
         e.Item.ItemType == ListItemType.AlternatingItem)
{

    ((DropDownList)e.Item.FindControl("drp_CompPropAddress")).DataSource =(DataRowView) e.Item.DataItem;//Or any other datasource.
    ((DropDownList)e.Item.FindControl("drp_CompPropAddress")).DataBind();

}

On your Repeater's ItemDatabound event use the following:

if (e.Item.ItemType == ListItemType.Item || 
         e.Item.ItemType == ListItemType.AlternatingItem)
{

    ((DropDownList)e.Item.FindControl("drp_CompPropAddress")).DataSource =(DataRowView) e.Item.DataItem;//Or any other datasource.
    ((DropDownList)e.Item.FindControl("drp_CompPropAddress")).DataBind();

}
阳光下的泡沫是彩色的 2024-12-10 21:48:22

使用 Repeater 的 ItemDataBound 事件,如下所示:

protected void rep_UnAssignComps_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    DropDownList selectList = e.Item.FindControl("drp_CompPropAddress") as DropDownList;
    if (selectList != null)
    {
        selectList.DataSource = SomeDataSource(); //your datasource
        selectList.DataBind();

        //selectList.DataTextField = "SomeColumn";
        //selectList.DataValueField = "SomeID";
    }
}

还记得在标记或 ItemDataBound 事件中设置 DataTextField 和 DataValueField 属性。

Use the ItemDataBound event of the Repeater, like this:

protected void rep_UnAssignComps_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    DropDownList selectList = e.Item.FindControl("drp_CompPropAddress") as DropDownList;
    if (selectList != null)
    {
        selectList.DataSource = SomeDataSource(); //your datasource
        selectList.DataBind();

        //selectList.DataTextField = "SomeColumn";
        //selectList.DataValueField = "SomeID";
    }
}

Also remember to set the DataTextField and DataValueField properties, either in the markup or in the ItemDataBound event.

香草可樂 2024-12-10 21:48:22

我刚刚找到了一种以声明方式执行此操作的方法:

<asp:Repeater ID="rep_UnAssignComps" runat="server">
<ItemTemplate><asp:DropDownList ID="drp_CompPropAddress" runat="server" DataSource='<%# MyList %>' SelectedValue='<%# Eval("Address") %>'>
        </asp:DropDownList></itemTemplate></asp:Repeater>

Eval() 中使用的“地址”是绑定到使用后面的代码完成的转发器的类的成员。在我的例子中,用作 MyList 的数据源是一个列表,其中包含将显示在下拉列表中的可能值。

I just found a way to do this declaratively:

<asp:Repeater ID="rep_UnAssignComps" runat="server">
<ItemTemplate><asp:DropDownList ID="drp_CompPropAddress" runat="server" DataSource='<%# MyList %>' SelectedValue='<%# Eval("Address") %>'>
        </asp:DropDownList></itemTemplate></asp:Repeater>

The "Address" used in the Eval() is a member of the class that is bound to the repeater done using the code behind. The DataSource used as MyList is a List in my case, that contains the possible values that will show up in the dropdown.

浪漫之都 2024-12-10 21:48:22

使用 Repeater 的 OnItemCreated 事件并绑定其中的下拉菜单。

HTML

<asp:Repeater runat="server" ID="repRoute" **OnItemCreated**="PopulateCountries">
            <ItemTemplate>
                <asp:DropDownList runat="server" ID="cboCountries" DataTextField="Name" DataValueField="CountryCode"/> 
            </ItemTemplate>
        </asp:Repeater>

隐藏代码:

protected void PopulateLocations(object sender, RepeaterItemEventArgs e)
    {
        var customerInfo = (CustomerInfo)e.Item.DataItem;
        if (customerInfo == null) return;

        var cboCountries = (DropDownList)e.Item.FindControl("cboCountries");
        cboCountries.DataSource = GetAll();
        cboCountries.DataBind();
    }

Use Repeater's OnItemCreated event and bind the dropdowns inside it.

HTML

<asp:Repeater runat="server" ID="repRoute" **OnItemCreated**="PopulateCountries">
            <ItemTemplate>
                <asp:DropDownList runat="server" ID="cboCountries" DataTextField="Name" DataValueField="CountryCode"/> 
            </ItemTemplate>
        </asp:Repeater>

Codebehind:

protected void PopulateLocations(object sender, RepeaterItemEventArgs e)
    {
        var customerInfo = (CustomerInfo)e.Item.DataItem;
        if (customerInfo == null) return;

        var cboCountries = (DropDownList)e.Item.FindControl("cboCountries");
        cboCountries.DataSource = GetAll();
        cboCountries.DataBind();
    }
眼前雾蒙蒙 2024-12-10 21:48:22

使用此代码

<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>

并在代码隐藏中:

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

use this

<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>

and in 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;
}
}

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文