C# 委托事件处理程序 SelectedIndexChanged

发布于 2024-10-16 13:50:54 字数 524 浏览 3 评论 0原文

我想将另一个值传递给 LeCmb_Statut_SelectedIndexChanged 方法,我该如何继续?

我有代码:

SqlDataReader reader = comm.ExecuteReader(); 
while (reader.Read())
{
   ...
   DropDownList LeCmb_Statut = new DropDownList();
   string tempLabel = reader["LIBELLE"].ToString();
   string templabelVeh = reader["LIBVEH"].ToString()
}

如何将这些临时变量传递到 LeCmb_Statut_SelectedIndexChanged 中?

LeCmb_Statut.SelectedIndexChanged += new EventHandler(LeCmb_Statut_SelectedIndexChanged);

I want to pass another value to the method LeCmb_Statut_SelectedIndexChanged, how can I proceed?

I have the code:

SqlDataReader reader = comm.ExecuteReader(); 
while (reader.Read())
{
   ...
   DropDownList LeCmb_Statut = new DropDownList();
   string tempLabel = reader["LIBELLE"].ToString();
   string templabelVeh = reader["LIBVEH"].ToString()
}

How can I pass these temp variables into LeCmb_Statut_SelectedIndexChanged?

LeCmb_Statut.SelectedIndexChanged += new EventHandler(LeCmb_Statut_SelectedIndexChanged);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

短叹 2024-10-23 13:50:54

您可以使用下拉列表的 Tag 属性。
首先,创建一个简单的 DTO:

public class LabelDTO
{
   public string Label {get;set;}
   public string LabelVeh {get;set;}
} 

然后:

while (reader.Read())
{
   ...
   DropDownList LeCmb_Statut = new DropDownList();
   LabelDTO lbl= new LabelDTO(){Label=reader["LIBELLE"].ToString(), LabelVeh=reader["LIBVEH"].ToString()};

    LeCmb_Statut.Tag=lbl;
}

然后在 LeCmb_Statut_SelectedIndexChanged 内部:

LeCmb_Statut_SelectedIndexChanged (object sender, System.EventArgs e)
{
    LabelDTO labelDTO= ((DropDownList)sender).Tag as LabelDTO ;
    if (labelDTO!=null)
     {
        ///here you can use the label and labelVeh properties.

     }
}

You could use the Tag property of dropdownlist.
first, create a simple DTO:

public class LabelDTO
{
   public string Label {get;set;}
   public string LabelVeh {get;set;}
} 

then:

while (reader.Read())
{
   ...
   DropDownList LeCmb_Statut = new DropDownList();
   LabelDTO lbl= new LabelDTO(){Label=reader["LIBELLE"].ToString(), LabelVeh=reader["LIBVEH"].ToString()};

    LeCmb_Statut.Tag=lbl;
}

then inside the LeCmb_Statut_SelectedIndexChanged:

LeCmb_Statut_SelectedIndexChanged (object sender, System.EventArgs e)
{
    LabelDTO labelDTO= ((DropDownList)sender).Tag as LabelDTO ;
    if (labelDTO!=null)
     {
        ///here you can use the label and labelVeh properties.

     }
}
花开柳相依 2024-10-23 13:50:54

您可以使用匿名方法来捕获它们:

string tempLabel = reader["LIBELLE"].ToString();
string templabelVeh = reader["LIBVEH"].ToString()
LeCmb_Statut.SelectedIndexChanged += delegate {
    // do something fun with   tempLabel  and templabelVeh 
};

对于小块或较长的代码,这可以是内联的 - 只需传递给方法:

string tempLabel = reader["LIBELLE"].ToString();
string templabelVeh = reader["LIBVEH"].ToString()
LeCmb_Statut.SelectedIndexChanged += delegate {
    DoSomethingFun(tempLabel,templabelVeh);
};

或者如果您需要发送者/参数:

string tempLabel = reader["LIBELLE"].ToString();
string templabelVeh = reader["LIBVEH"].ToString()
LeCmb_Statut.SelectedIndexChanged += delegate(object sender, EventArgs args) {
    DoSomethingFun(sender,args,tempLabel,templabelVeh);
};

或者在 C# 3.0 中:

string tempLabel = reader["LIBELLE"].ToString();
string templabelVeh = reader["LIBVEH"].ToString()
LeCmb_Statut.SelectedIndexChanged += (sender, args) =>
    DoSomethingFun(sender,args,tempLabel,templabelVeh);

You can use an anonymous method to capture them:

string tempLabel = reader["LIBELLE"].ToString();
string templabelVeh = reader["LIBVEH"].ToString()
LeCmb_Statut.SelectedIndexChanged += delegate {
    // do something fun with   tempLabel  and templabelVeh 
};

this could be inline for small blocks, or for longer code - just pass to a method:

string tempLabel = reader["LIBELLE"].ToString();
string templabelVeh = reader["LIBVEH"].ToString()
LeCmb_Statut.SelectedIndexChanged += delegate {
    DoSomethingFun(tempLabel,templabelVeh);
};

or if you need the sender/args:

string tempLabel = reader["LIBELLE"].ToString();
string templabelVeh = reader["LIBVEH"].ToString()
LeCmb_Statut.SelectedIndexChanged += delegate(object sender, EventArgs args) {
    DoSomethingFun(sender,args,tempLabel,templabelVeh);
};

or in C# 3.0:

string tempLabel = reader["LIBELLE"].ToString();
string templabelVeh = reader["LIBVEH"].ToString()
LeCmb_Statut.SelectedIndexChanged += (sender, args) =>
    DoSomethingFun(sender,args,tempLabel,templabelVeh);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文