0

I'm creating two version of the same page. So I want to create a common ancestor but I'll need to access some Controls from that code so the best way to do that would be to have the set of common Controls inside a shared designer.cs file but ASP.Net will recreate those for each page (due to axiomatization) as soon as it reach the asp tags with the same ID on the aspx file. Is there a way to do this anyway?

Exmple (in code look alike):

common.cs

partial class CommonClass [...]
    Function X
        Do something with MyRepeater

common.designer.cs (or other file name)

partial class CommonClass
    protected global::System.Web.UI.WebControls.Repeater MyRepeater;

page1.cs

Page1 : CommonClass

page1.aspx

[...]
<asp:Repeater ID="MyRepeater" runat="server" [...]

page2.cs

Page2 : CommonClass

page2.aspx

[...]
<asp:Repeater ID="MyRepeater" runat="server" [...]

EDIT: page1 and page2 allow the user to see a table and change it's content (there also are several other controls). page1 fills the table using a set of objects. page2 fills the table using a set of completely different objects. Yet the way page1 and page2 fill the data is the same (3 intermediate table are constructed from the set of objects) and it's quite complex. Since it the same (and quite complex) I'd liking the filling logic to be in a common file, but since that filling logic relies on some repeaters, I need those repeater to bedefined in the common page as well.

Serge
  • 6,554
  • 5
  • 30
  • 56
  • 3
    Is there some reason why you're not using a UserControl to bring together the common elements? – Luke Baughan Apr 25 '13 at 13:14
  • Yes, because that would be complex, ugly and inappropriate. – Serge Apr 25 '13 at 13:25
  • User controls are an easy, nice and appropriate solution that makes it less complex. Another solution is to make one page with additional logic to display what is needed depending on some values. – L-Four Apr 25 '13 at 13:29
  • Making a user control filing the whole aspx page and which interact with several common then completely different business logic and with several common then completely different web controls in page1 than in page2 is really what you guys think is best?!! – Serge Apr 25 '13 at 13:36
  • I don't get where you guys got the idea a UserControl's content was more flexible than a page's one. – Serge Apr 25 '13 at 13:38
  • 1
    What is your business logic doing in the UI? It should be independent of your controls. Separate your concerns. – L-Four Apr 25 '13 at 13:38
  • What are you talking about, my BL is in the code behind. – Serge Apr 25 '13 at 13:43
  • Perhaps we might be better able to help if you elaborate on what these pages are trying to achieve? – Luke Baughan Apr 25 '13 at 13:43
  • @Serge: that's what I mean, code behind is bound to your UI and should not contain business logic, only UI logic. See also http://stackoverflow.com/questions/1638912/refactoring-to-seperate-business-logic-from-code-behind. – L-Four Apr 25 '13 at 13:47
  • Easier said than done. Anyway, here I am, asking a question for a scenario one might find lacking of some best practice. – Serge Apr 25 '13 at 13:54
  • Maybe this is an opportunity to refactor :) Anyway, because these pages are almost identical, I would create 1 page and build its contents based on criteria. – L-Four Apr 25 '13 at 14:04

1 Answers1

0

Now that I've run some new test, apparently Asp.Net is clever enough not to redefine it the partial designer class controls already defined in a parent class.

Sweet. It's working.

Serge
  • 6,554
  • 5
  • 30
  • 56