1

根据 asp.net 中的下拉列表选择选项从文件夹动态加载多个图像。谁能帮我在visual studio中实现这个?这是我尝试过的代码,帮助我更好的选择或修改它,我还需要将图像输出绑定到图像按钮控件作为幻灯片。

 <script type="text/javascript">
   function new1(city) {
      switch (city) {
           case "Patna":
               var imlocation = "Patna/";
               var currentdate = 0;
               var image_number = 0;
               function ImageArray(n) {
                   this.length = n;
                   for (var i = 1; i <= n; i++) {
                       this[i] = ' '
                   }
               }
               image = new ImageArray(2)
               image[0] = '2.jpg'
               image[1] = '3.jpg'
               var rand = 60 / image.length
               function randomimage() {
                   currentdate = new Date()
                   image_number = currentdate.getSeconds()
                   image_number = Math.floor(image_number / rand)
                   return (image[image_number])
               }
                                  document.write("<img src='" + imlocation + randomimage() + "'>");

           case "Bhopal":
               var imlocation1 = "Bhopal/";
               var currentdate1 = 0;
               var image_number1 = 0;
               function ImageArray1(n) {
                   this.length = n;
                   for (var i = 1; i <= n; i++) {
                       this[i] = ' '
                   }
               }
               image1 = new ImageArray1(3)
               image1[0] = '2.jpg'
               image1[1] = '4.jpg'
               image1[2] = '1.jpg'
               var rand1 = 100/ image1.length
               function randomimage1() {
                   currentdate1 = new Date()
                   image_number1 = currentdate1.getSeconds()
                   image_number1 = Math.floor(image_number1 / rand1)
                   return (image1[image_number1])
               }
               document.write("<img src='" + imlocation1 + randomimage1() + "'>");

       }
   }

<asp:DropDownList ID="DropDownList1" runat="server" onchange="new1(this.value)"
                DataSourceID="SqlDataSource1" DataTextField="City" DataValueField="City" 
                onselectedindexchanged="DropDownList1_SelectedIndexChanged" 
                AutoPostBack="True" >
            </asp:DropDownList>
4

1 回答 1

2

最后我自己找到了答案,使用 ajax 幻灯片扩展器在 webservice 中使用下面的代码。

[System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
    [System.Web.Services.WebMethod()]
    [System.Web.Script.Services.ScriptMethod]
    public AjaxControlToolkit.Slide[] GetSlides(string contextKey)
    {

        string[] imagenames = System.IO.Directory.GetFiles(HttpContext.Current.Server.MapPath("~/" + contextKey ));
        AjaxControlToolkit.Slide[] photos = new AjaxControlToolkit.Slide[imagenames.Length];
        for (int i = 0; i < imagenames.Length; i++)
        {
            string[] file = imagenames[i].Split('\\');
            photos[i] = new AjaxControlToolkit.Slide(contextKey + "/" + file[file.Length - 1], file[file.Length - 1], "");
        }
        return photos;

    }

  }

在幻灯片扩展器中

 <asp:SlideShowExtender ID="SlideShowExtender1" TargetControlID="Image1"     SlideShowServiceMethod="GetSlides" SlideShowServicePath="~/WebService1.asmx" Loop="true" runat="server"  UseContextKey="true" 
    AutoPlay="True" BehaviorID="b1" >        
</asp:SlideShowExtender>

终于在 page_load

 if (Page.IsPostBack)
 {
     SlideShowExtender1.ContextKey = DropDownList1.SelectedValue;
 }
于 2013-01-10T20:18:03.487 回答