如何查找触发SelectedIndexChanged事件的下拉列表的控件ID

时间:2015-03-12 17:23:07

标签: c# asp.net

我有几个下拉列表都会触发相同的OnSelectedIndexChanged事件。一旦OnSelectedIndexChanged事件触发,我想知道负责触发事件的下拉列表的id。

有没有办法将此信息拉入字符串?

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="updateView">
   <asp:ListItem></asp:ListItem>
   <asp:ListItem>Not Started</asp:ListItem>
   <asp:ListItem>Submitted</asp:ListItem>
   <asp:ListItem>WIP</asp:ListItem>
   <asp:ListItem>Not Applicable</asp:ListItem>
   <asp:ListItem>Completed</asp:ListItem>
   <asp:ListItem>Rejected</asp:ListItem>
</asp:DropDownList>

<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" OnSelectedIndexChanged="updateView">
   <asp:ListItem></asp:ListItem>
   <asp:ListItem>Not Started</asp:ListItem>
   <asp:ListItem>Submitted</asp:ListItem>
   <asp:ListItem>WIP</asp:ListItem>
   <asp:ListItem>Not Applicable</asp:ListItem>
   <asp:ListItem>Completed</asp:ListItem>
   <asp:ListItem>Rejected</asp:ListItem>
</asp:DropDownList>

protected void updateView(object sender, EventArgs e)
{
    //string dd_id = id of dropdown that triggered this event;
}

1 个答案:

答案 0 :(得分:2)

sender是实际触发事件的控件。你可以试试这个:

protected void updateView(object sender, EventArgs e)
{
    DropDownList ddl = sender as DropDownList;
    string dd_id = ddl.ID;
}