Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C重复数据帮助_C#_.net_Video_Repeater - Fatal编程技术网

C# C重复数据帮助

C# C重复数据帮助,c#,.net,video,repeater,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() == "Yo

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

目前的工作原理是,他们将集合信息输入到数据库中,然后我将其拉到页面上,在文本中

这是背后的代码

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>";
}
现在,如果用户一次只想显示一个视频,这很好,但是我该如何显示多个视频呢


谢谢

如果您在最后更改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中,但这至少应该向您展示这个概念。

最简单的解决方案是在LB_Video下面添加多个iFrame。不是最优雅的解决方案,但它可以正常工作,并且尽可能简单

选择if/else语句 使用Extract方法重构,并创建一个可以多次调用的新方法“CreateVideoHtml” 将“Text”=更改为“Text+=”,以便可以添加多个批次的HTML 添加Where代替SingleOrDefault Add'ToList'返回所有项目或'take'最多检索n个项目 从循环中调用新的CreateVideoHtml方法 请注意,您的代码也有一个问题-SingleOrDefault可以返回null,因此下一行。如果VG为null,则Source将失效。。。你需要注意可以为空的东西

所以

        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>";
            }
        }
…并让VideoControl类包装HTML生成的细节


祝你好运和快乐

您有SingleOrDefault,这意味着只有一条记录从安全角度来看,您的代码非常容易受到攻击。确保在将用户输入直接放入生成的html中时,很好地检查用户输入。
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>");
        }
    }
LB_Video.Controls.Add(new VideoControl(video));