ASP.NET: 我正在尝试设置一个标签来显示图像按钮,但它没有显示任何页面,我该怎么做?

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?

我正在尝试按照以下代码将图像按钮设置为标签,从而将其添加到 gridview,但它没有显示在页面上。你能告诉我哪里出错了吗?

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

你不能这样添加。构建图像按钮并将其添加到您的 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);

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

首先我在我的网格视图中添加了一个模板字段:

 <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 事件来执行操作