C#重复数据帮助

时间:2011-06-21 11:10:58

标签: c# .net video repeater

我刚刚在视频库中创建了第一次尝试,我想知道如何显示多个视频,让用户上传更多!!!

它目前的工作原理是它们将设置信息输入数据库,然后我将其拉到页面内,在文字内。

以下是

背后的代码
DT_Control_VideoGallery VG = 
  db.DT_Control_VideoGalleries.SingleOrDefault(x => x.PageControlID == int.Parse(HF_CPID.Value));

if (VG.Source.ToString() == "YouTube")
{
    LB_Video.Text = "<iframe width=" + VG.Width + "height=" + VG.Height +
                    "src=\"http://www.youtube.com/embed/" + VG.ReferenceKey.Trim() +
                    "frameborder=\"0\" allowfullscreen></iframe>";
}
else
{
    LB_Video.Text = "<iframe width=\"" + VG.Width + "\"height=\"" + VG.Height +
                    "\"frameborder=0\" src=\"http://player.vimeo.com/video/" +
                    VG.ReferenceKey.Trim() + "?title=0&amp;byline=0\"></iframe>";
}

现在可以说,如果用户一次只想显示一个视频,但我如何才能显示多个?

谢谢!

2 个答案:

答案 0 :(得分:1)

如果您最后更改LINQ查询以获取多个元素,并且您只想添加更多iframe,则可以执行以下操作:

var VGs = db.DT_Control_VideoGalleries.Where(someSelector);
foreach( var VG in VGs )
{
  if (VG.Source.ToString() == "YouTube")
  {
    LB_Video.Text += "<iframe width=" + VG.Width + "height=" + VG.Height + "src=\"http://www.youtube.com/embed/" + VG.ReferenceKey.Trim() + "frameborder=\"0\" allowfullscreen></iframe>";
  }
  else
  {
    LB_Video.Text += "<iframe width=\"" +  VG.Width + "\"height=\"" + VG.Height + "\"frameborder=0\" src=\"http://player.vimeo.com/video/" + VG.ReferenceKey.Trim() + "?title=0&amp;byline=0\"></iframe>";
  }
}

您可能还应该使用StringBuilder来连接字符串,最后将它放入LB_Video.Text,但这应该至少向您展示这个概念。

答案 1 :(得分:1)

最简单的解决方案是在LB_Video下添加多个iFrame。不是最优雅的解决方案,但它可以正常工作,就像你可以做到的那样简单......

  • 选择if / else语句
  • 使用Extract方法重构并创建一个可以多次调用的新方法“CreateVideoHtml”
  • 将'Text'=更改为'Text + =',以便添加多个HTML
  • 添加Where()代替SingleOrDefault()
  • 添加'ToList()'(返回所有项目)或'Take(n)'(最多检索n)
  • 从循环中调用新的CreateVideoHtml方法

请注意,您的代码也存在问题 - SingleOrDefault()可以返回null,因此如果VG为null,下一行(.Source)将会失效...您需要注意可能为null的内容! / p>

所以......

        public void YourMethod()
        {
            var sb = new StringBuilder();
            var videoList = db.DT_Control_VideoGalleries.Where(x => x.PageControlID == int.Parse(HF_CPID.Value)).Take(3);
            foreach(var video in videoList)
            {
                CreateVideoHtml(video);
            }

            foreach(var video in videoList) 
            {
                CreateVideoHtml(video);
            }

            // Nothing returned from your query - CreateVideoHtml was never called!
            if (LB_Video.Text == String.Empty)
            {
                LB_Video.Text = "No video!";
            }
        }

        private void CreateVideoHtml(DT_Control_VideoGallery video)
        {
            if (video.Source.ToString() == "YouTube")
            {
                LB_Video.Text += "<iframe width=" + video.Width + "height=" + video.Height + "src=\"http://www.youtube.com/embed/" + video.ReferenceKey.Trim() + "frameborder=\"0\" allowfullscreen></iframe>";
            }
            else
            {

                LB_Video.Text += "<iframe width=\"" + video.Width + "\"height=\"" + video.Height + "\"frameborder=0\" src=\"http://player.vimeo.com/video/" + video.ReferenceKey.Trim() + "?title=0&amp;byline=0\"></iframe>";
            }
        }

请注意,如果在声明中使用'var',则会将代码与特定类型分离,这意味着代码可以更自由地移动。接下来,如果您有很多视频,最好使用StringBuilder(来自System.Text命名空间)。为此,请按如下方式调整上述内容:

public void RenderVideo()
        {
            var sb = new StringBuilder();
            var videoList = db.DT_Control_VideoGalleries.Where(x => x.PageControlID == int.Parse(HF_CPID.Value)).Take(3);
            foreach(var video in videoList)
            {
                // sb is passed in by reference, so we can see any changes here
                CreateVideoHtml(sb, video);
            }

        // Nothing returned from your query - CreateVideoHtml was never called!
        if (sb.Length == 0)
        {
            LB_Video.Text = "No video!";
        }
        else
        {
             LB_Video.Text = sb.ToString();
        }
    }

    // this is static - all dependencies are passed in by reference
    // the calling code can see the modifications to sb
    // all this method does is create Html so you could unit test it
    private static void CreateVideoHtml(StringBuilder sb, DT_Control_VideoGallery video)
    {
        if (video.Source.ToString() == "YouTube")
        {
            sb.Append("<iframe width=" + video.Width + "height=" + video.Height + "src=\"http://www.youtube.com/embed/" + video.ReferenceKey.Trim() + "frameborder=\"0\" allowfullscreen></iframe>");
        }
        else
        {

            sb.Append("<iframe width=\"" + video.Width + "\"height=\"" + video.Height + "\"frameborder=0\" src=\"http://player.vimeo.com/video/" + video.ReferenceKey.Trim() + "?title=0&amp;byline=0\"></iframe>");
        }
    }

在字符串处理代码中渲染Html绝不是最优雅的方法,但它可以为您提供可靠的工作,而且(从HTML中)生成的内容也很容易看到,因此很容易看出是否有任何内容出错......

您可能尝试的下一次重构可能是

LB_Video.Controls.Add(new VideoControl(video)); 

...让VideoControl类包含HTML生成方式的细节。

祝你好运,编码愉快!