view state in asp.net - asp.net

I can disable viewstate of each control, but not entire page.
Is there a way to disable viewstate in whole page?

Set Page.EnableViewState to false. This can be done either in the code-behind or in the page directive:
<%# Page EnableViewState="false" %>
You can also disable ViewState at the application level by setting the enableViewState attribute of the pages node to false in your web.config:
<pages enableViewState="false"/>

You can do it in the page declaration:
<%# Page EnableViewState='false' %>

private void Page_Init(object sender, System.EventArgs e)
{
this.EnableViewState = false;
}

You can set Page.EnableViewState="false"
But no matter what there will be some very small ViewState footprint on any .aspx page you create.

<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" EnableViewState="false" %>

Related

My web control doesn't appear in my web page in a different folder

My web control doesn't appear in my web page in a different folder,
However it works in another web page in the same location.
My source code is like this :
..
<%Register src="~/UI/Human/HumanUserControl.wuc" TagPrefix="uc1"
TagName="HumanUserControl"/>
...
<uc1:HumanUserControl runat="Server" id="HumanUserControl"/>
...
Error:
Request is not available in this context
The syntax on your Register tag is incorrect. And I'm not sure why you're using a ".wuc" extension instead of the default ".ascx".
You need matching <% %> tags, like so:
<%# Register Src="~/UI/Human/HumanUserControl.wuc" TagPrefix="uc1" TagName="HumanUserControl" %>
If you delete the <%Register .../> and <uc1 .../> tags from your page completely, then in Visual Studio, drag-and-drop the user control from the Solution Explorer directly onto your page wherever you want the control to appear, it will automatically add the correct Register and user control tags for you.
If this doesn't clear things up, then provide more detailed error information, including the code where the exception is thrown.
The following works just fine:
~/UI/Human/HumanUserControl.ascx
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="HumanUserControl.ascx.cs" Inherits="UserControlTest.UI.Human.HumanUserControl" %>
<asp:Label ID="lblTest" runat="server" />
~/UI/Human/HumanUserControl.ascx.cs
namespace UserControlTest.UI.Human
{
public partial class HumanUserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
lblTest.Text = Request.Browser.Browser;
}
}
}
~/Default.aspx
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="UserControlTest._Default" %>
<%# Register Src="~/UI/Human/HumanUserControl.ascx" TagPrefix="uc1" TagName="HumanUserControl" %>
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
<uc1:HumanUserControl runat="server" id="HumanUserControl" />
</asp:Content>

ASP.NET button click event only works when on Top level master page

I have base.master (top-level), second.master (submaster). A button in the top-level master fires the click event fine, on a page which ues that one master, if its on page where the second.master is used, that button click event doesn't work.
No buttons that I place in the submaster work either, heres the code:
protected void afd(object sender, EventArgs e)
{
var a = "C";
}
<asp:Button runat="server" OnClick="afd" Text="huh" />
Base.master directive:
<%# Master Language="C#" MasterPageFile="~/umbraco/masterpages/default.master" AutoEventWireup="True" CodeBehind="Base.master.cs" Inherits="UmbracoWebsite.masterpages.Base" %>
Register.master directive:
<%# Master CodeBehind="~/masterpages/Register.master.cs" Inherits="UmbracoWebsite.masterpages.Register" Language="C#" MasterPageFile="~/masterpages/Base.master" AutoEventWireup="True" %>
By this it means you are wiring up button_click event manually and also you are missing button name so i would assume it as btn1
so you also need to provide the following line on Page_Load of Page_Init of the page where using it:
btn1.Click += new EventHandler(afd);
Hope this helps....

Asp.net: conditional loading of user controls fails

Hello (sorry for the poor title)
I have a user control which loads different additional user controls based on some conditions like this:
<%# Control Language="C#" AutoEventWireup="true" CodeFile="SubPage.ascx.cs" Inherits="SubPage" %>
<%# Register Src="<srcA>" TagName="A" TagPrefix="CTRL" %>
<%# Register Src=">srcB>" TagName="B" TagPrefix="CTRL" %>
<% if (someValue == 1) { %>
Loading user control A..
<CTRL:A runat="server" />
<% } else { %>
Loading user control B..
<CTRL:B runat="server" />
<% } %>
The result will look correct; the expected content is displayed. But I noticed that even though someValue != 1 and control B is displayed, control A is still loaded behind the scenes (page load is called).
Why is this? And what would be a better approach? Thanks.
Page_Load is called because you handle this event.
Don't try to load them in this way but use the Visible-Property instead from codebehind.
Expose a public function that the controller(in your case SubPage.ascx) calls after it changed the visible state to load the content of the UserControl. Controls that aren't visible won't be rendered as html at all.
Loading controls dynamically if you don't really need can cause unnecessary ViewState- or Event-Handling issues. Here are some other disadvantages mentioned regarding dynamic UserControls.
You need to call LoadControl method instead
<% if (someValue == 1) { %>
Loading user control A..
Page.LoadControl(("~\ExampleUserControl_A.ascx");
<% } else { %>
Loading user control B..
this.LoadControl(("~\ExampleUserControl_B.ascx");
<% } %>
Code Front:
<%# Control Language="C#" AutoEventWireup="true" CodeFile="SubPage.ascx.cs" Inherits="SubPage" %>
<%# Register Src="<srcA>" TagName="A" TagPrefix="CTRL" %>
<%# Register Src="<srcB>" TagName="B" TagPrefix="CTRL" %>
<asp:placeholder id="plhControls" runat="server" />
Code Behind:
if (someValue == 1) {
CTRLA ctrlA = (CTRLA)LoadControl("~/Controls/ctrlA.ascx");
plhControls.Controls.Add(ctrlA);
} else {
CTRLB ctrlB = (CTRLB)LoadControl("~/Controls/ctrlB.ascx");
plhControls.Controls.Add(ctrlB);
}

Content control not accessbile from content page?

My content page looks like this:
<%# Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Basket.aspx.cs" Inherits="Basket" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
</asp:Content>
Now, I would like to add some controls dynamically to the content when page loads, so I am trying the following code:
protected void Page_Load(object sender, EventArgs e)
{
Content2. // I want to add controls to it dynamically
}
The problem is that the Content2 control is not visible by the compiler and I am getting error about missing directive or assembly reference.
Any solution?
The reason you can't get a reference to that asp:Content control is because it does not stay around when the page is combined with the masterpage. Basically ASP takes all the controls from inside these asp:Content sections and makes them children of the ContentPlaceholder controls inside the masterpage.
As MSDN says: A Content control is not added to the control hierarchy at runtime. Instead, the contents within the Content control are directly merged into the corresponding ContentPlaceHolder control.
That means that if you want to add more controls to that section, you will have to get a reference to the ContentPlaceholder control in the masterpage and add them to it. Something like:
ContentPlaceHolder myContent = (ContentPlaceHolder)this.Master.FindControl("ContentPlaceHolder1");
myContent.Controls.Add(??);
Notice you are using the ContentPlaceHolderID value, NOT the ID of the asp:Content section.
I will recommend that you put a placeholder control in content and use it to add controls. For example,
<%# Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Basket.aspx.cs" Inherits="Basket" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<asp:Placeholder runat="server" ID="Content1Controls" />
</asp:Content>
..
And
protected void Page_Load(object sender, EventArgs e)
{
Content1Controls.Controls.Add(...
}

progress template in asp.net?

in my web application i am uploading a video file. While uploading i want to give message to user wait video uploading can i use UpdateProgress can u give me a simple example. Thank you
Hi there here is a small example how you display a updateprogress on a background job:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
UpdateProgress control
Loading...
protected void UpdateButton_Click(object sender, EventArgs e)
{
System.Threading.Thread.Sleep(5000);
}

Resources