asp.net C# 中的幻灯片在第一张图片处停止

slideshow in asp.net C# stops at first image

我在学习asp.net。在这个练习中,我想要实现的是编译一个 3 图片幻灯片。该页面仅编译并显示第一张图片。该页面假设以 2 秒为间隔更新或刷新以显示每个图像。将我的代码基于另一个使用条件语句的工作程序,基本区别在于除了条件 if 语句之外,我还使用了一个数组。我没有使用 Trigger 元素,因为我基于我的代码没有使用它。这是讲义的摘录:"The control specifies a control and event that will cause a partial page update for the UpdatePanel that contains this trigger reference. The ControllD attribute sets the name of the control that triggers an asynchronous postback for an UpdatePanel control." 出于某种原因,我认为我的问题可能出在这里。但是因为我是新人,所以我真的不知道。这就是我来这里寻求帮助的原因。请。如果有人能给我指出正确的方向,我想我可以让这个程序按预期的方式工作。这是代码

 <%@ Page Language="C#" %>
 <script runat="server" type="text/vb">
 private void Timer1_Tick(Object sender, EventArgs e)
 {
 string[] circle = new string[3];
 circle[0]="red";
 circle[1]="green";
 circle[2]="blue";
 int i = Convert.ToInt32(circle);
 i = i + 1;
 if (i > 3)
 { 
 i = 1;
 }
 image1.ImageUrl = "red " + i + ".png";
  }
 </script>
 <!Doctype html>
 <html>
 <body>
 <form id="form1" runat="server">
 <asp:ScriptManager runat="server" id="ScriptManager1">
 </asp:ScriptManager>
 <asp:UpdatePanel runat="server" id="UpdatePanel1" UpdateMode="Conditional">
 <ContentTemplate>
 <asp:Timer runat="server" id="Timer1" Interval="2000" OnTick="Timer1_Tick" />
 <asp:Image id="image1" runat="server" ImageUrl="red.png"  AlternateText="Circles" />
 </ContentTemplate>
  </asp:UpdatePanel>
 </form>
 </body>
 </html>

您是否尝试过提取 C# 代码并查看控制台中发生了什么?

我想你不能将字符串数组转换为整数

int i = Convert.ToInt32(circle);

你是说

int i = circle.Length?

编辑:

除了你还没有正确的"i"(你可以添加一段逻辑来解析image1.ImageUrl并获取当前索引),我猜你的

 image1.ImageUrl = "red " + i + ".png";

实际上是

 image1.ImageUrl = circle[i - 1] + ".png";

编辑2:

要得到你想要的 "i",假设你的图像总是 xxx.png 并且 xxx 总是存在于圆数组中,你可以通过点的位置对 imageURL 进行子串以获得 "xxx",然后在圆数组中找到匹配的字符串,并得到它的索引。

我猜问题出在数组初始化上。每一层都与计数器一起创建全新的对象。

string[] circle = new string[3];
circle[0]="red"; 
circle[1]="green";
circle[2]="blue"; 
int i = Convert.ToInt32(circle);

这导致计数器重置为每个厚度的起始值。 我最好将初始化代码移到 Timer1_Tick 函数之外的某个地方。

请原谅我的糟糕评论,但今天我很赶时间。 我假设该代码仅用于教育目的。 Javascript 更适合您要完成的任务。每次刷新时,c# 或每种服务器端语言都会在服务器上进行一次往返(页面重新加载)

您需要使幻灯片计数器变量在回发过程中保持不变。 有很多方法可以完成任务数据库、会话变量、ViewState 对象(也许最后一种是最好的,但您可以继续使用它)。

这里是您的代码的一个小变体。希望对你有帮助。

<html>
  <body>
    <form id="form1" runat="server">
        <asp:ScriptManager runat="server" ID="ScriptManager1">
        </asp:ScriptManager>
        <asp:UpdatePanel runat="server" ID="UpdatePanel1" UpdateMode="Conditional">
            <ContentTemplate>
                <asp:Timer runat="server" ID="Timer1" Interval="2000" OnTick="Timer1_Tick" />
                <asp:Label Text="no images loaded" ID="LabelImageName" runat="server" />
            </ContentTemplate>
        </asp:UpdatePanel>
    </form>
</body>
</html>

<script runat="server" type="text/vb">

string[] _arrImgNames = new string[] { "red", "green", "blue" };


protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        // set counter session variable only the first time the page is loaded (not when script refreshs)
        Session["counter"] = -1;
    }
}


private void Timer1_Tick(Object sender, EventArgs e)
{

    string textToShow = String.Format(
        "Now the program shows image '{0}' "
        , this.GetNextSlideImageName());

    LabelImageName.Text = textToShow;

}


private string GetNextSlideImageName()
{
    // get the counter from session variable 
    int counter = Convert.ToInt32(Session["counter"]);

    // increase the counter 
    counter++;

    if (counter >= this._arrImgNames.Length)
    {
        counter = 0;
    }

    // updating the session variable 
    Session["counter"] = counter;

    // compose the image name 
    string imageName = String.Format("{0}.png", this._arrImgNames[counter]);

    return imageName;
}

</script>