I have created user control, in that user control i have one method and I want to call this method in .aspx. I have registered this user control in aspx
For example:
Below is method in user control.
public void SetGridData()
{
}
I want to call above method in .aspx.cs file.
How can we call this method?
Somewhere in the ASPX page's code you should have a reference to the user control object. For example, if the user control is called MyUserControl then somewhere at the class level for the page (possibly in a separate partial class designer file) should be:
protected MyUserControl myUserControl1;
or something similar to that. That's the instance of the user control for the page's class. The page life cycle should instantiate it by the time Page_Load is reached, so from then on you can use that object:
myUserControl1.SetGridData();
If this is purely an example, then you can call methods in codefiles with the following syntax:
<%= SetGridData(); %>
However, just be aware of the notes I put in the comments above.
Related
I have a class which has a method, and a web page (default.aspx) with textboxes buttons etc. They both are in the same namespace.
I need to access to default.aspx' s control ID's (for example a textbox ID) from the class method. Because I need to get the values from default.aspx controls to do some checks with the database, and it needs to be done in the class method.
have any ideas?
I've a Master Page which contains a DropDownList. I've a function for binding the list in the master and it works fine.
My problem is: How will I call that Master Page function from a form, which is not the child of the above master page
See article here.
Here is something nice that comes from the new compilation model in ASP.NET 2.0. Let’s say you add a custom property to a master page code-behind file like so:
partial class otcMaster : System.Web.UI.MasterPage
{
public string FooterText {
get { return Footer.Text; }
set { Footer.Text = value; }
}
}
You can get to the master page for a web form using the inherited Master property, which returns a MasterPage reference. To get to a property defined in otcMasterPage though, you might think you need to use a cast.
((otcMaster)Master).FooterText == "foo"
Casting to a derived type is just a part of life when using frameworks and statically typed languages, but there is a better way. Use the # MasterType directive in the ASPX.
<%# MasterType VirtualPath="~/otc.master" %>
Now when ASP.NET codegens the page, it puts the following inside a partial class definition. Notice the Shadows keyword (that would be the new keyword in semicolon land [yeah, I’m experimenting with alternative languages]).
public new otc Master {
get { return (otcMaster)base.Master; }
}
The result is a strongly typed Master Page. We don’t need a cast, we can go right to the Master.FooterText property. Another way to do this is to specify a TypeName in the #MasterType directive.
Provide a public method in your MasterPage, then you need to cast the ContentPage's Master property to the appropriate type:
public void DataBindDropDowns()
{
// ...
}
Then you can call it from your ContentPages in the following way(assuming your masterpage's type is called SiteMaster:
((SiteMaster)this.Page.Master).DataBindDropDowns();
Edit:
...which is not the child of the above master page
I assume that means it's no a ContentPage of that Master, am i right?
Then it's not possible to get a reference to the master except when:
The master's method is static, what is impossible in this use case since you want to bind controls on the master
You have a reference to a page which master is of that type, but again impossible since the current HTTP-Handler is another page which does not use this master
Note that the master page actually is a child of a ContentPage and will be merged with it. It's not possible to get a reference to an object that does not exist!
From MSDN:
Note that the master page becomes a part of the content page. In
effect, the master page acts in much the same way a user control acts
— as a child of the content page and as a container within that page.
You need to refer to MasterPage property, cast to your master page type and invoke your method.
((MyMasterPage)this.Master).MyBindingFunction();
Put this in your page code (where MyMasterPage is your master page object):
MyMasterPage masterPage = (MyMasterPage) this.Master;
masterPage.MyBindDropDownListFunction(); // Replace with your public function name
If you're making it very frequently, you can create a BasePage derived from System.Web.UI.Page, and use it as the bage page for your forms.
There you can add a property of the type of your master page, that will give you acces to all public members of the Master Page.
If you master page class is Site1, you could do something like this in your BasePage.
public class BasePage : System.Web.UI.Page
{
protected Site1 Site1Master
{
get { return Master as Site1; }
}
}
Then in the pages where you need to acces the methods of the master page replace:
public partial class DefaultPage : System.Web.UI.Page
with
public partial class DefaultPage : BasePage
Then you'll have the property Site1Master available in the pages, and you can use any of its public members like this:
Site1Master.MyBindingFunction(...);
You can also add any other desired functionality in your BasePage.
NOTE: If you want to make sure that the property isn't null in the pages, you can add a check to see if the page has the Site1 master, like this:
protected Site1 Site1Master
{
get
{
if (!(Master is Site1))
throw new Exception("This page doesn's have Site1 as master page");
return Master as Site1;
}
}
For accessing the members of a Master page there's a Master property exposed on Page Content.
First you've to specify the # MasterType directive :
<%# Page masterPageFile="~/MasterPage.master"%>
<%# MasterType virtualPath="~/MasterPage.master"%>
Then in the Master Page create a Public function and in your content Page you simply call
Master.MethodNameInMaster()
For better design use EventAggregator pattern. Create your custom event and handle it in Master Page.
Basically I have a userControl.ascx with a lot of code... this user control is used in the Child.MasterPage and the Child.MasterPage inherits from a Parent.MasterPage.
In the Parent.MasterPage I have a method in the .cs file, this method accepts a string and passes it to a literal.
The problem:
I have spent hours trying to pass a string from my UserControl directly to the Parent.MasterPage.cs method with no luck. The child.MasterPage does not have a c# file, it just inherits from the parent.master
a line of code that I am using so that I can pass values to the patent.MasterPage reads like this.. but I am not able to pass to the method please help!
MasterPage myMasterPage = this.Parent.Parent.Page.Master.Master;.
Try typecasting it like ((ParentMaster)this.parent.parent).SomeMethod()
I have a.master and b.aspx .
i have some functions in my aspx page.
how to access that functions in a.master page.
thank you
Let's say, you want to call Foo from b.aspx from a.master. So first thing is that you have make the method internal (or public) and then you can use code such as below in master page is call that method.
var page = (b)this.Page;
page.Foo();
Note that b will be the code behind class name in b.aspx. Note that above code will fail if you use another page c.aspx and use the same master a with it. Generally, I will say that invoking page specific functions from master does not make sense unless functions are present in some base page class and in such case you should be casting to that base page class.
Edit: More elaborate example as requested by Asif:
Consider your content page b.aspx such as
<%# Page Language="C#" MasterPageFile="a.Master" Title="Page B" AutoEventWireup="true"
CodeBehind="b.aspx.cs" Inherits="YourProject.b" %>
And in code behind file (b.aspx.cs), you have a method Foo such as
namespace YourProject
{
public partial class b : System.Web.UI.Page
{
void Foo(string someParameter)
{
Label1.Text = someParameter
}
...
}
}
Now in code behind (a.master.cs) of a.master page
namespace YourProject
{
public partial class a : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
b contentPage = (b)this.Page;
contentPage.Foo("Hello");
}
....
}
}
Of course you can make the method in b.aspx be public to call it in a.master. However I suggest you consider your design carefully. Because it's really weird just like that you call a method of a child class from its parent class (even though it's theoretically possible). Before your modification, ask yourself:
Is it necessary to call this method in the master page? If yes, do I have a better place to put the method?
As others have said, it's possible to do this. However, it's an odd way of doing things. You are probably going to be better off doing whatever you want to do in a different way. The whole idea of a master page is that it "wraps" many kinds of content pages. What if you content page doesn't have the function you want to call?
You could make sure all your content pages have the function, but then why not just put it in the master page?
Perhaps if you descired what you wanted to do a little better, we could advies you on a better way to handle things.
To access, either:
Make that method a static method.
Move your code in App_Code folder.
Move your code out of your web project, into some generic assembly and use that as a reference.
I am using a user control that I created (just a .cs file not an .ascx file), that does some magic and depending on a value generated by the control, I need it to do something on the parent page that is 'hosting' the control. It needs to call a method under certain circumstances (method is on the parent control).
the control is placed on the parent page like so:
<customtag:MyControl ID="something" runat="server" />
I'm dynamically creating buttons etc on the control itself but when a button is clicked, let's say for example that there's a text box on the control and if the value of the textbox is "bob" it needs to call a method on the page that's housing the control...how can I accomplish this?
You could do what casperOne suggested, but I wouldn't advise it. This is tightly coupling your user control to your parent page, which kind of defeats the purpose of a user control.
Personally, I'd add an event to the user control (say, ButtonClicked) that the parent page can handle. In the event handler in your parent, deal with the event however you see fit. This way you can plug the user control into a different page at a later date and not have to worry about logic in the user control that requires a specific kind of parent page.
You should be able to get the Page hosting the control through the Parent property. However, that's going to be returned to you as a Control. You have to cast it to the type of your page in order to access any methods on the page which you have defined.
I think that casperOne is on the right track, but you need to go a step further. I'm giong to make the assumption that this user control will be used on more then on page. (I normally write VB.Net, sorry if my C# is malformed)
Make a base page class (you can store it in your App_Code directory or in a project):
public class PageToInheritFrom : System.Web.UI.Page {
public void SpecialFunction() {
}
}
Now make sure that all of your pages inherit from this page in your code behind file:
public partial class _Default : PageToInheritFrom {
}
Now in your user control you know what the page type is and can call
((PageToInheritFrom)this.Page).SpecialFunction();