MasterPage with slight modification. Do I need to create a new one? - asp.net

Working on a ASP.NET webform project. I have a MasterPage which works fine for 99% of my pages. However, on one page where I use a GridView, I need a slighty altered version of this MasterPage. I basically need to remove a few lines off of it:
<%--<div id="pageContentInner">--%>
<asp:ContentPlaceHolder ID="MainContent" runat="server"/>
<%--</div>--%>
Do I need to create a completely separate MasterPage or is there a way to disable that part only for that one page ?

Creating another MasterPage is probably the right way to go. Although you could do something like this:
Change your MasterPage div that needs to be invisible to a Panel.
Add a Property in the MasterPage to toggle visibility of that Panel:
public bool PageContentInnerViewable
{
get
{
return PageContentInner.Visible;
}
set
{
PageContentInner.Visible = value;
}
}
Change the visibility in page code-behind
this.Master.PageContentViewable = false;

Related

How to hide some controls in the usercontrol conditionally from parent page

I have a aspx page which has five panels. Each panel has a usercontrol in it. The user controls on all the five panels are the same. The user control in the first panel is to gather data for primary members and the rest are used for secondary members. I use ">" and "<" custom buttons to move from one panel to another. Whenever I move from the primary panel to secondary I wan to hide two textboxes in the usercontrol. When they move back to the primary panel, those textboxes should reappear. Since the buttons are in the parent page, please let me know how to do it. I believe creating an event in the usercontrol and accessing it from parent page may work. But not sure how to do it. Or please let me know any other methods.
Thanks
You need not create an event in the user controls for this.
All that you need is to create a public property on the user control, that you can set when you use the user control.
Since you have not provided any code, I will just give a sample.
public partial class MyWidget: System.Web.UI.UserControl
{
private bool showPrimary;
public bool ShowPrimary
{
get { return showPrimary; }
set
{
showPrimary = value;
txtPK1.Visible = value;
txtPK2.Visible = value;
}
}
}
Then you set it when you call it as follows:
Main Panel:
<uc1:MyWidget ID="MyWidget1" ShowPrimary="true" runat="server" />
Secondary Panel:
<uc1:MyWidget ID="MyWidget1" ShowPrimary="false" runat="server" />
I don't understand, the buttons > and < are in the page to advance to the next/previous control. Then you just have to handle their click event in the page and switch visibility of the UserControls.
If you don't know how to control visibility of controls inside of UserControls from the page:
Use properties in the UserControl, for example PrimaryMode. There you can hide/show the TextBoxes accordingly. You can call these properties from the page. PrimaryMode could be of type bool or a custom enum type(if you want to provide multiple display-modes).

ASP.Net User control How to read the child node

I have a user control defined like this
<%# Control .....
<Test:MyCustomControl id="xxx" runat="server>
</Test:MyCustomControl>
I would like to use this control on a page like
<Tag:MyControl runat="server">
<div>
my html
</div>
</Tag...
In my custom control codebehind I would like to read the inner html and set it to a property of Test:MyCustomcontrol
Currently I am getting an error saying that "...does not have property div"
How can I do this?
Note: For clarification the inner html can be an arbitrary html, so I need a way to read anything that user has typed in the page.
you can expose the div(running on the server) as a property from your UserControl
on the usercontrol html:
<div id="dvSomething" runat="server"></div>
on ur usercontrol codebehind ".cs file":
public System.Web.UI.HtmlControls.HtmlGenericControl TheDiv
{
get
{
return dvSomething;
}
set {
dvSomething = value;
}
}
on the page that contains the usercontrol:
WebUserControl11.TheDiv.InnerHtml = "addin something to div from page";
good luck
I will keep the other question open in case someone need a different solution:
I'm not sure about your requeriments like but here are two solutions, I hope this is what you want:
one adding control(you can add any by the way, not only textbox) and other pure html as per you described
TextBox txtAdd = new TextBox();
txtAdd.Style.Add("width", "200px");
WebUserControl11.Controls.Add(txtAdd);
TextBox txtRead = (TextBox)WebUserControl11.Controls[1];
((System.Web.UI.HtmlControls.HtmlGenericControl)WebUserControl11.Controls[0]).InnerHtml = "<b>something</b>";
string currentHtml = ((System.Web.UI.HtmlControls.HtmlGenericControl)WebUserControl11.Controls[0]).InnerHtml;
of course, the index will change depending how many elements you have on your user control

Hide / Replace ASP.NET WebForms Controls

I am working on a project that requires that the programmers can add asp:hyperlinks to the pages, but I want to replace those with a custom spun asp:hyperlink which checks before render if the end user has a role or not.
So basically I need a way to tell the asp application that where it renders asp:hyperlink to actually render mycontrols:customhyperlink. Is there a way to make it so that the asp:hyperlink goes to my control library instead of System.Web.UI?
I'm going to assume/suggest that you perform the user-check in the code behind. In that case, you could simply have the two controls right next to each other and only make one visible. For example, in the web-form (aspx):
<asp:Hyperlink ID="Link1" ... />
<asp:CustomHyperlink ID="CustLink1" .../>
Then in the code-behind:
if (user.HasRole) {
CustLink1.Visible = true;
Link1.Visible = false;
}
else {
CustLink1.Visible = false;
Link1.Visible = true;
}

ASP.NET Custom/User Control With Children

I want a create a custom/user control that has children.
For Example, I want my control to have the following markup:
<div runat="server" id="div">
<label runat="server" id="label"></label>
<div class="field">
<!-- INSERT CHILDREN HERE -->
</div>
</div>
and when I want to use it on a page I simply:
<ctr:MyUserControl runat="server" ID="myControl">
<span>This is a child</span>
<div runat="server" id="myChild">And another <b>child</b>
</ctr:MyUserControl>
The child controls inside my user control will be inserted into my user control somewhere. What is the best way to accomplish this?
The functionality is similar to a asp:PlaceHolder but I want to add a couple more options as well as additional markup and the such. Also the child controls still need to be able to be accessed by the page. (in the example above the page should have the myChild Control on it)
EDIT ------
It can be a template control as long as it allows me to reference the children on the page.
I asked something similar myself a while ago. See here.
I believe you will have to use an ITemplate as an InnerProperty:
[PersistenceMode(PersistenceMode.InnerProperty)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[TemplateInstance(TemplateInstance.Single)]
public ITemplate Content
{
get
{
return _content;
}
set
{
_content = value;
}
}
private ITemplate _content;
Then override the CreateChildControls method of your control:
protected override void CreateChildControls()
{
if (this.Content != null)
{
this.Controls.Clear();
this.Content.InstantiateIn(this);
}
base.CreateChildControls();
}
What's the harm in using an ITemplate You can combine it with your existing markup and write whatever HTML you want within the Content property.
Another way to approach this would be to look at the source of the Panel control (using Reflector for example). It looks like it just overrides the RenderBeginTag and RenderEndTag methods (among others to add attributes and whatnot) and defers the rest of the rendering to the WebControl class.
I know that the answer is a bit old but I have a problem which was not mentioned here.
I've tried this solution and it works well if the content are default aspx controls or plain html tags. When I put a custom web control inside I have a problem with NullReferenceException in the custom web control (child controls are all null). I overloaded OnInit method (in the custom web control code behind) to call EnsureChildControls() but child controls are not still instantiated. Do you have any idea or sugestions what the point is?
Here is the code which I use to instantiate the controls:
this._pnlButtons.Controls.Add( _lbtnOkHidden );
this._pnlButtons.Controls.Add( _lgbtnOk );
this._pnlPopup.Controls.Add( _pnlHeader );
this._pnlPopup.Controls.Add( _pnlContent );
this._pnlPopup.Controls.Add( _pnlButtons );
if ( this.Content != null )
this.Content.InstantiateIn( _pnlContent );
this._updatePanel.ContentTemplateContainer.Controls.Add( _lbShowPopup );
this._updatePanel.ContentTemplateContainer.Controls.Add( _lbtnShowPopupHidden );
this._updatePanel.ContentTemplateContainer.Controls.Add( _pnlPopup );
this._updatePanel.ContentTemplateContainer.Controls.Add( _modalExtender );
this.Controls.Add(_updatePanel);
base.CreateChildControls();

Can I create custom directives in ASP.NET?

I have created a menu control in asp.net and have put it into master page. This menu control have property ActiveItem which when modified makes one or another item from menu appear as active.
To control active item from child page I have created a control which modifies master page property enforced by IMenuContainer interface to update menu control's active item in master page.
public class MenuActiveItem : Control
{
public ActiveItemEnum ActiveItem
{
get
{
var masterPage = Page.Master as IMenuContainer;
if (masterPage == null)
return ActiveItemEnum.None;
return masterPage.ActiveItem;
}
set
{
var masterPage = Page.Master as IMenuContainer;
if (masterPage == null)
return;
masterPage.ActiveItem = value;
}
}
}
Everything works perfectly and I really enjoy this solution, but I was thinking that, if I knew how, I would have created a custom directive with same feature instead of custom control because it just makes more sense that way.
Does anybody know how to do it?
You should be able to turn this into a custom property of your Page, which you can set in the Page directive.
Create a base class for your page, and then change your Page directives like this:
<%# Page Language="C#" MasterPageFile="~/App.master"
CodeFileBaseClass="BasePage" ActiveItem="myActiveItem" AutoEventWireup="true"
CodeFile="Page1.aspx.cs" Inherits="Page1" %>
You may have to change the property to be a string, and do a conversion to the enum. But otherwise your code can remain the same, and it doesn't have to be in a control.

Resources