|
|||||||||||||||||||||
|
|||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionIn this article, I’m going to present an implementation of an ASP.NET slide show user control. It is an HTML table based slide show. It is a very simple user control and can easily be modified depending upon your needs. BackgroundA few months ago, I was searching the Internet for a JavaScript slide show that could easily be applied to a About the User ControlAt runtime, the slide show user control has the following appearance:
The upper part contains the title of the slide show while the lower part contains the left and right arrows (hyperlinks). The middle part consists of the necessary images for the slide show. When we put the mouse pointer over the left arrow, the images start to move from right to left, and when we put the mouse pointer over the right arrow, the images start to move from left to right. When we remove the mouse pointer from the left or right arrow, the slide show doesn’t stop immediately. The movement of the current image first completes, and then the slide show stops. The speed of the slide show differs from browser to browser in this user control. Properties of the User ControlThe slide show user control has the following public properties:
Events of the User ControlThe slide show user control has only one server-side event:
Using the User ControlDrag and drop the user control into an ASPX page and set the values for its properties. The HTML code for this user control looks like: <uc1:SlideShow ID="SlideShow1" runat="server" NoOfVisibleImages="4"
ImageHeight="71" ImageWidth="95" LeftImage="~/ArrowImages/back[1].gif"
RightImage="~/ArrowImages/next[1].gif" Title="Image Slide Show"
ArrowAlign="Left" TitleAlign="Left" />
Now, bind the slide show user control to an image data source of type SlideShow1.ImageDataSource = GetImageItems();
where the method private ImageItems GetImageItems()
{
ImageItems oImageItems = new ImageItems();
ImageItem oImageItem = null;
FileInfo[] Images = new DirectoryInfo(Server.MapPath("Images")).GetFiles("*.*");
foreach (FileInfo Image in Images)
{
oImageItem = new ImageItem();
oImageItem.URL = string.Format("~/Images/{0}", Image.Name);
oImageItem.ToolTip = Image.Name;
oImageItems.Add(oImageItem);
}
return oImageItems;
}
You can also handle the < ... EnableImageClick="true" ... >
Then, create an event handler for the protected void SlideShow1_Click(object sender, SlideShowImageEventArgs e)
{
...
}
Next, wire up the event handler in the control tag by adding the prefix < ... OnClick="SlideShow1_Click" ... >
I’ll discuss about the User Control HTMLThe HTML of this UserControl is very simple. <asp:Panel ID="pnlParent" runat="server">
<asp:Panel ID="pnlTitle" runat="server" HorizontalAlign="Left">
<asp:Label ID="lblTitle" runat="server" Text="Slide Show"
Font-Bold="True" Font-Names="Garamond" ForeColor="#404040">
</asp:Label>
</asp:Panel>
<asp:Panel ID="pnlBase" runat="server">
<table id="tblBase" border="0" cellpadding="0" cellspacing="0"
runat="server" style="position:absolute; left: 0px;">
<tr>
<td>
<asp:DataList ID="dataList" runat="server" GridLines="Vertical"
RepeatDirection="Horizontal" ShowHeader="False"
CellPadding="0" OnItemCreated="dataList_ItemCreated"
ShowFooter="False">
<ItemTemplate>
<asp:ImageButton ID="imgSlideShow" alt="" ImageUrl='<%# Eval("URL") %>'
ToolTip='<%# Eval("ToolTip") %>' runat="server" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:DataList>
</td>
</tr>
</table>
</asp:Panel>
<asp:Panel ID="pnlNavigation" runat="server" BackColor="#E0E0E0">
<asp:HyperLink ID="aLeft" runat="server"></asp:HyperLink>
<asp:HyperLink ID="aRight" runat="server"></asp:HyperLink>
</asp:Panel>
</asp:Panel>
There are three The second The third User Control CodeThe public partial class SlideShow : System.Web.UI.UserControl
{
...
}
The if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
ImageButton imgSlideShow = (ImageButton)e.Item.FindControl("imgSlideShow");
imgSlideShow.Height = Unit.Pixel(_ImageHeight);
imgSlideShow.Width = Unit.Pixel(_ImageWidth);
}
There are two private string GetJS()
{
StringBuilder JS = new StringBuilder();
JS.Append("<script type=\"text/javascript\">\n");
...
JS.Append("</script>");
return JS.ToString();
}
private string GetCSS ()
{
StringBuilder CSS= new StringBuilder();
CSS.Append("<style type=\"text/css\">\n");
...
CSS.Append("</style>");
return CSS.ToString();
}
The CSS has been attached to the page’s //Adding a stylesheet to the page header
((HtmlHead)this.Page.Header).Controls.Add(new LiteralControl(GetCSS()));
Note that in order to attach the necessary CSS through code, the page’s <head runat="server">
The JavaScript has been registered to the page in the //Register client side script.
this.Page.ClientScript.RegisterStartupScript(typeof(Page), "MyScript", GetJS());
Client side aLeft.Attributes["onmouseover"] = "javascript:Start();MoveLeft();";
aLeft.Attributes["onmouseout"] = "javascript:Stop();StopMoveLeft();";
aRight.Attributes["onmouseover"] = "javascript:Start();MoveRight();";
aRight.Attributes["onmouseout"] = "javascript:Stop();StopMoveRight();";
The slide show user control has following definition for its public event SlideShowClick Click;
The delegate that represents the public delegate void SlideShowClick(object sender, SlideShowImageEventArgs e);
The protected void imgSlideShow_Click(object sender, ImageClickEventArgs e)
{
// Fire the event.
if (Click != null)
Click(this, new SlideShowImageEventArgs((ImageButton)sender, e.X, e.Y));
}
The if (_EnableImageClick)
imgSlideShow.Click += new ImageClickEventHandler(imgSlideShow_Click);
else
imgSlideShow.Attributes["onclick"] = "javascript:return false;";
Note that if <pages enableEventValidation="false"></pages>
If you don’t add this line of the code, then you will face the following error:
I’ve tried to handle it through code, but couldn’t. If any one has ideas about it, then please let me know.
public class SlideShowImageEventArgs : EventArgs
{
private ImageButton _ImageButton = null;
private int _X = 0;
private int _Y = 0;
public int X
{
get { return _X; }
}
public int Y
{
get { return _Y; }
}
public string URL
{
get { return _ImageButton.ImageUrl; }
}
public string ToolTip
{
get { return _ImageButton.ToolTip; }
}
public SlideShowImageEventArgs(ImageButton O, int X, int Y)
{
_ImageButton = O;
_X = X;
_Y = Y;
}
}
Images SourceThe slide show user control has a public property, public class ImageItems : List<ImageItem>
{
public ImageItems() { }
}
public class ImageItem
{
private string _ToolTip = string.Empty;
public string ToolTip
{
get { return _ToolTip; }
set { _ToolTip = value; }
}
private string _URL = string.Empty;
public string URL
{
get { return _URL; }
set { _URL = value; }
}
// Default constructor.
public ImageItem() { }
public ImageItem(string ToolTip, string URL)
{
this._ToolTip = ToolTip;
this._URL = URL;
}
}
JavaScript MethodsThere are six JavaScript methods that are responsible for performing the slide show on the client side, namely: ConclusionSo, this is my approach to implement an ASP.NET slide show using the
| ||||||||||||||||||||