如何创建foreach语句以确定所选索引值是否为“0”

时间:2015-12-12 16:34:18

标签: c# asp.net webforms

我有一个下拉列表,当用户从下拉列表中选择视频时,该列表会加载视频。我在我的下拉列表的索引0中插入了一个空值,因此没有预先选择的视频。这很有效,直到用户重新选择第一个项目。该应用尝试加载此视频不存在。我想创建一个foreach循环,如果所选索引为0,我想突破并且不想尝试加载视频。

我在中断和继续行上收到此错误“没有封闭循环以继续或中断”。

感谢您提前获得的帮助。

  protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
  {
    string YourFilePathWithFileName = DropDownList1.SelectedValue;

        foreach (int DropDownList1_SelectedIndexChanged in DropDownList1.SelectedIndex)
        if (DropDownList1.SelectedIndex.Equals(0))

        {
          break;

          media_video.Attributes.Add("src", "/Uploads/" + YourFilePathWithFileName + ".mp4");
          media_video.Attributes.Add("type", "video/mp4");
          media_video.Attributes.Add("autoplay", "autoplay");
          media_video.Attributes.Add("src", "/Uploads/" + YourFilePathWithFileName + ".ogc");
          media_video.Attributes.Add("type", "video/ogv");
          media_video.Attributes.Add("autoplay", "autoplay");
          media_video.Attributes.Add("src", "/Uploads/" + YourFilePathWithFileName + ".webm");
          media_video.Attributes.Add("type", "video/webm");
          media_video.Attributes.Add("autoplay", "autoplay");
        }
      }

2 个答案:

答案 0 :(得分:0)

你的代码不在任何循环中,所以它抱怨它不能从代码块中断继续(cz它没有找到任何代码) 。据我所知,你只是不想加载视频,或者更喜欢将属性添加到media_video(技术上),你只需检查索引并跳过这个过程: -

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
   if (DropDownList1.SelectedIndex != 0)
   {
      string YourFilePathWithFileName = DropDownList1.SelectedValue;
      media_video.Attributes.Add("src", "/Uploads/" + YourFilePathWithFileName + ".mp4");
      media_video.Attributes.Add("type", "video/mp4");
      ..other attributes
   }
   else
   {
      media_video.Attributes.Remove("src");
   }
} 

答案 1 :(得分:0)

您不必为您的问题使用任何循环。如果我理解正确,如果用户将选择索引为0的视频,您想要停止加载视频或显示一些默认视频。您的代码应该是:

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedIndex == 0)
    {
      media_video.Attributes.Add("src", "/Uploads/" + DefaultVideo + ".mp4");
      media_video.Attributes.Add("type", "video/mp4");
      media_video.Attributes.Add("autoplay", "autoplay");
      media_video.Attributes.Add("src", "/Uploads/" + DefaultVideo + ".ogc");
      media_video.Attributes.Add("type", "video/ogv");
      media_video.Attributes.Add("autoplay", "autoplay");
      media_video.Attributes.Add("src", "/Uploads/" + DefaultVideo + ".webm");
      media_video.Attributes.Add("type", "video/webm");
      media_video.Attributes.Add("autoplay", "autoplay");
    }
  }

如果您不需要执行任何操作,只需关闭视频控件(它将禁用您的视频控件):

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedIndex == 0)
    {
       media_video.Visible = false;
    }
  }

希望有所帮助