【问题标题】:Accessing image button created in code behind file on aspx访问在 aspx 文件后面的代码中创建的图像按钮
【发布时间】:2015-08-02 00:55:23
【问题描述】:

我在代码隐藏文件中创建了一个图像按钮。它有一个事件 imageButton_Click。我还想使用这个图像按钮和事件来为 aspx page.amspanel(基本上是一个面板)中的更新面板创建一个异步回发触发器,它正在使用图像按钮从文件夹中加载所有图像。当我单击 amspanel 中的图像时,我基本上是在尝试在页面上进行部分回帖,而不是再次加载 amspanel。

可以这样做吗?现在它说....在 UpdatePanel 'UpdatePanel1' 中的触发器的关联控件 'imageBtn1' 上找不到名为 'imageButton_Click' 的事件。

        private void LoadImages()
        {
        var fileIdx = 0;

            foreach (string strfile in Directory.GetFiles(Server.MapPath("~/images")))
            {
                fileIdx++;
                ImageButton imageButton = new ImageButton() { ID = "imageBtn" + fileIdx };
                FileInfo fi = new FileInfo(strfile);
                imageButton.ImageUrl = "~/images/" + fi.Name;
                imageButton.Height = Unit.Pixel(100);
                imageButton.Style.Add("padding", "5px");
                imageButton.Width = Unit.Pixel(100);
                imageButton.Click += new ImageClickEventHandler(imageButton_Click);
                AMSPanel1.Controls.Add(imageButton);
                AMSPanel1.Height=Unit.Pixel(860);
                UpdatePanel1.Triggers.Add(new AsyncPostBackTrigger
                {
                    ControlID = imageButton.ID,
                    EventName = "imageButton_Click"
                });


            }
        }
        protected void imageButton_Click(object sender, ImageClickEventArgs fi)
        {

            testimage.ImageUrl = ((ImageButton)sender).ImageUrl;

        }



// In aspx file i am doing this.AMSPanel is just a panel control.

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
      <ContentTemplate>
        <asp:Image ID="testimage" runat="server"  />
      </ContentTemplate>
</asp:UpdatePanel>


<ams:AMSPanel ID="AMSPanel1" CssClass="CustomWidth" runat="server" Width="130px" Height="700px" ScrollBars="Vertical"> 
</ams:AMSPanel>

【问题讨论】:

  • 你给它起名字了吗?没有名字你可能有工作找到它
  • 是的 imagebutton 是它的名字
  • 你确定吗?它不在您发布的代码中,并会解释您的问题
  • 您没有为新按钮命名。严重地。这就是为什么你找不到它
  • 哪一行说找不到?

标签: c# asp.net


【解决方案1】:

你需要添加

Imagebutton.Name = "some_name_here";

然后你可以用 FindControl("some_name_here") 找到它或者至少引用它

【讨论】:

  • 谢谢。我再次编辑了我的问题。现在它有了一个名称。但是我在我的问题中解释了一个新错误。
【解决方案2】:

ImageButton 创建一个在页面内唯一的 ID,并将触发器从后面的代码添加到更新面板:

    // There are many possible strategies for generating IDs unique within the page. 
    //For example, based on an index corresponding to the current file record.
    var fileIdx = 0;
    foreach (string strfile in Directory.GetFiles(Server.MapPath("~/images")))
    {
        fileIdx++;
        ImageButton imageButton = new ImageButton(){ ID = "imageBtn" + fileIdx };
        FileInfo fi = new FileInfo(strfile);
        imageButton.ImageUrl = "~/images/" + fi.Name;
        imageButton.Height = Unit.Pixel(100);
        imageButton.Style.Add("padding", "5px");
        imageButton.Width = Unit.Pixel(100);
        imageButton.Click += new ImageClickEventHandler(imageButton_Click);
        AMSPanel1.Controls.Add(imageButton);
        AMSPanel1.Height=Unit.Pixel(860);

        UpdatePanel1.Triggers.Add(new AsyncPostBackTrigger { 
                                    ControlID = imageButton.ID
                                  });
    }

【讨论】:

  • 您好,感谢您的意见。我之前的问题可能误导了你。我编辑了我的问题以便更好地理解。我实际上是在尝试在 aspx 页面中创建 asyncpostback 触发器。
  • 我实际上使用了您在我的图像按钮中添加 id 的想法。我现在有一个新错误,我在我的问题中解释了。
  • 您可以为每个ImageButton 生成一个唯一的ID,并从后面的代码中注册触发器。查看更新的答案。
  • 好主意。虽然它找不到事件。错误说......“在 UpdatePanel 'UpdatePanel1' 中的触发器的关联控件 'imageBtn1' 上找不到名为 'imageButton_Click' 的事件。”你认为这是访问修饰符阻止它找到它吗?
  • 还根据您的想法和新错误更新了我的问题。
【解决方案3】:

我想帮忙,所以请多多包涵。

  1. “Panel”没有 DataSource 属性,对比 ListView 有。
  2. 当您单击 imageButton 时,页面会刷新(因为它是“输入类型=图像”),然后“面板”会处理 imageButtons,因此即使您的 .cs 中有 imageButtons_Click,也会包含 imageButtons_Click,我有疑问那个aspx不能调用它。
  3. 我想知道您在项目中的真正目标,因为我对当前主题有 2 条建议。
    1. 面向事件的服务器页面(纯 asp.net): 和我的 .cs
    2. 或 jquery / javascript(没有 ajax 更新面板和 imageButton_Click 服务器代码隐藏)。首先只创建一个列表视图。 然后导入一个 jquery 库,并执行如下所示的一些脚本:

*注意:请注意我是如何使用 CssClass="img-btn" 而不是 ID="imgBtn" 从 javascript 调用“点击”的?仅仅是因为在 HTML 中呈现时 ID 会发生变化,请参见下图: 这使得属性“class”比属性“ID”更容易访问。

希望这些有所帮助。干杯!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-04
    • 1970-01-01
    • 1970-01-01
    • 2020-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多