3

I am finding some serious problems in accessing a button placed in a repeater from code behind.

This is the repeater code:

  <asp:Repeater ID="Repeater1" runat="server">

                    <HeaderTemplate></HeaderTemplate>
                    <ItemTemplate>
                        <table>
                            <tr>
                                <td>Username:</td>
                                <td> <%# Eval("UserName") %></td>
                            </tr>
                            <tr>
                                <td>Date:</td>
                                <td><%# Eval("CommentTime") %></td>
                            </tr>
                            <tr>
                                <td>Comment:</td>
                                <td><%# Eval("Comment") %></td>
                            </tr>
                            <tr>
                                <td>
                                    <asp:Button ID="btnDeleteComment" runat="server" Text="Delete" /></td>
                            </tr>
                        </table>
                        <br />
                    </ItemTemplate>

                </asp:Repeater>

And This is the code Behind placed int the page load:

  Button btn = new Button();
                    btn = (Button)Repeater1.FindControl("btnDeleteComment");
                    btn.Visible = false;

Am I missing something?

Thanks

2
  • can u tell why are u doing button visible false? Commented Apr 30, 2011 at 10:46
  • Because i want that only the author of that comment can delete it! In fact the visibility of the button is setted in a if statement that cheks the username Commented Apr 30, 2011 at 10:51

3 Answers 3

3

I am sure, I've solved your problem as to why you are getting the object reference not set.

As you have this conditional statement, if (e.Item.ItemType == ListItemType.Item), when its first iterates it will be a header item type. Since your your button is in the item template, that's doesn't exist in the Header template.

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e) 
{ 
  if (e.Item.ItemType == ListItemType.Item)
    {
     Button btn = new Button(); 
     btn = (Button)e.Item.FindControl("btnDeleteComment"); 
     btn.Visible = false; 
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can't find the control like this. What you need to do is attach an event to repeater "item data bound event" and in that event handler do:

(Button)e.Item.FindControl("btnDeleteComment");

1 Comment

If I change code in this way the error object reference not set appears. protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e) { Button btn = new Button(); btn = (Button)e.Item.FindControl("btnDeleteComment"); btn.Visible = false; }
0

on .aspx

    <asp:Button ID="btnDeleteComment" runat="server" Text="Delete" Visible='<# IsAuthor?"true":"false" >' />

in code behind

//global  scope  
public bool IsAuthor=false;

//in pageload event

IsAuthor= GetIsAuthor();

1 Comment

Ok i will try this method and I will let you know

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.