如何在下拉选择的索引更改事件中找到转发器内的下拉选择值?

时间:2015-01-29 15:58:59

标签: c# asp.net

 <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
            <ItemTemplate>
                <tr>
                    <td><%#subtypes.FindByPk(Convert.ToInt32(Eval("SubitemID"))).title%></td>
                    <td><%#Eval("quantity")%></td>
                    <td><%#ThanaRecord.FindByPk(Convert.ToInt32(Eval("Thanaid"))).title%></td>
                    <td><%#Eval("created_at")%></td>
                    <td> <% if (Employee.GetCurrentEmployee().role == "Admin") { %>
                        <a href="AddDemand.aspx?type=update&id=<%#Eval("id")%>">EDIT</a>

                            <a href="AddDemand.aspx?type=delete&id=<%#Eval("id")%>">DELETE</a>
                        <% } %>
                    <%if (Employee.GetCurrentEmployee().role == "SuperVisor")
                       { %>
                      <asp:DropDownList ID="DropDownList1" runat="server" Width="120px"  AutoPostBack="true"  OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" CssClass="form-control">
                 <asp:ListItem Text="Status" Value="0">Status</asp:ListItem>
                          <asp:ListItem Text="Accept" Value="1">Accept</asp:ListItem>
                <asp:ListItem Text="Reject" Value="2">Reject</asp:ListItem>

            </asp:DropDownList>

                        <%--<asp:textbox runat="server" id="textTest"></asp:textbox>--%>
                    </td>
                    <%} %>
                </tr>
            </ItemTemplate>
        </asp:Repeater>

代码:

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    //DropDownList DropDownList1 = (DropDownList)sender;
    //string SelectedValue = DropDownList1.SelectedValue;
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        DropDownList ddldrop = (DropDownList)e.Item.FindControl("DropDownList1");
        int  value =Convert.ToInt32( ddldrop.SelectedValue);
        Supervisor sup = new Supervisor();
    if (value ==1 ) {
            sup.Status = "Accept";
            sup.Save();
        }
    }
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    //Supervisor sup = new Supervisor();
    //sup.Status = "Accept";
    //sup.Save();
}

1 个答案:

答案 0 :(得分:1)

您只需将事件处理程序中的sender参数转换为DropDownList。它始终是事件的源头。所以不需要使用FindControl

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
   DropDownList ddl = (DropDownList) sender;
   string selectedValue = ddl.SelectedValue;
   // ...
}