In my inline .aspx page, I want to override the pre-init event.
How can I do this?
is it:
void pre_init(object sender)
?
Update
This is a user control
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
}
In your page you can start typing "protected override", hit enter or tab to complete, hit space, and intellisense will give a list of all the stuff you can override. If you pick one from the list it will generate the
base.myOverriddenMethod(e);
etc..
Have a look at this MSDN page for an overview of the page life cycle and a description of all events you can handle.
Related
Hi and thanks in advance.
I have an issue where I show some links at the top of page (they are not breadcrumbs) and for different circumstances I need to turn some on/off and for the page the user is on highlight that one. When I originally put the page together the call to the function that displays the links was in the Page_Init like so in the master page:
protected new void Page_Init(object sender, EventArgs e)
{
SetTabs(session);
}
protected void SetTabs(session)
{
makeTab.add(session.param, session.param);
makeTab.generate();
}
protected void add(param, param)
{
Tabs.add(new tab() { link = param, name = param });
}
protected void generate()
{
foreach(tab t in Tabs)
{
liTab.Text = string.Format("<li>{1}", t.name, t.link);
}
}
The links that get created are an unordered list of items displayed inline.
And I have a separate method that is applying a style change to highlight the link that is associated with the page that the user is on that is called from the Page_Load of the page itself:
protected void Page_Load(object sender, EventArgs e)
{
Master.makeTabBlue(nameOfTab);
}
However, in order to make hidden tabs reappear correctly, I had to move SetTabs(session) to the Page_PreRender function so that they would be called after the page was saved, when session is updated but now that I have done that the function that makes the tabs blue no longer has an effect. I've read about the page life cycle and I think that the CSS that's rendered in makeTabBlue is only effective during the Page_PreInit or Page_Init cycles but if I move the makeTabBlue to either of those then the page starts throwing additional errors that the controls can't be found which makes sense because they aren't called until PreRender. And calling it in the save function has no effect either. Where should I be calling makeTabBlue from so that it works correctly?
UPDATE:
In this particular situation, the resolution to my issue was to leave the SetTabs in the Page_Init, leave makeTabBlue in the Page_Load and I added a unique class to the LI and I am hiding, showing the links via javascript by storing values in a hidden field and checking that field when the js loads.
The last time you can do something with the page is on the Page_LoadComplete() (the one before PreRender) method. Changes to the markup will still take effect if you do it here. This should allow you to run some code in your init then run stuff later on whilst still affecting the front end.
Another option would be to add a variable in the code behind:
protected string TabColour = "some default colour";
Then on the front end, do something like:
<div style='color:#<%= TabColour %>'></div>
Where the div example above is your tab.
This would allow you to set TabColour on the prerender event but still show the correct colour on the front end as it's evaluated in the Rendering event. I prefer this method as page life cycle can be tricky and this avoids being constrained by it.
I made researching about this subject I could not find proper answer.
In my default.aspx page, I have a treeview. Codes are in default.aspx like below:
protected void Page_Load(object sender, EventArgs e)
{
}
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
Control ucont;
if (TreeView1.SelectedNode.Value == "Yeni Dönem")
{
ucont = LoadControl("usercontrols/yenidonem.ascx");
PlaceHolder1.Controls.Add(ucont);
}
else
{
ucont = LoadControl("usercontrols/tabloktar.ascx");
PlaceHolder1.Controls.Add(ucont);
}
}
I load user controls dnynmicaly. User controls are have button control. I can not fire user control's button click when I load it dynamcally. How can I solve this ?
Thanks.
First of all, I would not recommend adding control dynamically later than in Page_Load event. Other things to remember is that You should add it on each page load and assign unique ID value the control that does not change between postbacks.
In this case, the easiest way would be to always add both controls to the page and show appropriate one using Visibility property.
If that's not suitable for You, try to move the code from TreeView1_SelectedNodeChanged to the Page_Load event and load appropriate control on each postback until it should be changed to another one.
I haven't tested this, so if You have any issues when using thise answer, let me know in the comments and I'll try to help.
I have an authentication roles-based system: different roles are redirected to different folders and in each folder there is a web.config that allows the access only to a particular username.
Few roles have the default page in common with a gridview that react in different ways depending on the role(different columns are shown, events trigger different methods, etc.).
so my problem is that everytime I need to make minor changes to a page I need to copy/paste the same changes to all the others default pages in the other folders.
In terms of code I solved by creating a DefaultFather class which extends System.Web.UI.Page and every other Default class inherits from DefaultFather. In this way, if I dont declare a Page-life-method, the DefaultFather method will be triggered.
but what about the graphic part(html, javascript, asp components, etc...)??
I created a NestedMasterPage just for the Default pages but everytime I need to change the appearance/behaviour of controls(gridview, buttons, linkbuttons) I must use the FindControl() method.
there isnt really another way to solve this problem?
Im thinking of using the Page_Load() method to search for each control with FindControl() and save them into attributes for later usage but it doesnt really look like a good solution.
It would be nice if I could use the masterpage components as properties but I think that in order to do that I should create public properties and I dont know if it will cause some kind of security problem.
any suggestion?
btw, if masterpage is the solution, should I remove the DefaultFather class and place the code directly into the masterpage? or is it a good idea to have another class just for the code?
I'd say there's nothing wrong with having both a master page and a base class for your page. They serve different purposes. The master page is generally all about layout, and the base class would be about page functionality.
If you want to manipulate the markup on your master page, rather than accessing the fields directly, I'd say create a logical function which does what you need it to do, and let the master page do it.
// Site.Master.cs
public void HideSubmitButton()
{
btnSubmit.Visible = false;
}
// Default.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
((SiteMaster)Master).HideSubmitButton();
}
I'd probably wrap that cast so you can use it more easily - that is something that would belong in your base class:
// DefaultFather.cs
protected new SiteMaster Master { get { return (SiteMaster)base.Master; } }
// Default.aspx.cs
Master.HideSubmitButton();
EDIT
Per your comment about attaching event handlers - if you need to attach events to objects that live on the master (which may not be a good idea - ideally the event handler for something living on the master lives on the master - but if you really need it) you can expose methods to do that as well, like:
// Site.Master.cs
public void AttachEventHandlerToGoButton(EventHandler eventHandler)
{
btnGo.Click += eventHandler;
}
// Default.aspx.cs
Master.AttachEventHandlerToGoButton(DoMyThing);
private void DoMyThing(object sender, EventArgs e) { }
or if you want to get fancy, write a wrapper event:
// Site.Master
<asp:Button ID="btnGo" runat="server" OnClick="btnGo_Click" />
// Site.Master.cs
public event EventHandler GoButtonClick;
protected void btnGo_Click(object sender, EventArgs e) {
if (GoButtonClick != null) {
GoButtonClick(sender, e);
}
}
// Default.aspx.cs
Master.GoButtonClick += DoMyThing;
private void DoMyThing(object sender, EventArgs e) { }
Also see my edit on the Master wrapper - you need the base. there to avoid a stack overflow.
In the MSDN page lifecycle reference it states that the pre-init is used to "Create or re-create dynamic controls."
However, elsewhere on MSDN, an example implies that a dynamic user control should be loaded in Page_Load
Is this a contradiction? Or is pre-init used only for standard aspx controls?
What have I missed :)
Edit:
Either way works, however, there is presumably some benefit of one approach over the other.
(WebUserControl1 is a UserControl with a simple label property, SomeProperty)
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
WebUserControl1 wc1 = LoadControl(#"~\WebUserControl1.ascx") as WebUserControl1;
wc1.SomeProperty = "Hello World";
Controls.Add(wc1);
}
protected void Page_Load(object sender, EventArgs e)
{
WebUserControl1 wc1 = LoadControl(#"~\WebUserControl1.ascx") as WebUserControl1;
wc1.SomeProperty = "Hello World";
Controls.Add(wc1);
}
What exactly are you trying to do? Are you trying to pass information to the user control? If so you can pass the information to the user control in the page_load event from the parent page.
The answer is it depends on your custom control. If your user control doesn't need anything to happen before page_load, then you can go ahead and add your control during page load, if you have code in your user control that executes at an earlier time, then you should add your control at an earlier stage as the MSDN article suggests.
I am trying to change the master page dynamically, and although it's easy to do from a content page (overriding OnPreInit), there is no such event for a master page. Is it possible to introduce this event somehow?
UPDATE: I got halfway there by going via the PreInit of the pages at the bottom of the ladder, turns out you can do things like base.Master.MasterPageFile = "/master.Master";, but for some reason this doesn't load the stuff in the header of the top-most master page, namely stylesheets.
Quoting from: Can I change a nested master page's master dynamically?
Just tested this and it works from the PreInit of the Page that is using the nested MasterPage.
protected void Page_PreInit(object sender, EventArgs e)
{
this.Master.MasterPageFile = "/Site2.Master";
}
Obviously you will need to ensure that the ContentPlaceholderIds are consistent across the pages you are swapping between.
If you overrode the MasterPageClass and added your own onPreInit you might could do it, but I don't think even that would work. There's definitely no construct for it according to Reflector, nothing to even override, altho since it inherits UserControl then there's always OnInit ... alternately you could attempt to override get_Master() but that might not work either ...
Use the masterpage constructor.
Let's say you want to use a different master page without a menu, pass query string NoMenu.
protected void Page_PreInit(object sender, EventArgs e)
{
//You'll go through infinite loop if you do not check if we already have the new master page, this will switch to different master page if requested without a menu for example
if (Request.QueryString["NoMenu"] != null && this.MasterPageFile != "/MasterPageNoMenu.master")
{
this.MasterPageFile = "/MasterPageNoMenu.master";
base.OnPreInit(e);
}
}