C# 如何使用c将ImageButton添加到标签#

C# 如何使用c将ImageButton添加到标签#,c#,html,asp.net,visual-studio-2010,imagebutton,C#,Html,Asp.net,Visual Studio 2010,Imagebutton,我正试图使用C#将ImageButton添加到标签中,但没有成功。。。。有人能帮我吗 谢谢 这就是我所做的-(删除是标签的名称) 我认为不应该将ImageButton控件添加到Label控件。这看起来不像是标签控件的语义用法(应该用来显示文本) 但是,您通常希望将子控件添加到要将其添加到的任何控件的.controls集合中,而不是页面或表单的.controls集合中。像这样: Delete.Controls.Add(img); 您可能应该使用以下内容,而不是标签: <asp:Place

我正试图使用C#将ImageButton添加到标签中,但没有成功。。。。有人能帮我吗

谢谢

这就是我所做的-(删除是标签的名称)


我认为不应该将ImageButton控件添加到Label控件。这看起来不像是标签控件的语义用法(应该用来显示文本)

但是,您通常希望将子控件添加到要将其添加到的任何控件的
.controls
集合中,而不是页面或表单的
.controls
集合中。像这样:

Delete.Controls.Add(img);

您可能应该使用以下内容,而不是标签:

<asp:PlaceHolder ID="myControlHolster" runat="server" />

如果Delete是标签的名称(我建议将其重命名为DeleteLbl或类似名称),那么您将转到标签,获取对页面的引用,然后是页面上的表单,然后将ImageButton添加到该标签。那太复杂了。相反,我认为您希望将其直接添加到标签的控件中

Delete.Controls.Add(img);

您可以使用以下代码,其中2.jpg是您的按钮图像:

<asp:button id="myButton" style="background-image:url('2.jpg'); background-color:red; cursor:hand;" Runat="server" Height="25px" BorderStyText="ButtonText" Width="200px" />

为什么要将其添加到标签中?
Delete.Controls.Add(img);
<asp:button id="myButton" style="background-image:url('2.jpg'); background-color:red; cursor:hand;" Runat="server" Height="25px" BorderStyText="ButtonText" Width="200px" />