[英]ASP.NET C# - Retrieving HTML Button from Repeater
I have a repeater containing, amongst others, two buttons. 我有一个转发器,其中包含两个按钮。 One of the buttons is an ASP.NET button whilst the other is of the type "input type="button"".
其中一个按钮是ASP.NET按钮,而另一个按钮是“input type =”按钮“”。
Now, in my code-behind, I want to retrieve both buttons from the repeater to either hide them or show them. 现在,在我的代码隐藏中,我想从转发器中检索两个按钮,以隐藏它们或显示它们。 I have successfully hidden the ASP.NET button however I do not know how to retrieve the other button.
我已经成功隐藏了ASP.NET按钮但是我不知道如何检索其他按钮。
Here is some code in ASP.NET: 这是ASP.NET中的一些代码:
<input type="button" name="ButtonEditUpdate" runat="server" value="Edit Update" class="ButtonEditUpdate" />
<asp:Button ID="ButtonDeleteUpdate" CssClass="ButtonDeleteUpdate" CommandName="Delete" runat="server" Text="Delete Update" />
Here is the code-behind: 这是代码隐藏:
protected void RepeaterUpdates_ItemBinding(object source, RepeaterItemEventArgs e)
{
RepeaterItem item = e.Item;
TextBox Update_ID = (TextBox)item.FindControl("TextBoxUpdateID_Repeater");
//Button Edit_Update = (Button)item.FindControl("ButtonEditUpdate");
Button Delete_Update = (Button)item.FindControl("ButtonDeleteUpdate");
if (Social_ID == String.Empty)
{
//Edit_Update.Visible = false;
Delete_Update.Visible = false;
}
}
How can I retrieve the HTML button and hide it since it is NOT an ASP.NET button? 如何检索HTML按钮并隐藏它,因为它不是ASP.NET按钮?
That button is a HTML control and will be of type System.Web.UI.HtmlControls.HtmlButton
该按钮是一个HTML控件,其类型为
System.Web.UI.HtmlControls.HtmlButton
System.Web.UI.HtmlControls.HtmlButton button = item.FindControl("ButtonEditUpdate") as System.Web.UI.HtmlControls.HtmlButton;
if(button!=null)
button.Visible = false;
If you just want to set the visibility you shouldn't need to cast it. 如果您只想设置可见性,则不需要进行投射。
var myButton = e.Item.FindControl("ButtonEditUpdate");
if(myButton != null)
myButton.Visible = false;
EDIT: you should give your button an ID. 编辑:你应该给你的按钮一个ID。
In general you cannot straightly retrieve a plain-old HTML button because ASP.NET considers that as part of the text markup. 通常,您无法直接检索普通的HTML按钮,因为ASP.NET将其视为文本标记的一部分。 Fortunately, you already added
runat="server"
that makes your button a server control. 幸运的是,您已经添加了
runat="server"
,使您的按钮成为服务器控件。
The easiest way is to use an HtmlButton
control. 最简单的方法是使用
HtmlButton
控件。 But in your markup you need the id
attribute 但是在你的标记中你需要
id
属性
<input type="button" name="ButtonEditUpdate" runat="server" value="Edit Update" class="ButtonEditUpdate" id="ButtonEditUpdate" />
Then in the code behind 然后在代码背后
//Button Edit_Update = (Button)item.FindControl("ButtonEditUpdate");
HtmlButton Delete_Update = (HtmlButton)item.FindControl("ButtonEditUpdate");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.