Click here to Skip to main content
15,886,016 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have created 3 user controls in three difference directories(Comment,Comment1,Comment2) like
Comment.ascx
Comment1.ascx
Comment2.ascx


Reference those as per give below:

Comment.ascx > Comment1.ascx
Comment1.ascx > Comment2.ascx
Comment2.ascx > Comment.ascx


And getting error like:
Circular file references are not allowed.


As per some of forum I have set
batch="false"
in web config file but still I am getting error.
Is there any solution for that ?
Posted

The problem with circular references is that the compiler can not decide which component compile first...
So the only option is to remove such circular dependencies by re-design your application...
 
Share this answer
 
Comments
Jaimin H. Soni 30-Mar-15 8:37am    
I have referenced first user control in aspx page.
Can you let me know which dependencies I have to remove ? or is ther any other solution for that without re-design application ?
Kornfeld Eliyahu Peter 30-Mar-15 8:40am    
The question you have to ask is why did you set up those references at the first place...
That will give you the answer of which remove...
I can not tell you an exact answer as I do not know what is your goal, so you have to see by yourself...
And advice: remove all the references between the controls and see if it works...Add only the missing references...
Jaimin H. Soni 30-Mar-15 8:49am    
I want to display data hierarchy using three or two user control.
Kornfeld Eliyahu Peter 30-Mar-15 8:52am    
It does not mean that you have to have references between the controls you use to display the data. It even not necessary you have different control for every hierarchy - see tree view control that uses the very same control to display endless levels of hierarchical data...
Jaimin H. Soni 30-Mar-15 8:51am    
Or I want to reuse of one user control in same page. is it possible ?
1. As I know is not possible to have circular references.

2. The solution is to redesign you controls to avoid this circular references.
 
Share this answer
 
Comments
Jaimin H. Soni 30-Mar-15 9:05am    
can you see my conversation with peter ?
U have any idea about that ?
Sergey Alexandrovich Kryukov 1-Apr-15 12:13pm    
Good point, 5ed.
—SA
Raul Iloc 2-Apr-15 1:08am    
Thank you for your vote!
You can do this using recursive code. The actual solution will change depending on what your data looks like and how you access it, but this is the gist. All files are in the root of the site.

CommentObject.cs

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MyNamespace
{
    public class CommentObject
    {
        public int ID { get; set; }
        public int ParentID { get; set; }
        public string Author { get; set; }
        public string Body { get; set; }
        public List<CommentObject> Replies { get; set; }
    }
}


Comment.ascx

XML
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Comment.ascx.cs" Inherits="MyNamespace.Comment" %>
<div style="border: 1px dashed #aaaaaa; margin-left:30px;">
    <h2><asp:Literal ID="LiteralAuthor" runat="server" /></h2>

    <div><asp:Literal ID="LiteralBody" runat="server" /></div>

    <asp:PlaceHolder ID="PlaceReplies" runat="server" />
</div>


Comment.ascx.cs

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MyNamespace
{
    public partial class Comment : System.Web.UI.UserControl
    {
        public CommentObject CommentObject { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            LiteralAuthor.Text = this.CommentObject.Author;
            LiteralBody.Text = this.CommentObject.Body;
        }
    }
}


TestPage.aspx

XML
<form id="form1" runat="server">

<asp:PlaceHolder ID="PlaceComments" runat="server" />

</form>


TestPage.aspx.cs

C#
protected void Page_Load(object sender, EventArgs e)
{
    // build up a tree of comments
    // how you do this depends on your data structure, you might
    // even want to load data on the fly in the ShowComments method instead
    List<CommentObject> comments = new List<CommentObject>();

    comments.Add(new CommentObject { ID = 1, Author = "Author A", Body = "Comment 1" });
    comments.Add(new CommentObject { ID = 2, Author = "Author B", Body = "Comment 2" });
    comments.Add(new CommentObject { ID = 3, Author = "Author C", Body = "Comment 3" });
    comments.Add(new CommentObject { ID = 4, Author = "Author D", Body = "Comment 4" });

    // add replies to comment 2
    CommentObject comment = comments.Single(c => c.ID == 2);
    comment.Replies = new List<CommentObject>();
    comment.Replies.Add(new CommentObject { ID = 5, Author = "Author A", Body = "Comment 5" });
    comment.Replies.Add(new CommentObject { ID = 6, Author = "Author C", Body = "Comment 6" });
    comment.Replies.Add(new CommentObject { ID = 7, Author = "Author D", Body = "Comment 7" });

    // add replies to comment 6
    comment = comment.Replies.Single(c => c.ID == 6);
    comment.Replies = new List<CommentObject>();
    comment.Replies.Add(new CommentObject { ID = 8, Author = "Author A", Body = "Comment 8" });
    comment.Replies.Add(new CommentObject { ID = 9, Author = "Author F", Body = "Comment 9" });

    ShowComments(PlaceComments, comments);
}

private void ShowComments(PlaceHolder placeHolder, List<CommentObject> comments)
{
    foreach(CommentObject comment in comments)
    {
        Comment ctlComment = (Comment) LoadControl("~/Comment.ascx");
        ctlComment.CommentObject = comment;

        placeHolder.Controls.Add(ctlComment);

        if (comment.Replies != null && comment.Replies.Any())
        {
            ShowComments((PlaceHolder)ctlComment.FindControl("PlaceReplies"), comment.Replies);
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900