i'm trying to solve a bug in Visual Studio, the suggestion is to stop using UserControls and use Control instead..
So i'm converting all my UserControl into just Control, e.g.:
public partial class Controls_UserManagement_GroupManager : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
becomes
public partial class Controls_UserManagement_GroupManager : System.Web.UI.Control
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
Except that there is no Control.IsPostBack?
How do i replace UserControl with Control?
Series
This question is one in the ongoing Stackoverflow series, "Templating user controls":
How to add a Templating to a UserControl?
How to inherit from Control, rather than UserControl?
UserControl has IsPostBack, but Control does not
UserControl does not have public property named ContentTemplate
How do i specify CodeFileBaseClass from web.config?
Control has a Page property, which has an IsPostback property. This should give you the value you need.
public class MyControl : Control{
protected override void OnInit( EventArgs e ){
if( this.Page.IsPostBack ){
// do something
}
}
}
MSDN Reference
Related
I want to create a usercontrol for retrieving and filtering a dataset. The problem I'm having is that the controls to be populated are not in the usercontrol, but on the main page on which the usercontrol exists. The reason for this is that the controls that are to be populated are different on each page.
If I could get some help solving the following simplified problem, I will probably be on my way to solve the bigger problem:
<body>
<form id="form1" runat="server">
<div>
<uc1:uc1 ID="uc1" runat="server" />
<asp:Label ID="lbl" runat="server"></asp:Label>
</div>
</form>
</body>
What I want to do is set the label on the main page by clicking a button on the usercontrol. Problem I'm having is that the usercontrol postback happens after the main page.
Thanks!
I managed to solve it by calling Parent.Page. I also implemented an interface so that I can call different pages' methods from the user control. Not sure if it's a good solution, but it will do:
interface IFilter
{
void SetLabel(string text);
}
public partial class uc : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn_Click(object sender, EventArgs e)
{
((IFilter)Parent.Page).SetLabel("changed");
}
}
public partial class _Default : System.Web.UI.Page , IFilter
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
}
}
public void SetLabel(string text)
{
lbl.Text = text;
}
}
create a custom event in user control
create a property in usercontrol
fire the event when the button is clicked & set the property value
access the property in the main page in user control's eventhandler
I use an ascx user control for manage CRUD o database entity. I reuse this userc control in my aspx page for show in readonly mode the database data of a record on database. The user control have inside a simple FormView and an objectdatasource.
Now, in a aspx page that contains that ascx i have to know, in DATABIND time of the aspx some data of the record of the database that is considerate by the user control. User control are databind after the aspx page, and for this reason i don't have the data. I have to do a select in aspx page on database and after the user control do the same select.
How can i do for optimize this process?
ASCX base events may be fired after your ASPX's base events, but in the whole lifecycle, you can fire your own events.
You could define an Event on your ASCX, make your page register to this event, and then propagate your custom event from your ASCX to your ASPX, with whatever data you need in the arguments
rough example (may not compile) : in the ASCX
public partial YourControl : System.Web.UI.UserControl {
public event EventHandler MyControlDataBound;
public void FireMyControlDataBound()
{
if (MyControlDataBound!= null)
{
MyControlDataBound(this, new EventArgs());
}
}
protected void MyDataBound(object sender, EventArgs e) {
// ......
FireMyControlDataBound();
}
}
and in the ASPX
public partial class MyPage: Page
{
protected void Page_Load(object sender, EventArgs e)
{
yourUserControlInstance.MyControlDataBound += HandleYourDataInYourPage;
}
protected void HandleYourDataInYourPage(object sender, EventArgs e) {
// .. do whatever needed in your page, with your data
// if you have defined a custom Args class that inherits EventArgs, your could collect data here...
}
}
Feel free to create a class that inherits EventArgs to pass data with your event if you need this
You can pass your arguments to your UserContol in init event of your Page
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
var control = (UserControl)this.FindControl("UserControlId");
control.Property = ...;//Pass User Control properties
}
I want to bind to the Page PreRender method on at the global.asax level, but for some reason the method is never getting called. My method looks like:
protected void Page_PreRender(object source, EventArgs e)
{
/* do stuff */
}
Am I able to call Page events like this in Global.asax?
The global.asax is derived from HttpApplication class and not contain the Page_PreRender event as you can see on the MSDN reference:
http://msdn.microsoft.com/en-us/library/system.web.httpapplication(VS.90).aspx
If you want to globally capture the PreRender event you can make a different base class for the System.Web.UI.Page, overwrite this event, and use this class to your pages.
For example
public abstract class BasePage : System.Web.UI.Page
{
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
}
}
and use the BasePage on your pages
I created a textbox and a submit button in header(User Control) included in a master page
and I want to use that textbox value after clicking submit in my .aspx.vb page.
How can i access that, as the vb page is loaded first and then master page is loading ?
TextBox Val = (TextBox)this.Master.FindControl("TextBoxID");
The best way to communicate from UserControl to Page/MasterPage is using events
The best way to communicate from MasterPage to Page is using events
On this way you don't hardlink UserControls with their Pages/MasterPages and the Page with it's Master.
Add an event to your UserControl that is raised when the user clicks the button, for example:
In UserControl of type MyControl:
public delegate void SubmittedHandler(MyControl ctrl);
public event SubmittedHandler Submitted;
protected void BtnCLick(object sender, EventArgs e) {
Submitted(this);
}
Then add an handler to this event in your MasterPage, handle it and raise the Master's event again:
In Master's codebehind:
public delegate void MyControlSubmittedHandler(MyControl ctrl);
public event MyControlSubmittedHandler ControlSubmitted;
protected void Page_Init(Object sender, EventArgs e) {
this.MyControl1.Submitted += MyControlSubmitted;
}
protected void MyControlSubmitted(MyControl sender) {
ControlSubmitted(sender);
}
Then add an handler to this event to your page:
In your Page:
protected void Page_Init(object sender, EventArgs e) {
((SiteMaster)Master).ControlSubmitted += MasterControlSubmitted;
}
protected void MasterControlSubmitted(MyControl sender){
// do whatever you need to do
}
If you only need to access the TextBox from the page and you don't need to handle the click-event, you could also use properties to achieve this:
add a public property f.e. MyControlText in your UserControl that get/set the TextBox.Text property
add a public property f.e. MyControlText in your Master that get/set your UserControl's MyControlText property
Now you can get/set this property from your page in this way:
((SiteMaster)Master).MyControlText = "Hello World";
I have hard time to modify a page that had a Custom User Control directly to the ASPX page and now require to have it dynamically loaded when needed only. The User Control does have html and other controls via the ASCX file and has code in the code-behind.
I have read multiple page and have found that I cannot instantiate directly the User Control but should use the Page.LoadControl(...). The problem is not the compiling but when the page load the control it happen that all controls inside the ASCX are null and then crash.
How can I use a User Control that has code in the ASCX and in the code-behind dynamically?
Edit:
Example of what I am doing in (PageLoad or PagePreRender or PagePreInit)
Control c = LoadControl(typeof(MyControl), null);
myControl= (MyControl)c;
myControl.ID = "123";
myControl.Visible = false;
Controls.Add(myControl);
MyControl does have for example <div id="whatever" runat="server">... and inside the MyControl it set the visibility to True or False... but when it does that, now it crash because the "whatever" div is NULL.
What I have done is use the Page.LoadControl method in the Page_Init to add the custom user control to a place holder on the page.
protected void Page_Init(object sender, EventArgs e)
{
//MyControl is the Custom User Control with a code behind file
MyControl myControl = (MyControl)Page.LoadControl("~/MyControl.ascx");
//UserControlHolder is a place holder on the aspx page
// where I want to load the user control to
UserControlHolder.Controls.Add(myControl);
}
This works fine for me.
Here is the code for the dynamically loaded user control:
MyControl.ascx.cs
public partial class MyControl : System.Web.UI.UserControl
{
protected void Page_Init(object sender, EventArgs e)
{
LiteralControl lit = new LiteralControl("Test Literal Control");
Page.Controls.Add(lit);
}
protected void Page_Load(object sender, EventArgs e)
{
whatever.Visible = true;
if (IsPostBack)
{
whatever.Visible = false;
}
}
}