1

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

        }
    }

提前致谢!

4

1 回答 1

1

1)在顶部的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

于 2012-08-29T23:58:32.880 回答