动态创建ImageButton并将其onclick函数设置为ServerTransfer到另一个WebForm - c#

因此,基本上,我会在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();
        }

参考方案

这是我得到的:

    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所述,您需要关联click事件,而不仅仅是添加它。

ASP.NET-如何更改JSON序列化的方式? - javascript

我正在使用ASP.NET通过以下查询返回Json文件:public ActionResult getTransactionTotals(int itemID) { DBEntities db = new DBEntities(); var query = from trans in db.Transactions // Linq query removed …

asp.net:treeview-显示文本框是否选中? - c#

我需要向用户显示字符串列表。用户可以选择多个字符串。如果选择了一组特定的字符串,则每个字符串旁边都会出现一个文本框。我想做的是向用户显示一个TREEVIEW,每个节点都是字符串之一。他们将通过选中复选框来选择所需的每个字符串。问题:如果用户选择特定的复选框,则我需要一个文本框来显示用户的GET输入。问题:如何从用户使用树形视图获取字符串输入? 参考方案 Tr…

当回复有时是一个对象有时是一个数组时,如何在使用改造时解析JSON回复? - java

我正在使用Retrofit来获取JSON答复。这是我实施的一部分-@GET("/api/report/list") Observable<Bills> listBill(@Query("employee_id") String employeeID); 而条例草案类是-public static class…

.NET C#Webbrowser填充输入,不带ID或类名 - javascript

我需要在网络浏览器中填写一个输入,但这不起作用。我认为必须使用name属性来完成,但是怎么做呢?foreach (HtmlElement login in webBrowser1.Document.GetElementsByTagName("input")) { if (login.GetAttribute("name"…

ASP.net C#崩溃一行 - c#

我有一个母版页,在on load事件中包含以下几行: string menuIDdata = Page.Request.QueryString["mid"]; menuID = 0; // Get the menu ID if (!int.TryParse(menuIDdata, out menuID)) { menuID = 0; } …