从数据库获取图像到Ajax ToolKit幻灯片放映扩展程序
问题描述:
大家好,
如何从数据库检索图像到Ajax ToolKit幻灯片扩展器
i使用下面的代码在幻灯片中显示图像显示
代码工作正常但是,
而不是webservice方法中存在的这些静态图像,我需要从数据中获取我的图像基础..
注意:我使用c#与asp.net,..数据库中的图像类型是nvarchar(50)
以下所有代码在一页[default.aspx]
hi guys ,
how to retrieve images from database to Ajax ToolKit slideshow extender
i use the code below to display images in slide show
the code working fine but,
Instead of these static images which exist in webservice method , i need to get my images from data base ..
Note :i using c# with asp.net ,..the images type in database is nvarchar(50)
All the below code in one page [default.aspx]
<%@ Page Language="C#" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
/*the method below which contain the images ...
.........i need to get them from database instead the below way */
public static AjaxControlToolkit.Slide[] GetSlides()
{
return new AjaxControlToolkit.Slide[] {
new AjaxControlToolkit.Slide("images/1.gif", "Blue Hills", "Go Blue"),
new AjaxControlToolkit.Slide("images/2.jpg", "Water lillies", "Lillies in the water"),
new AjaxControlToolkit.Slide("images/3.jpg", "Sedona", "Portrait style picture")
};
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Ajax Control - Slide Show</title>
<style>
/* SlideShow styles */
.slideTitle
{
font-weight:bold;
font-size:small;
font-style:italic;
}
.slideDescription
{
font-size:small;
font-weight:bold;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
<div style="text-align:center"> <b>
SlideShow Demonstration</b><br /><br /></div>
<div style="text-align:center">
<asp:Label runat="Server" ID="imageTitle" CssClass="slideTitle"/><br />
<asp:Image ID="Image1" runat="server"
Height="300"
Style="border: 1px solid black;width:auto"
ImageUrl="images/3.jpg"
AlternateText="Blue Hills image" />
<asp:Label runat="server" ID="imageDescription" CssClass="slideDescription"></asp:Label><br /><br />
<asp:Button runat="Server" ID="prevButton" Text="Prev" Font-Size="Larger" />
<asp:Button runat="Server" ID="playButton" Text="Play" Font-Size="Larger" />
<asp:Button runat="Server" ID="nextButton" Text="Next" Font-Size="Larger" />
<asp:SlideShowExtender ID="slideshowextend1" runat="server"
TargetControlID="Image1"
SlideShowServiceMethod="GetSlides"
AutoPlay="true"
ImageTitleLabelID="imageTitle"
ImageDescriptionLabelID="imageDescription"
NextButtonID="nextButton"
PlayButtonText="Play"
StopButtonText="Stop"
PreviousButtonID="prevButton"
PlayButtonID="playButton"
Loop="true" />
</div>
</form>
</body>
</html>
答
这里有一个很好的链接,你到底做了什么试图实现。
这是在VB中,但是转换为c#并不是那么多工作。
http://www.dotnetcode.in/2011/07/how-to-slideshow-images-from -database.html [ ^ ]
问候
Hi,
here's a great link with exactly does what you're trying to achieve.
It's in VB, but translation to c# isn't so much work.
http://www.dotnetcode.in/2011/07/how-to-slideshow-images-from-database.html[^]
Regards
Http Handler:
Http Handler:
<%@ WebHandler Language="C#" Class="ShowImage" %>
using System;
using System.Web;
public class ShowImage : IHttpHandler {
public void ProcessRequest (HttpContext context) {
Int32 theID;
if (context.Request.QueryString["id"] != null)
theID = Convert.ToInt32(context.Request.QueryString["id"]);
else
throw new ArgumentException("No parameter specified");
//theID = 2;
context.Response.ContentType = "image/jpeg";
System.IO.Stream strm = DisplayImage(theID);
byte[] buffer = new byte[2048];
int byteSeq = strm.Read(buffer, 0, 2048);
while (byteSeq > 0)
{
context.Response.OutputStream.Write(buffer, 0, byteSeq);
byteSeq = strm.Read(buffer, 0, 2048);
}
}
public System.IO.Stream DisplayImage(int theID)
{
string cs = System.Configuration.ConfigurationManager.ConnectionStrings["TempConnectionString"].ConnectionString;
using (System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection(cs))
{
string sql = "SELECT g_images FROM tblImage WHERE id = @ID";
using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sql, connection))
{
cmd.CommandType = System.Data.CommandType.Text;
cmd.Parameters.AddWithValue("@ID", theID);
connection.Open();
object theImg = cmd.ExecuteScalar();
try
{
return new System.IO.MemoryStream((byte[])theImg);
}
catch
{
return null;
}
finally
{
cmd.Dispose();
connection.Close();
connection.Dispose();
}
}
}
}
public bool IsReusable {
get {
return false;
}
}
}
Slide Show Page(Code Behind):
Slide Show Page(Code Behind):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using COSTACCLib;
using System.Data;
public partial class Gallery : CustomBasePage
{
public static string comCode;
public static string proCode;
CAProcessAccess ImageProc = new CAProcessAccess("master");
protected void Page_Load(object sender, EventArgs e)
{
comCode = txtComCode.Text;
proCode = txtProCode.Text;
}
protected void btnShowGallery_Click(object sender, EventArgs e)
{
Image1.Visible = true;
prevButton.Visible = true;
playButton.Visible = true;
nextButton.Visible = true;
}
[System.Web.Services.WebMethodAttribute()]
[System.Web.Script.Services.ScriptMethodAttribute()]
public static AjaxControlToolkit.Slide[] GetSlides()
{
CAProcessAccess ImageProc = new CAProcessAccess("master");
DataSet ds = ImageProc.GetRecords("", "SP_Gallery_Image", "GETID", comCode, proCode, "", "");
DataTable dt = ds.Tables[0];
AjaxControlToolkit.Slide[] slides = new AjaxControlToolkit.Slide[dt.Rows.Count];
for (int i = 0; i < dt.Rows.Count; i++)
{
int imgID = Convert.ToInt32(dt.Rows[i]["ID"]);
slides[i] = new AjaxControlToolkit.Slide("ShowImage.ashx?ID=" + imgID, "test", "test");
}
return slides;
}
}
Slide Show Page:
Slide Show Page:
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Gallery.aspx.cs" Inherits="Gallery" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
<ajaxToolkit:ToolkitScriptManager ID="ScriptManager1" runat="server">
</ajaxToolkit:ToolkitScriptManager>
<script type="text/javascript">
function pageLoad() {
var slide =
find('SSBehaviorID');
if (slide != null) {
slide.add_slideChanging(animateSlides);
var ae =
find('SSBehaviorID'); if (slide != null) { slide.add_slideChanging(animateSlides); var ae =