There is a aspx page which makes use of UserControl as follows.
Page1.aspx.cs
public class Page1
{
public string Code {get;set;}
....
}
In the Page1.aspx a user control called "UsrControl"
<@Register TagPrefix="Usr" NameSpace="UsrControl">
I have the following code in UsrControl.
public class UsrControl : Label
{
private Page1 _page1;
protected override void OnInit(EventArgs e)
{
_page1 = (Page1)Page;
string test = _page1.Code;
}
.....
}
In the usercontrol , the parent page is harcoded and from there the property of parent page (Page1) is being used .
Now the thing is I have one more aspx page Page2 which has same set of properties as Page1(Code) needed to invoke "UsrControl"
How can I refactor "UsrControl" to have the capability of accepting Page1 and Page2 and execute the code ? Should reflection be used?
2
Answers
It would be easier to turn it around. Set the
Code
value from the page to the UserControl instead of getting it from the Page.So in your UserControl add a property
Code
Then you can set that property from the aspx page of the parent.
And then call
DataBind();
inPage_Load
of the parent page.You can also add the UserControl programatically on the parent page and set
Code
from there.Just make an interface that shapes your data provider
and implement this interface in both your pages
After that, refactor your user control