Matthew
Matthew

Reputation: 4607

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"".

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.

Here is some code in 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?

Upvotes: 0

Views: 2732

Answers (3)

usr-local-ΕΨΗΕΛΩΝ
usr-local-ΕΨΗΕΛΩΝ

Reputation: 26874

In general you cannot straightly retrieve a plain-old HTML button because ASP.NET considers that as part of the text markup. Fortunately, you already added runat="server" that makes your button a server control.

The easiest way is to use an HtmlButton control. But in your markup you need the id attribute

<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");

Upvotes: 0

Nick Bork
Nick Bork

Reputation: 4841

That button is a HTML control and will be of type 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;

Upvotes: 2

user1921
user1921

Reputation:

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.

Upvotes: 0

Related Questions