C# 重复数据帮助
我刚刚在视频库中进行了第一次尝试,我想知道如何显示多个视频,并提供用户上传更多视频!
目前的工作原理是,他们将设置的信息输入数据库,然后我将其拉到页面上的文字内。
这是背后的代码
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>";
}
现在,如果用户一次只想显示一个视频,那很好,但是我该如何显示多个视频???
谢谢!
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最简单的解决方案是在 LB_Video 下添加多个 iFrame。不是最优雅的解决方案,但它会工作正常并且尽可能简单...
请注意,您的代码也存在一个问题 - SingleOrDefault() 可以返回 null,因此如果 VG 为 null,下一行 (.Source) 将失败...您需要注意可以为空的东西!
所以...
请注意,如果您在声明中使用“var”,这会将您的代码与特定类型解耦,这意味着代码可以更自由地移动。接下来,如果您有很多视频,您可能最好使用 StringBuilder(来自 System.Text 命名空间)。为此,请按如下方式调整上述内容:
在字符串处理代码中渲染 Html 从来都不是最优雅的方法,但它会为您可靠地工作,并且也很容易(从 HTML)看到生成的内容,因此很容易看出是否出了什么问题......
您可能尝试的下一个重构可能是
......并让 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...
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 ...
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:
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
...and let that VideoControl class wrap the specifics of how the HTML is generated.
Good luck and happy coding!
如果您在末尾更改 LINQ 查询以获取多个元素,并且您只想添加更多
iframe
,您可能可以执行以下操作:您可能还应该使用
StringBuilder
code> 连接字符串,最后将其放入 LB_Video.Text 中,但这至少应该向您展示这个概念。If you change the LINQ query at the end to fetch several elements, and you simply want more
iframe
s added, you can probably do something like this:You should probably also use a
StringBuilder
to concatenate the strings, and put it intoLB_Video.Text
in the end, but this should show you the concept at least.