How can I dynamically display a list of 10 thumbnail images that are stored in the file system? The thumbnail images are for videos. This is what I have so far:
default.aspx, where I have hardcoded the thumbnail links like this:
<a id="A1" href="~/PlayVideo.aspx?video=23" runat="server"> <img id="Img1" src="~/Uploads/Thumbs/Sample.jpg" runat="server" name="go" border="0" height="100" width="144" /> <a id="A2" href="~/PlayVideo.aspx?video=24" runat="server"> <img id="Img2" src="~/Uploads/Thumbs/Sample2.jpg" runat="server" name="go" border="0" height="100" width="144" />
clicking on these links loads playvideo.aspx, where I have the code for the player, and video number 23 or 24 (in this case) plays.
I have a mysql query string that sorts the videos (SELECT file FROM video ORDER BY date DESC LIMIT 10)
This is what I need to do: - Insert the query string into the codebehind (default.aspx.cs) so that the sorted thumbnails show on the page instead of the hardcode开发者_Go百科d ones. Is this what GridView is used for?
I hope that makes sense. I am using asp.net c#. Thanks in advance!
EDIT: The following is my code now:
protected void Page_Load(object sender, EventArgs e)
{
List<VideoDescriptor> vidList = new List<VideoDescriptor>();
MySqlConnection DBConn = new MySqlConnection(WebConfigurationManager.ConnectionStrings["LocalMySqlServer"].ConnectionString);
MySqlCommand DBCmd = new MySqlCommand();
DBConn.Open();
MySqlDataReader reader = DBCmd.ExecuteReader();
try
{
DBCmd = new MySqlCommand("SELECT VideoID, ImgFile FROM (SELECT VideoID, ImgFile FROM video ORDER BY Rank DESC) as myalias LIMIT 10", DBConn);
DBCmd.ExecuteReader();
String ImgFileName;
Int64 VidID;
while (reader.Read())
{
ImgFileName = reader["ImgFile"].ToString().Trim();
VidID = (Int64)reader["VideoID"];
vidList.Add(new VideoDescriptor() { FileName = ImgFileName, VideoID = VidID});
}
}
catch (Exception exp)
{
Response.Write(exp);
}
reader.Close();
DBCmd.Dispose();
DBConn.Close();
DBConn = null;
}
class VideoDescriptor
{
public string FileName { get; set; }
public Int64 VideoID { get; set; }
}
So I extracted the image file name and the video id, both of which I need for the thumbnail links I pasted above. How do I do this? I know how to just display this info in a gridview, but how do you include the info as part of a url?
Thanks again!
Probably your best bet would be to define a new class such as
Private Class VideoDescriptor
Dim date, thumbPath, videoPath as String
'etc
Then create a List or some other collection of these descriptors.
Dim lstVideos as New List(Of VideoDescriptor)
Create your VideoDescriptors in whatever way (i don't know if youre pulling from DB or what) and add them all to the list.
If you modify your SQL query a bit, you can use it as a LINQ query to sort your list: http://www.devcurry.com/2009/02/sorting-date-in-generic-list-using-linq.html
Then you can use your list as a DataSource for say, a GridView.
Dim grdViewVideos as New GridView
grdViewVideos.DataSource = lstVideos
The GridView will automatically generate columns based upon each attribute of the items in the list and populate rows based upon each items' data.
This is a bit generic I know, and in VB, but without seeing any of your code I just wanted to give you a few ideas.
I ended up creating image buttons from the code behind, which seems like an easier solution:
protected void Page_Load(object sender, EventArgs e)
{
MySqlConnection DBConn = new MySqlConnection(WebConfigurationManager.ConnectionStrings["LocalMySqlServer"].ConnectionString);
MySqlCommand DBCmd = new MySqlCommand();
DBConn.Open();
try
{
DBCmd = new MySqlCommand("SELECT VideoID, ImgFileName FROM (SELECT VideoID, ImgFileName FROM videos ORDER BY Rank DESC) as myalias LIMIT 10", DBConn);
MySqlDataReader reader = DBCmd.ExecuteReader();
String ImgFileName;
Int64 VidID;
while (reader.Read())
{
ImgFileName = reader["ImgFileName"].ToString().Trim();
VidID = (Int64)reader["VideoID"];
ImageButton imgBtn = new ImageButton();
imgBtn.ID = "image_id";
imgBtn.ImageUrl = "~/Uploads/Thumbs/" + ImgFileName;
imgBtn.PostBackUrl = "~/PlayVideo.aspx?video=" + VidID;
placeHolder.Controls.Add(imgBtn);
}
reader.Close();
catch (Exception exp)
{
Response.Write(exp);
}
DBCmd.Dispose();
DBConn.Close();
DBConn = null;
}
and the placeholder control in the aspx file where you want the thumbnails to go:
<asp:PlaceHolder ID="placeHolder" runat="server"></asp:PlaceHolder>
精彩评论