在Repeater Control ASP.NET中获取DropdownList文本

时间:2017-06-06 12:46:24

标签: c# asp.net

我试图在DropDownList的选定索引更改中获取所选文本。这是模拟了。

   <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");
    }


}

1 个答案:

答案 0 :(得分:0)

我不知道为什么DropDownList 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);

    }