将下拉列表作为参数传递

Passing in dropdown as a parameter

我正在尝试将下拉 ID 作为参数传递,以便我可以对其进行另一个操作 class,然后将其传递回主要 class。

说我在另一个 class 中有这个方法,我想将下拉列表传递到:

 public MyLevel GetLevel(string myValue, myDropDown)
 {
      return new MyLevel
      {

      }
 }
 public class MyLevel
 {

 }

我的下拉菜单将像这样出现在我的标记中

<asp:DropDownList AutoPostBack="true" ID="myDropDown" runat="server" CssClass="searchfield form-control" EnableViewState="true" OnSelectedIndexChanged="myDropDown_Change">
</asp:DropDownList>

所以我的问题是,我怎样才能将它传递给我的其他 classes 来操纵它,然后在 C#

中正确地传回它

首先您需要在您的页面上找到该控件并将其分配给一个变量。然后可以将其作为变量传入并进行操作。

var dropDown = Page.FindControl("myDropDown") as DropDownList;
MyLevel myLevel = GetLevel(myValue, dropDown);