使用更新面板旋转不同的.ascx文件

时间:2012-08-29 23:14:08

标签: c# asp.net

我过去曾使用过更新面板和计时器来使用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;

        }
    }

提前致谢!

1 个答案:

答案 0 :(得分:1)

1)在asp

的aspx文件中添加以下指令
<%@ Register TagPrefix="uc1" TagName="ucLoanTerms" Src="ucLoanTerms.ascx" %>

(2)然后在表单中添加以下内容,以便动态加载用户控件

<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>

(3)在代码后面的代码跟踪文件Page_Load方法,

PlaceHolder1.Controls.Clear();
ucLoanTerms lt = (ucLoanTerms) LoadControl("ucLoanInfo.ascx");
PlaceHolder1.Controls.Add(lt);

我还发现了一篇关于动态加载用户控件的好文章。

http://geekswithblogs.net/rashid/archive/2007/08/11/Loading-UserControl-Dynamically-in-UpdatePanel.aspx