When surfing the Internet, I often see several advertising images. The following is the image code. I think it is quite good. I am learning how to create a user control. I just want to make the rotation advertisement a control for my own convenience. Most of what I see on the Internet is made of JavaScript. I am not familiar with JavaScript, so I found some js code for this function on the Internet. I adapted it into an Asp.net control and used it for myself. I like to write code in the background code to bind a database. Because the first write, a good number of functions are not available, now only negative values can be achieved through the background Code, does not support interface design. (In fact, it is the Javascript for translation, and there is no technical content! I wrote a blog for the first time. I don't know what to write. I just want to show it out. Haha! Right to entertain !)
Namespace webcontrollib
{
/// <Summary>
/// Ad summary.
/// </Summary>
[Toolboxdata ("<{0}: trunad runat = Server> </{0}: trunad>")]
Public class trunad: system. Web. UI. webcontrols. webcontrol
{
/// <Summary>
/// Present the control to the specified output parameter.
/// </Summary>
/// <Param name = "output"> HTML writer to be written </param>
Protected override void render (htmltextwriter output)
{
// Output. Write (text );
Output. Write (createad ());
}
# Region variable
Private numalign _ numalign;
Private numvlign _ numvlign;
Private int _ imgwidth;
Private int _ imgheight;
Private int _ numpicwidth;
Private int _ numpicheight;
Private int _ changetime;
Private string _ target;
Private system. Collections. arraylist _ ads = new system. Collections. arraylist ();
# Region JS
Private const string jsfunctoin = @ "<script language = JavaScript>
VaR cpad = new array ();
VaR cpadlink = new array ();
VaR cpadmsg = new array ();
VaR adnum = [arraynum];
VaR coll = 0;
[Initarray]
VaR preloadedimages = new array ();
For (I = 1; I <cpad. length; I ++)
{
Preloadedimages [I] = new image ();
Preloadedimages [I]. src = cpad [I];
}
Function jump2url ()
{
Jumpurl = cpadlink [adnum];
Jumptarget = '[target]';
If (jumpurl! = '')
{
If (jumptarget! = '')
Window. Open (jumpurl, jumptarget );
Else
Location. href = jumpurl;
}
}
Function changeimg (N)
{
Adnum = N;
Switch (adnum)
{
[Case1]
}
Window. clearinterval (thetimer );
Adnum = adNum-1;
Nextad ();
}
Function nextad ()
{
Coll ++;
If (Coll> 1)
{
Switch (adnum + 1)
{
[Case2]
}
}
If (adnum <cpad. Length-1)
{
// Document. Images. cpadrush. src = cpad [adnum];
Adnum ++;
}
Else
{
Adnum = 0;
// Document. Images. cpadrush. src = cpad [adnum];
}
Settransition ();
Document. Images. cpadrush. src = cpad [adnum];
Playtransition ();
Displaystatusmsg ();
Thetimer = setTimeout ("" nextad () "", [changetime]);
}
Function settransition ()
{
If (document. All)
{
Document. Images. cpadrush. Filters. revealtrans. Transition = 23;
Document. Images. cpadrush. Filters. revealtrans. Apply ();
}
}
Function playtransition ()
{
If (document. All)
Document. Images. cpadrush. Filters. revealtrans. Play ()
}
Function displaystatusmsg ()
{
Status = cpadmsg [adnum];
Document. returnvalue = true;
}
</SCRIPT> ";
Private const string adhtml = @"
<Tr>
<TD width = "" 100% "" Height = "" 100% "">
<Div style = "" width: 100% "" align = "" center "">
<A href = "" javascript: jump2url () "">
</A>
<Script language = "" JavaScript ""> nextad () </SCRIPT>
</Div>
</TD>
</Tr> ";
Private const string numhtml = @"
<Tr>
<TD>
<Table border = "" 0 "" cellspacing = "" 1 "" cellpadding = "" 0 "" width = "" 100% "">
<Tr>
[Num]
</Tr>
</Table>
</TD>
</Tr> ";
Private const string tableheader = @ "<Table cellspacing =" "0" "cellpadding =" 0 "> ";
Private const string numahref = @"
<TD width = "" [numpicwidth] ""> <a onmouseover = "" changeimg ([ID]) "" href = "" # ""> </> </TD>
";
# Endregion
# Endregion
# Region attributes
Public String target
{
Get
{
If (_ target = NULL)
Return "_ blank ";
Return _ target;
}
Set {_ target = value ;}
}
Public numalign
{
Get {return _ numalign ;}
Set {_ numalign = value ;}
}
Public numvlign
{
Get {return _ numvlign ;}
Set {_ numvlign = value ;}
}
Public int imgwidth
{
Get {return _ imgwidth ;}
Set {_ imgwidth = value ;}
}
Public int imgheight
{
Get {return _ imgheight ;}
Set {_ imgheight = value ;}
}
Public int numpicwidth
{
Get {return _ numpicwidth ;}
Set {_ numpicwidth = value ;}
}
Public int numpicheight
{
Get {return _ numpicheight ;}
Set {_ numpicheight = value ;}
}
Public int changetime
{
Set {_ changetime = value ;}
Get {return _ changetime * 1000 ;}
}
# Endregion
# Region Public Method
Public void addad (aditem item)
{
_ Ads. Add (item );
}
# Endregion
# Region private Method
Private string initadhtml ()
{
String tempstr = adhtml;
Tempstr = tempstr. Replace ("[imgheight]", this. imgheight. tostring ());
Tempstr = tempstr. Replace ("[imgwidth]", this. imgwidth. tostring ());
Return tempstr;
}
Private string initnumhtml ()
{
String tempstr = numhtml;
String tempnumahref = "";
String tempnum = "";
String tempnulltd = "";
Int tempnumwidth;
Tempnumwidth = This. numpicwidth * _ ads. Count + _ ads. count;
For (INT I = 0; I <_ ads. Count; I ++)
{
Tempnumahref = numahref;
Tempnumahref = tempnumahref. Replace ("[ID]", I. tostring ());
Tempnumahref = tempnumahref. Replace ("[numpicheight]", this. numpicheight. tostring ());
Tempnumahref = tempnumahref. Replace ("[numpicwidth]", this. numpicwidth. tostring ());
If (I = 0)
{
Tempnumahref = tempnumahref. Replace ("[numpicpath]", (aditem) _ ads [I]). shownumpath );
}
Else
{
Tempnumahref = tempnumahref. Replace ("[numpicpath]", (aditem) _ ads [I]). hiddennumpath );
}
Tempnum + = tempnumahref + "/N ";
}
If (this. numalign = numalign. Center)
{
Int tempw = (this. imgwidth-tempnumwidth)/2;
Tempnulltd = "<TD width =/" "+ tempw +"/"> </TD> ";
Tempnum = tempnulltd + tempnum + tempnulltd;
}
Else if (this. numalign = numalign. Left)
{
Int tempw = This. imgwidth-tempnumwidth;
Tempnulltd = "<TD width =/" "+ tempw +"/"> </TD> ";
Tempnum = tempnum + tempnulltd;
}
Else
{
Int tempw = This. imgwidth-tempnumwidth;
Tempnulltd = "<TD width =/" "+ tempw +"/"> </TD> ";
Tempnum = tempnulltd + tempnum;
}
Tempstr = tempstr. Replace ("[num]", tempnum );
Return tempstr;
}
Private string initjsfunctin ()
{
String tempstr = jsfunctoin;
String tempinitarray = "";
String tempcase = "", tempcase2 = "";
Aditem tempitem = NULL, tempitem2 = NULL;
System. Collections. arraylist tempad = new system. Collections. arraylist ();
Tempad = (system. Collections. arraylist) _ ads. Clone ();
Tempstr = tempstr. Replace ("[arraynum]", _ ads. Count. tostring ());
Tempstr = tempstr. Replace ("[target]", this. Target );
Tempstr = tempstr. Replace ("[changetime]", this. changetime. tostring ());
For (INT I = 0; I <_ ads. Count; I ++)
{
Tempitem = (aditem) _ ads [I];
Tempinitarray + = "cpad [" + I. tostring () + "] =/" "+ tempitem. imgpath +"/";/N ";
Tempinitarray + = "cpadlink [" + I. tostring () + "] =/" "+ tempitem. url +"/";/N ";
Tempinitarray + = "cpadmsg [" + I. tostring () + "] =/" "+ tempitem. Title +"/";/N ";
Tempcase + = "case" + I. tostring () + ":/N ";
Tempcase + = "{/N ";
For (Int J = 0; j <tempad. Count; j ++)
{
Tempitem2 = (aditem) tempad [J];
If (j = I)
{
Tempcase + = "document. All. IMG" + I. tostring () + ". src =/" + tempitem. shownumpath + "/";/N ";
}
Else
{
Tempcase + = "document. All. IMG" + J. tostring () + ". src =/" "+ tempitem2.hiddennumpath +"/";/N ";
}
}
Tempcase + = "break;" +
"}/N ";
}
Tempstr = tempstr. Replace ("[initarray]", tempinitarray );
Tempstr = tempstr. Replace ("[case1]", tempcase );
Tempcase2 = tempcase. Replace ("case 0:", "case" + tempad. Count. tostring () + ":");
Tempstr = tempstr. Replace ("[case2]", tempcase2 );
Return tempstr;
}
Private string createad ()
{
String tempstr = "";
String temptableherder = tableheader;
Tempstr + = initjsfunctin ();
Tempstr + = temptableherder;
If (this. numvlign = numvlign. Top)
{
Tempstr + = initnumhtml ();
Tempstr + = initadhtml ();
}
Else
{
Tempstr + = initadhtml ();
Tempstr + = initnumhtml ();
}
Tempstr + = "</table> ";
Return tempstr;
}
# Endregion
}
Public class aditem
{
# Region variables and attributes
Private string _ imgpath;
Public String imgpath
{
Get
{
If (_ imgpath = NULL)
Return "";
Return _ imgpath;
}
Set {_ imgpath = value ;}
}
Private string _ URL;
Public String URL
{
Get
{
If (_ url = NULL)
Return "";
Return _ URL;
}
Set {_ url = value ;}
}
Private string _ shownumpath;
Public String shownumpath
{
Get
{
If (_ shownumpath = NULL)
Return "";
Return _ shownumpath;
}
Set {_ shownumpath = value ;}
}
Private string _ hiddennumpath;
Public String hiddennumpath
{
Get
{
If (_ hiddennumpath = NULL)
Return "";
Return _ hiddennumpath;
}
Set {_ hiddennumpath = value ;}
}
Private string _ title;
Public String title
{
Get
{
If (_ Title = NULL)
Return "";
Return _ title;
}
Set {_ Title = value ;}
}
# Endregion
Public aditem ()
{
}
Public aditem (string imgpath, string URL, String title, string shownumpath, string hiddennumpath)
{
This. _ imgpath = imgpath;
This. _ url = URL;
This. _ Title = title;
This. _ shownumpath = shownumpath;
This. _ hiddennumpath = hiddennumpath;
}
}
Public Enum numalign
{
# Region members
Left,
Center,
Right
# Endregion
}
Public Enum numvlign
{
Top,
Bottom
}
}