Search code examples
c#asp.netrepeater

How to show data of nested repeaters in page load event?


I am trying to create a comment reply box where i have used two repeaters one for the comments and second one for the replies. Both the repeaters work fine in displaying the data but the main problem is when i submit the reply to my comments, the reply doesn't show just after clicking the submit button. When I load the page again then the reply is shown on the page.

Below is my aspx code

<div>

    <asp:Repeater runat="server" ID="repAnswer" EnableViewState="True" OnItemDataBound="OnItemDataBound">
        <ItemTemplate>
            <h6>Answer</h6>
            <p><%# Eval("Answer") %></p>
            <asp:Label runat="server" ID="lblAnsId" Text='<%# Eval("AnsId")%>' ></asp:Label>
            <a class="link" id='lnkReplyParent<%#Eval("AnsId") %>' href="javascript:void(0)" onclick="showReply(<%#Eval("AnsId") %>);return false;">Reply</a>   
            <a class="link" id="lnkCancle" href="javascript:void(0)" onclick="closeReply(<%#Eval("AnsId") %>);return false;">Cancle</a>  
              <div id='divReply<%#Eval("AnsId") %>' style="display:none;">  
                 <asp:TextBox ID="textCommentReplyParent" CssClass="input-group" runat="server" Width="300px" TextMode="MultiLine" EnableViewState="True"></asp:TextBox>  
                 <br />  
                 <asp:Button ID="btnReplyParent" runat="server" Text="Reply" OnClick="btnReply_Click" /></div>

            <div>
                <asp:Repeater runat="server" ID="repRply">
                    <ItemTemplate>
                      <p><%# Eval("reply") %></p>
                    </ItemTemplate>
                </asp:Repeater>
            </div>
        </ItemTemplate>
    </asp:Repeater>

</div>

Below is my cs page code

protected void btnReply_Click(object sender, EventArgs e)
{
    RepeaterItem item = (sender as Button).NamingContainer as RepeaterItem;
    rplycmnt =(item.FindControl("textCommentReplyParent") as TextBox).Text.Trim();
    answerid=Convert.ToInt32((item.FindControl("lblAnsId") as Label).Text);
    OnlineSubjects onlinesub = new OnlineSubjects()
    {
        reply = rplycmnt,
        AnsId = answerid
    };
    onlinesub.addReply();
}

//getting replies
protected void OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        answerid = Convert.ToInt32((e.Item.FindControl("lblAnsId") as Label).Text);
        Repeater repRply = e.Item.FindControl("repRply") as Repeater;
        OnlineSubjects onlinerply = new OnlineSubjects()
        {
            AnsId = answerid
        };
        onlinerply.showReply();
        repRply.DataSource = onlinerply.showReply();
        repRply.DataBind();
    }
}

Code of Comments... When this comment is posted then we can give reply to this comments

protected void btnAnswer_Click(object sender, EventArgs e)
    {
        OnlineSubjects onlinesub = new OnlineSubjects()
        {  
            Answer = txtAddAnswer.Text.Trim(),
            QuesId=Id
        };
        onlinesub.addAnswer();
        txtAddAnswer.Text = "";
        GetAnswer();
    }

Code of displaying the comments

private void GetAnswer()
    {
        OnlineSubjects onlineans = new OnlineSubjects()
        {
            QuesId = Id
        };
        onlineans.showAnswer();
        repAnswer.DataSource = onlineans.showAnswer();
        repAnswer.DataBind();
    }

On Page Load

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            GetAnswer();       
        }  
    }

Solution

  • You need to call getanswer after reply

    protected void btnReply_Click(object sender, EventArgs e)
    {
        RepeaterItem item = (sender as Button).NamingContainer as RepeaterItem;
        rplycmnt =(item.FindControl("textCommentReplyParent") as TextBox).Text.Trim();
        answerid=Convert.ToInt32((item.FindControl("lblAnsId") as Label).Text);
        OnlineSubjects onlinesub = new OnlineSubjects()
        {
            reply = rplycmnt,
            AnsId = answerid
        };
        onlinesub.addReply();
        GetAnswer(); 
    }
    

    BTW, you are calling answer twice, which is waste of resources. Change it as below. Also change method name to bindanswers as it binds data

    private void BindAnswers()
        {
            OnlineSubjects onlineans = new OnlineSubjects()
            {
                QuesId = Id
            };
            var answers = onlineans.showAnswer();
            repAnswer.DataSource = answers;
            repAnswer.DataBind();
        }