vb.net 你如何在c#的下拉列表中使用if条件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26381378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:21:21  来源:igfitidea点击:

How do you use an if condition in a dropdownlist in c#

c#asp.net.netvb.net

提问by user3196662

I have the following ASP.net code that I want to port to C#.

我有以下 ASP.net 代码要移植到 C#。

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    If DropDownList1.SelectedItem.Text = "1" Then
        Response.Redirect("default.aspx")
    ElseIf DropDownList1.SelectedItem.Text = "2" Then
        Response.Redirect("default.aspx")
    End If
End Sub

How should I go about this?

我该怎么办?

回答by krystan honour

The code translated to c# is

翻译成c#的代码是

protected void Button1_Click (Object sender, EventArgs e)
{
    if (DropDownList1.SelectedItem.Text == "1")
    {
        Response.Redirect("default.aspx");
    }
    else if (DropDownList1.SelectedItem.Text == "2")
    {
        Response.Redirect("default.aspx");
    }
}

Note there is no 'Handles' statement in c# to attach the event to the handler to use the below code in your Initialisation stages.

请注意,c# 中没有“句柄”语句将事件附加到处理程序以在初始化阶段使用以下代码。

Button1.Click += Button1_Click;

回答by Santosh Panda

So here goes your C# equivalent code:

所以这里是你的 C# 等效代码:

protected void Button1_Click(object sender, System.EventArgs e)
{
    if (DropDownList1.SelectedItem.Text == "1") {
        Response.Redirect("default.aspx");
    } else if (DropDownList1.SelectedItem.Text == "2") {
        Response.Redirect("default.aspx");
    }
}

Sometimes you better use some online converter which are readily available with zero cost.

有时,您最好使用一些零成本随时可用的在线转换器。

  1. http://converter.telerik.com/
  2. http://www.developerfusion.com/tools/convert/vb-to-csharp/
  1. http://converter.telerik.com/
  2. http://www.developerfusion.com/tools/convert/vb-to-csharp/

回答by asdf_enel_hak

When use http://www.developerfusion.com/tools/convert/vb-to-csharp/conversion link

当使用http://www.developerfusion.com/tools/convert/vb-to-csharp/转换链接时

the result is:

结果是:

protected void Button1_Click(object sender, System.EventArgs e)
{
    if (DropDownList1.SelectedItem.Text == "1") {
        Response.Redirect("default.aspx");
    } else if (DropDownList1.SelectedItem.Text == "2") {
        Response.Redirect("default.aspx");
    }
}