4

I have two nested repeaters. In the nested one's footer I have text box and file upload controls. I was able to get an instance of the file upload without any problem, but the instance of the text box is null, though both are placed in the footer.

Here is the aspx part representing the footer of the inner repeater:

<FooterTemplate>
            <tr class="add_comment">
                <td>Add comment </td>

            </tr>
            <tr>
                <td>
                    <asp:TextBox runat="server" Columns="20" Rows="3" ID="comment_txt" TextMode="MultiLine" Width="60%" CssClass="new_comment" ViewStateMode="Inherit"></asp:TextBox>

                </td>

             </tr>

           <tr class="add_comment">
                <td>
                            <asp:FileUpload ID="uploadImageBtn" runat="server" Text="Add image" OnClick="uploadImage" CssClass="comment_buttons" />

                            <asp:Button ID="comment_btn" runat="server" OnClick="submitComment" Text="Comment" CssClass="comment_buttons" />
                </td>
       </tr>

         </table>
     </FooterTemplate>

This is the C# code where I'm trying to access the controls:

protected void commentsRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if ((e.Item.ItemType == ListItemType.Footer ))
            {
                Repeater childRepeater = (Repeater)sender;
                TextBox commentTextBox = (TextBox)e.Item.FindControl("comment_txt");
                String postText = commentTextBox.Text.ToString(); 
                FileUpload upFile = (FileUpload)e.Item.FindControl("uploadImageBtn");
            }
        }

When running the page I get this error,

Object reference not set to an instance of an object 

Which is caused by this line:

String postText = commentTextBox.Text.ToString(); 

I tried to remove the text box code and retrieve only the upload file and it worked very well. The problem is in accessing the text box.

Edit: The accessed text of the text box and the instance of upload button should be accessed in an onclick event handler of a button in the same page. Thus, I have defined both globally, assigned them the values while executing some nested repeater event of the Repeater like ItemDataBound or the event suggested by Adrian Iftode, which is ItemCreated. Then, in the onclick of the button I used them assuming that they have values since the nested repeater event should be fired before onclick of the button. Upload file instance is retrieved successfully, but the text box is always null.

Global variables declaration:

  TextBox commentTextBox;
    FileUpload upFile;
    Repeater childRepeater;
    String postText; 

Code inside nested repeater event:

 protected void commentsRepeater_ItemCreated(object sender, RepeaterItemEventArgs e)
        {
            if ((e.Item.ItemType == ListItemType.Footer))
            {
                 childRepeater = (Repeater)sender;
                 commentTextBox = (TextBox)(e.Item.FindControl("comment_txt"));
                 postText = commentTextBox.Text.ToString();
                upFile = (FileUpload)e.Item.FindControl("uploadImageBtn");
            }
        }

Code inside onclick:

protected void submitComment(object sender, EventArgs e)
        {


            Boolean insert = true;
            if (upFile.HasFile || !String.IsNullOrEmpty(postText))
            {
                   //some code. 
              }

The above if statement is only executed if upFile has file, postText is always seen as null.

Can anyone please help me, what is causing this error?

Thank you.

Dania
  • 1,478
  • 2
  • 22
  • 51
  • The cast is the reason. Would (TextBox)(e.Item.FindControl("comment_txt")); help? – Sami Dec 08 '15 at 19:04
  • @Sami Thank you, but I didn't get your point, I'm using this casting to get an instance of the text box, then retrieve its text. Would you please elaborate more on how the cast is causing the problem, and how to solve it? Thanks. – Dania Dec 08 '15 at 19:16
  • Just a guess, but since you get a null reference, it's likely that the cast fails and therefore returns null. In your original code (I suspect) the compiler is trying to cast the "e" into textbox, which can't be done. I added the parenthesis so the result of the e.Item.FindControl would be what the compiler should try to cast. – Sami Dec 08 '15 at 19:20
  • @Sami, thanks I tried what you suggested, but I'm still getting the same error. Please let me know if there is any other possible solution. – Dania Dec 08 '15 at 19:24

1 Answers1

1

ItemDataBound is not the right event to handle in this situation, because the header and footer templates are not instantiated in for the repeater items.

The proper event is ItemCreated

protected void rp_ItemCreated(Object sender, RepeaterItemEventArgs e)
{
            if (e.Item.ItemType == ListItemType.Footer)
            {    
              e.Item.FindControl(ctrl);
            }
            if (e.Item.ItemType == ListItemType.Header)
            {
              e.Item.FindControl(ctrl);
            }
}
Adrian Iftode
  • 14,740
  • 3
  • 41
  • 71
  • Thank you, I tried this, it works well only in local scope. when I define the String postText globally, and retrieve its value from ItemCreated, the global string value stays null. I'm checking the string value in a button onclick method. Can you please tell me why this is happening? Thanks a lot. – Dania Dec 08 '15 at 20:24
  • postText? What is this? – Adrian Iftode Dec 08 '15 at 21:00
  • @ Adrian Iftode, it is a string to store the text value entered in the text box. I want to get the text in the text box when the button "comment_btn" is clicked. Since the text box is inside a nested repeater, I won't be able to get an instance of it unless in one of the nested repeater events like ItemCreated, at the same time this text should be accessible from inside onclick. So, I defined postText globally, and assigned to it the value of text in the repeater event. I think that the repeater event must be executed before onclick. Can you please help me? What is causing this error? Thanks – Dania Dec 09 '15 at 04:52
  • I think better for you is to build a very simple example which includes the nested repeater too so I can take a better look. Also maybe there is a simpler way to do what you want. – Adrian Iftode Dec 09 '15 at 06:21
  • thanks a lot, I updated the question please check it and let me know how can I solve this problem. Many thanks. – Dania Dec 09 '15 at 07:26