Are themes visible at design time? - asp.net

This is my first experience with themes.
I've created a skin file for a textbox and the defined theme is only visible at runtime.
Is this the way theme works or I am missing something?

You should use StylesheetTheme property that can be set on page level or globally
Page Level
<%# Page Language="C#" StylesheetTheme="MyTheme" %>
Global (web.config)
<system.web>
<pages styleSheetTheme="MyTheme" />
<system.web>

Related

Preventing Pages theme from being imposed on each aspx page directive during compilation

in my web.config i use the following pages declaration
<pages maintainScrollPositionOnPostBack="true" Theme="Theme1">
with the intention of being able to use different themes on a web application to customize the way it displays for a few different clients(some of which want their own branding on the web application's pages)
if i leave out the theme directive in my development code, it doesn't display properly when debugging, so i want to have the Theme defined in the web.config... however when it is defined as above and i go to publish my solution every .aspx page declaration goes from this:
<%# Page Language="VB" CodeFile="MRSHome.aspx.vb"%>
to this:
<%# page language="VB" inherits="App_Web_peiwi24o" theme="Theme1" maintainScrollPositionOnPostBack="true" %>
and page level theme declarations override the ones set in the web.config file, i need that to remain the case since some pages programmatically switch to a print friendly theme when the user needs it.
How do i prevent the web.configs pages settings from being appended to each page's page declaration during compilation?
Instead of declaring the theme in web.config page declaration, you can add a appsettings in webconfig:
<appSettings>
<add key="MyCustomTheme" value="Theme1" />
</appSettings>
And in form PreInit you can set the theme for the page:
Protected Sub WebForm1_PreInit(sender As Object, e As EventArgs) Handles Me.PreInit
Dim myTheme As String = ConfigurationManager.AppSettings("MyCustomTheme")
Page.Theme = myTheme
End Sub
So, when you deploy, your page declaration will not contain any theme. In runtime it will pickup the correct theme defined in config.

How to disable ViewState for a .aspx page?

I need to completely disable ViewState for a .aspx page inside my web application. I have gone through different blogs and what I understand is that we must set <%# Page EnableViewState="false" ...%>, but this is not working.
Is this enough to disable ViewState for all the controls inside the page? Or should I make any additional modifications? If so, please specify them. I don't want the ViewState enabled for even a single control inside the .aspx page
Disabling View State
Machine Level -
Disabling view state at machine level in machine.config, will disable ViewState of all the applications on the web server.
<Machine.config>
<system.web>
<pages enableViewState="false" />
</system.web>
</Machine.config>
Application Level -
You can disable ViewState for all pages in /web.config file.
<configuration>
<system.web>
<pages enableViewState="false" />
</system.web>
</configuration>
Page Level -
Disabling view state for a specific aspx file at the top.
<%# Page Language="C#" .. EnableViewState="false" .. %>
Control Level - You can disable ViewState for a specific control.
<asp:TextBox EnableViewState="false" ID="Name"
runat="server"></asp:TextBox>
I think the quotes should be:
EnableViewState="false"
Apart from that, if you are still seeing the hidden fields then they are used by ASP.Net. You may see:
Page.EnableViewState Property
Even if EnableViewState is false, the page might contain a hidden view
state field that is used by ASP.NET to detect a postback.
If you truly don't need postback, you can remove the form element from your page, this will remove the viewstate entirely.

Controlling the page from reaching the top of the page

I have a registration page with many fields and DDl's when i scroll down and select the ddl value then the page going to top of the page and again i have to scroll to the control and have to select the page.How to control the page by stopping at the control even though the value is changed.
Use MaintainScrollPositionOnPostback="true" in Page directive
Something like this
<%# Page MaintainScrollPositionOnPostback="true" %>
to know more about this Go here.
Alternatively you can, put this in Web.config to make this setting global, By this
<?xml version="1.0"?>
<configuration>
<system.web>
<pages maintainScrollPositionOnPostBack="true">
</pages>
</system.web>
<configuration>

How to set default Master Page dynamically

I have two master pages. When the user logs in it should set first master page as default master page. If other login it should set second master page.
You can specify the default master page to use in the web.config file, using the masterPageFile attribute of the pages element:
<system.web>
<pages masterPageFile="~/DefaultMaster.master" />
<!-- more configuration goes here... -->
</system.web>
The page will use that master page unless it has a MasterPageFile specified.
As #RoBYCoNTe pointed out in the comments, you can set the MasterPageFile property on your pages.
If each page inherits from a common BaseClass, you can set the MasterPageFile property in your BaseClass to avoid having to do it on each individual page.

Cannot see new asp.net user controls

I am feeling like an idiot right now, but I cannot for the life of me figure out why I cannot seem to call user controls from within my asp.net webpage. I'm still learning asp.net but I can't find any information from searching on google.
I'm trying to load a specific control on the page when the user presses a linkbutton. So I created an empty user control via the right click menu:
<%# Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
In other words, I have not touched any part of the created user control. Yet attempting to create this web control and add it to the form seems to not work, as it claims that the WebUserControl class does not exist (I have no other controls in my project):
UserControl blah = new WebUserControls();
produces a "The type or namespace is invalid". Why can none of my asp.net webform pages get the control into scope?
The new control must be added to the site's Web.config file.
<configuration>
<system.web>
<pages>
<controls>
<add tagPrefix="my"
tagName="WebUserControl"
src="~/WebUserControl.ascx"/>
</controls>
</pages>
</system.web>
</configuration>
Use this to place the new control in an .aspx page.
<my:WebUserControl runat="server" ID="MyWebUserControl" />
In addition to registering them one-by-one in web.config, you can also use a <%# Register %> directive in the source of the markup files that reference the control.
Or, you can move your controls into a DLL and include them all by referencing the assembly from your web.config (takes some extra work, though).

Resources