【问题标题】:ASP.NET: I'm trying to set a label to show an image button but it isn't showing no the page, How do i do this?ASP.NET:我正在尝试设置标签以显示图像按钮,但它没有显示页面,我该怎么做?
【发布时间】:2019-01-29 08:34:27
【问题描述】:

我正在尝试通过按照以下代码将图像按钮设置为标签来将图像按钮添加到网格视图,但它没有显示在页面上。请问你能告诉我哪里出错了吗?

 lbl3.Text = "<asp:ImageButton id=\"SuperSButton\" runat=\"server\" Image src=\"Images/Prop.png\" OnClick=\"superSession_Click\"/>";

【问题讨论】:

    标签: c# asp.net imagebutton


    【解决方案1】:

    你不能那样添加它。构建图像按钮并将其添加到您的 gridview。

    var img = new ImageButton();
    img.ID = "SuperSButton";
    img.ImageUrl = "images/Prop.png";
    img.Click += new ImageClickEventHandler(superSession_Click);
    img.Width = 48;
    img.Height = 38;
    
    //then add it somewhere in your grid
    GridView1.FooterRow.Cells[0].Controls.Add(btn);
    

    【讨论】:

    • 我认为OnClick是一种方法,你应该改用Click事件(参考ImageButton reference
    • 这对我来说不太奏效,我想将按钮添加到列中,但不是因为它并不总是可见。但是指出我这样做的方式是错误的非常有用。我已经解决了,我会添加我的解决方案。
    • 很高兴你让它工作安迪!我不太确定你想要它在网格上的哪个位置,所以对于我的示例,只需将它放在页脚的一个单元格中。如果您需要根据条件将按钮设置为不可见,那么您将需要遍历 gridview 的行集合以找到按钮,或者更好地使用您在解决方案中提到的 findcontrol 函数。找到它后,您可以更改按钮上所需的任何属性
    • @JamesBlackburn 这很公平,我没有解释其余部分,因为我认为我认为其余部分是正确的,结果我没有!感谢您的帮助。
    【解决方案2】:

    James 指出了正确的方向,我用不同的方法让它工作。

    首先我在我的 gridview 中添加了一个模板字段:

     <asp:TemplateField>
                        <ItemTemplate>
                            <asp:ImageButton ID="SuperSButton" runat="server" ImageUrl="images/Prop.png" OnClick="superSession_Click"/>
                        </ItemTemplate>
                    </asp:TemplateField>
    

    然后我在后面的代码中添加了这个:

     ImageButton LB1 = (ImageButton)e.Row.FindControl("SuperSButton");
            LB1.Visible = false;
    
            if (line.SuperSessionFlag)
            {
                if(line.SuperSessionIndicator == "1" || line.ErrorType =="S" )
                {
    
                    LB1.CommandArgument = line.PartNumber;
                    LB1.Visible = true;
                }
    

    最后,我添加了一个 rowcommand 而不是 onclick 事件来执行操作

    【讨论】:

      猜你喜欢
      • 2020-07-06
      • 2019-04-06
      • 2016-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多