在中继器控件ASP.NET中获取LatpdownList文本



我正在尝试在选定的索引更改下拉列表上获取所选文本。这是模拟。

   <pre> 
                    <ItemTemplate>
                        <tr>
                            <td><%# Eval("Teaenter code herem_Name")%></td>
                            <td class="text-center"><a href="http://localhost:4886/Payments/<%#Eval("Team_Payment")%>">View</a></td>
                            <td>       
                                <asp:DropDownList ID="drlApplicationStatus" runat="server" AutoPostBack="true" CssClass="form-control" CommandName="Update" CommandArgument='<%#Eval("Team_ID")%>'OnSelectedIndexchanged="Selected_IndexChanged" >
    <asp:ListItem CssClass="text-center" Text="--Select--" Value="0"></asp:ListItem>
                                    <asp:ListItem CssClass="text-center" Text="Active" Value="1"></asp:ListItem>                              
                                    <asp:ListItem CssClass="text-center" Text="Pending" Value="2 "></asp:ListItem>
                                </asp:DropDownList>

                            </td>
                        </tr>
                    </ItemTemplate>
</pre>

背后的代码
protected void TeamList_ItemCommand(object source, RepeaterCommandEventArgs e)
{
    DropDownList drlApplicationStatus = (DropDownList)source;
    if (drlApplicationStatus.SelectedIndex == 1 && e.CommandArgument !=null)
    {
        Teams.UpdateTeamStatus(int.Parse(e.CommandArgument.ToString()), "Active");
    }
    else if(drlApplicationStatus.SelectedIndex == 2 && e.CommandArgument != null)
    {
        Teams.UpdateTeamStatus(int.Parse(e.CommandArgument.ToString()), "Pending");
    }

}

我不知道为什么下拉列表selectedIndexchanged不会触发 ItemCommand事件。但是您可以这样做:

    protected void repeater_ItemCreated(object sender, RepeaterItemEventArgs e)
    {
        var drlApplicationStatus = (DropDownList)e.Item.FindControl("drlApplicationStatus");
        drlApplicationStatus.SelectedIndexChanged += drlApplicationStatus_SelectedIndexChanged;
    }
    private void drlApplicationStatus_SelectedIndexChanged(object sender, EventArgs e)
    {
        var drlApplicationStatus = (DropDownList)sender;
        Response.Write(drlApplicationStatus.SelectedValue);
        Response.Write(drlApplicationStatus.SelectedItem.Text);
    }

最新更新