0
votes

I have a nested asp repeater which is displaying all my items correctly. I have another div within the ItemTemplate of each repeater which submits content to my code behind.

<asp:Repeater runat="server" id="repeaterNote" OnItemDataBound="repeaterNote_ItemDataBound">
        <ItemTemplate>
            <div id="note" class="staff"><p><%# Eval("Content") %></p><ul>
                <li><%# Eval("AddedDate") %></li><li>20:29</li>
                <li><%# Eval("AddedByName") %></li><li>
                    <a href="#" onclick="showAddComment(addComment<%# Eval("NoteID") %>)" class="but">Add comment</a></li></ul>
                                <div id="addComment<%# Eval("NoteID") %>" class="staff" style="display:none">
                                <asp:TextBox ID="notecommenttext" runat="server" rows="4"> </asp:TextBox>
                                <asp:Button ID="Button" runat="server" Text="Add" CommandArgument='<%# Eval("NoteID") %>' OnCommand="Add_Comment" /> 
                                </div>
                            </div>

When I submit the button I want to retrieve the content within the textbook "notecommenttext". There doesn't seem to be an easy way to do this. I have tried to set the ID of the textbook to the unique "NoteID" but it appears that cannot be done.

    string uniqueNoteID = e.CommandArgument.ToString();

    int parent = int.Parse(uniqueNoteID);

    TextBox txtName = new TextBox();

    foreach(RepeaterItem noteItem in repeaterNote.Items)
    {
        if (noteItem.ItemType == ListItemType.Item || noteItem.ItemType == ListItemType.AlternatingItem)
        {
            Notes rin = (Notes)noteItem.DataItem;
            if (rin.RelativeNoteId == parent)
            {
                txtName = (TextBox)noteItem.FindControl("notecommenttext");
            }
        }
    }

The snippet above is from the button callback "Add_Comment", repeaterNote is my top level Repeater id. It seems to have the correct number of RepeaterItems but the DataItem property is always null. I have added an inner foreach loop to iterate over my nested repeater. Again, this correctly identifies only one of the top level repeaters has a nested repeater element, but the DataItem is still null.

Any suggestions on how I can access the content from within the textbook of each repeater?

2
typically the data is not available on the server until you rebind it. You would have to reload the data based on the id you have.ps2goat
Could you expand on this? I set the datasource and call databank in page_load. Should I call DataBind again in my button call back?Tom smith
Do you set it all the time in page_load, or only if it is not a postback? You would have to set the data and call DataBind everytime for the data to be available from that property.ps2goat
Currently it is only set when it is not a post back. I just tried removing that check and It still comes back with DataItem as null unfortunately.Tom smith
@Tomsmith did you try what i proposed in my answer and my comment ?Med.Amine.Touil

2 Answers

0
votes

I think your problem is that you are doing the Binding mecanism in your Page_Load() method without testing if (!IsPostback) That's why you dont find the result you want. Don't forget to set EnableViewState="False" So you should move your Binding mecanism in Page_LoadComplete() method if you are in aspx page or add in your page load

if(!IsPostback)
{
  //your binding mecanism
}
0
votes

I think instead of setting OnCommand in your button, you should set OnItemCommand in your repeater to your event handler and just set the CommandName property in your button to an easily identifiable string. Then in the repeater's OnItemCommand event handler, you do a Select Case on e.CommandName, and if it's that button's CommandName that you set, you can use e.Item.FindControl("notecommenttext") to get your textbox control. Then get its content. When a control is in a repeater its ID gets a bunch of stuff appended to it so it is hard to find outside of a repeater event like ItemCommand.