我过去曾使用更新面板和计时器来使用 ASP.net 创建幻灯片。我想做同样的事情,除了旋转图像,我想旋转 .ascx 文件。这可能吗?到目前为止,这是我所拥有的,但我被困在如何从代码隐藏文件中访问 .ascx 上。
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="SlideShow" runat="server" UpdateMode="Always">
<ContentTemplate>
<img runat="server" id="SlideShowImage1" src="~/Controls/Testimonials.ascx" alt="Testimonial" />
<asp:Label runat="server" ID="SlideShowText" Text="First Slide" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="SlideShowTimer" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>
<asp:Timer ID="SlideShowTimer" runat="server" Interval="15000" OnTick="SlideShowTimer_Tick">
</asp:Timer>
代码背后:
protected void SlideShowTimer_Tick(object sender, EventArgs e)
{
DateTime later = DateTime.Now.AddSeconds(.5);
while (DateTime.Now < later)
{
}
this.SlideShowImage1.Src = RandomImageName();
this.SlideShowText.Text = this.SlideShowImage1.Src.ToString();
}
private string RandomImageName()
{
Random rand = new Random();
int RandomInt = rand.Next(0, 5);
switch (RandomInt)
{
case 0:
return "~/Controls/Testimonials.ascx";
break;
case 1:
return "~/Controls/Testimonials2.ascx";
break;
case 2:
return "~/Controls/Testimonials3.ascx";
break;
default:
return "~/Controls/Testimonials4.ascx";
break;
}
}
提前致谢!