I have a usercontrol as given below.
public partial class lcont : System.Web.UI.UserControl
{
public delegate void mydel(string str);
protected void Page_Load(object sender, EventArgs e)
{
}
public event mydel myevent;
public void butt_click(object sender, EventArgs e)
{
if (myevent != null)
{
myevent(tv.SelectedNode.Value);
}
}
}
And a test.aspx which has used the usercontrol of the above.
<body>
<form id="form1" runat="server">
<uc1:lcont ID="lcont1" runat="server" />
<div>
<asp:Label ID ='lbl' runat="server" Text ="lbl" />
</div>
</form>
</body>
Its codebehind is:
protected void Page_Load(object sender, EventArgs e)
{
lcont1.myevent += delegate(string st)
{
lbl.Text = st;
};
}
As you can see here i am able to call "lcont1" which is the id of usercontrol in the codebehind of aspx file.
Now the question is: I want to do the same thing but this time with the aspx file that hasn't directly implemented the usercontrol but has inherited the master page which has implemented the usercontrol.
IN other word, the usercontrol is implemented in the master page and the master page is implemented by the aspx page.Now i want to access the usercontrol id in the aspx page just like the above example.Please Help!
The way I would do it is this...
Create a public function/property in your MasterPage
that exposes the UserControl
public lcont GetLcont1()
{
return lcont1;
}
Then in your .aspx
you can cast the MasterPage
MyMasterPage myMaster = (MyMasterPage)Page.Master;
Now you should be able to get your UserControl
and do what you need with it
lcont lcont1 = myMaster.GetLcont1();
lcont1.doSomething();
Update
As the OP requires spoon-feeding, here is the code-behind for the .aspx
page...
protected void Page_Load(object sender, EventArgs e)
{
// Note: replace "MyMasterPage" with the name of your master page class
MyMasterPage myMaster = (MyMasterPage)Page.Master;
lcont lcont1 = myMaster.GetLcont1();
lcont1.myevent += delegate(string st)
...
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.