C# 重复数据帮助

发布于 2024-11-16 08:11:07 字数 875 浏览 3 评论 0原文

我刚刚在视频库中进行了第一次尝试,我想知道如何显示多个视频,并提供用户上传更多视频!

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

这是背后的代码

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>";
}

现在,如果用户一次只想显示一个视频,那很好,但是我该如何显示多个视频???

谢谢!

I've just created my first attempt at a video gallery and I am wondering how I would go about displaying more than one video, providing the user uploads more!!!

How it currently works is that they enter set information into a database and I then pull this onto the page, inside a literal.

Here is the code behind

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&byline=0\"></iframe>";
}

Now this is fine say if the user has only one video they want to display at a time, but how would I go about displaying more than one???

Thanks!

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

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

发布评论

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

评论(2

明天过后 2024-11-23 08:11:07

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

  • 选择 if / else 语句
  • 使用 Extract Method 重构并创建一个可以多次调用的新方法“CreateVideoHtml”
  • 更改“Text” ' = 到 'Text +=',以便可以添加多批 HTML
  • 添加Where() 代替 SingleOrDefault()
  • 添加 'ToList()'(返回所有项目)或 'Take(n)'(在以下位置检索 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&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&byline=0\"></iframe>");
        }
    }

在字符串处理代码中渲染 Html 从来都不是最优雅的方法,但它会为您可靠地工作,并且也很容易(从 HTML)看到生成的内容,因此很容易看出是否出了什么问题......

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

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

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

祝你好运,编码愉快!

The simplest solution is to add multiple iFrames beneath LB_Video. Not the most elegant solution, but it will work OK and is as simple as you can make it...

  • Select the if / else statement
  • Refactor with Extract Method and create a new method 'CreateVideoHtml' that can be called multiple times
  • Change 'Text' = to 'Text +=' so multiple lots of HTML can be be added
  • Add Where() in place of SingleOrDefault()
  • Add 'ToList()' (returns all items) or 'Take(n)' (to retrieve n at most)
  • Call the new CreateVideoHtml method from a loop

Note there's also an issue with your code - SingleOrDefault() can return null, so the next line (.Source) will fall over if VG is null... you need to watch out for stuff that can be null!

So ...

        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&byline=0\"></iframe>";
            }
        }

Note that if you use 'var' in declarations, this decouples your code from specific Types, meaning code can be moved around more freely. Next, if you have a lot of videos you might be better to use a StringBuilder (from System.Text namespace). To do that, tweak the above as follows:

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&byline=0\"></iframe>");
        }
    }

Rendering Html in string handling code is never the most elegant way to go, but it will work reliably for you, and it's also easy to see (from the HTML) what has been produced, so it's easy to see if anything is going wrong....

The next refactoring you might try could be

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

...and let that VideoControl class wrap the specifics of how the HTML is generated.

Good luck and happy coding!

稚气少女 2024-11-23 08:11:07

如果您在末尾更改 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&byline=0\"></iframe>";
  }
}

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

If you change the LINQ query at the end to fetch several elements, and you simply want more iframes added, you can probably do something like this:

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&byline=0\"></iframe>";
  }
}

You should probably also use a StringBuilder to concatenate the strings, and put it into LB_Video.Text in the end, but this should show you the concept at least.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文