【问题标题】:Dynamically create ImageButton and set its onclick function to ServerTransfer to another WebForm动态创建 ImageButton 并将其 onclick 函数设置为 ServerTransfer 到另一个 WebForm
【发布时间】:2014-07-24 16:14:00
【问题描述】:

所以基本上我在 Div 元素内使用 for 循环创建 ImageButtons,
但是我在创建此 ImageButtons 时设置的 onclick 功能不起作用并且它不会传输。所以我想我没有正确添加该功能,尽管下面的按钮功能工作正常

 protected void Page_Load(object sender, EventArgs e)
        {
              foreach (string strFileName in Directory.GetFiles(Server.MapPath("~/path/")))
            {
                  ImageButton imageButton = new ImageButton();
                  FileInfo fileInfo = new FileInfo(strFileName);
                  imageButton.ImageUrl = "~/path/" + fileInfo.Name.ToString();
                  imageButton.Attributes.Add("ID" , strFileName);                                  
                  imageButton.Attributes.Add("class","imgOne");
                  imageButton.Attributes.Add("runat", "server");
                  imageButton.Attributes.Add("OnClick", "toImageDisplay");
                  photos.Controls.Add(imageButton);



            }



        }
        public void toImageDisplay() 
        {
            Server.Transfer("ImageDisplay.aspx");
        }

        protected void Unnamed1_Click(object sender, EventArgs e)
        {
            toImageDisplay();
        }

【问题讨论】:

    标签: c# javascript asp.net webforms


    【解决方案1】:

    这是我得到的:

        private void LoadPictures()
        {
            foreach (string strFileName in Directory.GetFiles(Server.MapPath("~/path/")))
            {
                ImageButton imageButton = new ImageButton();
                FileInfo fileInfo = new FileInfo(strFileName);
                imageButton.ImageUrl = "~/path/" + fileInfo.Name.ToString();
                imageButton.Click += new ImageClickEventHandler(imageButton_Click);
                imageButton.ID = Path.GetFileName(strFileName);
                photos.Controls.Add(imageButton);
                //imageButton.Attributes.Add("ID", strFileName);
                //imageButton.Attributes.Add("class", "imgOne");
                //imageButton.Attributes.Add("runat", "server");
                //imageButton.Attributes.Add("OnClick", "toImageDisplay");
            }
        }
    
        void imageButton_Click(object sender, ImageClickEventArgs e)
        {
            //your code...
        }
    

    在页面加载时调用 LoadPictures()。

    正如 elaw7 所提到的,您需要连接点击事件而不是仅仅添加它。

    【讨论】:

      【解决方案2】:

      您需要连接事件而不是添加 onclick 属性。有两种方法可以解决这个问题(在这两种方法中都不需要手动添加 runat=server):

      1.

      foreach (string strFileName in Directory.GetFiles(Server.MapPath("~/path/")))
      {
             ImageButton imageButton = new ImageButton();
             FileInfo fileInfo = new FileInfo(strFileName);
             imageButton.ImageUrl = "~/path/" + fileInfo.Name.ToString();
             imageButton.Attributes.Add("ID" , strFileName);                                  
             imageButton.Click += Unnamed1_Click;
             photos.Controls.Add(imageButton);
      }
      

      2。 第二个涉及 javascript... 在您的代码中,而不是指定要调用的服务器端方法,只需使用:

      imageButton.Attributes.Add("onclick", string.format("location.href('{0}');","whateverURL.html"));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-03-17
        • 1970-01-01
        • 1970-01-01
        • 2017-12-16
        • 2012-03-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多