Reputation: 1688
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?
Upvotes: 5
Views: 1615
Reputation: 15683
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);
}
}
Upvotes: 2