Disabling Update panel from content page - asp.net

Hello I have a ToolkitScriptManager and update panel on master page. I want to disable update panel functionality on a content page but it should work fine for other pages.
Master Page Code:
<ajax:ToolkitScriptManager runat="server" ID="sm1" EnableScriptGlobalization="true"
EnableScriptLocalization="true" ScriptMode="Release" CompositeScript-
ScriptMode="Release" />
<asp:UpdatePanel ID="udpEmail" runat="server">
<ContentTemplate>
<asp:ContentPlaceHolder ID="cphMain" runat="server">
</asp:ContentPlaceHolder>
</ContentTemplate>
</asp:UpdatePanel>
Content Page Code:(e.g. Page1.aspx)
<asp:Content ID="Content3" ContentPlaceHolderID="cphMain" runat="Server">
Code Here
</asp:Content>
So Now I want to that Update panel functionality should not work on Page1.aspx but it should work on other content pages of same master page. Please help

what about on the code behind to check for the name of the page and set the visibility of cphMain to false? http://forums.asp.net/t/1163743.aspx
Have you explored Razor + MVC yet?

On the page where it should be disabled, try something like this:
UpdatePanel panel = Page.Form.FindControl("UpdatePanel1") as UpdatePanel;
if (panel != null)
{
panel.Enabled = false;
}
Depending on where the UpdatePanel is located in the master page, you may need to search the page recursively, like this:
private void DisableControl(Control parentCtrl, string controlID)
{
foreach (Control ctrl in parentCtrl.Controls)
{
if (ctrl.ID == controlID)
{
((WebControl)ctrl).Enabled = false;
continue;
}
DisableControl(ctrl, controlID);
}
}

You can either cast Master property on content page to correct masterpage type, or use
<%# MasterType %>
to have typed master page.
Then you will have access to controls on it and can disable update panel.

I haven't found a way yet better than the below, To override the onInit and add that code
protected override void OnInit(EventArgs e)
{
//disables the ajax effect, and allows postback
ScriptManager.GetCurrent(Page).EnablePartialRendering = false;
base.OnInit(e);
}

Related

Why is Page.PreviousPage always null?

I am experimenting with cross-page posting by following this MSDN article. I have this code:
CrossPagePosting1.aspx
<form id="form1" runat="server">
<h1>Page 1</h1>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" Text="Button" PostBackUrl="CrossPagePosting2.aspx"/>
</form>
CrossPagePosting2.aspx
<form id="form1" runat="server">
<h1>Page 2</h1>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</form>
CrossPagePosting2.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
TextBox TextBox1 = (TextBox)Page.PreviousPage.FindControl("TextBox1");
Label1.Text = TextBox1.Text;
}
This code above produces a NullReferenceException at Page.PreviousPage. Why?
This is an ASP.Net 4.0 application.
It uses FriendlyUrls, which is the default.
NOTE: I do NOT want the previous page to be strongly-typed, e.g. using the PreviousPageType directive. According to the referenced article, this shouldn't be necessary.
I found that Friendly URLS might get you this problem. By default, the Web Forms template includes ASP.NET Friendly URLs.
When you use the default WebForm from visual Studio, the AutoRedirectMode is set to Permanent. This makes you request into a "GET" and since you are using Friendly URLS, you can’t evaluate the PreviousPage.
Workarounds:
If you want a "POST" action then set the AutoRedirectMode =
RedirectMode.Off (this will give you PreviousPage info but only from
non-Friendly-Url pages [ex: www.you.com/mypage.aspx], however this
will get you an error if you try to access the Friendly-Url page [ex:
www.you.com/mypage] << no .aspx).
If you want the PreviousPage information you will have to set the
previous post directive in you webpage <%# PreviousPageType
VirtualPath="~/Page1.aspx" %> OR maybe use the Server.Transfer in a
OnClick Method.
The problem here was being caused by FriendlyUrls, which were installed by default on the test site I was working in. I disabled FriendlyUrls, and it worked.
I think following article will helps you.
http://csharpdotnetfreak.blogspot.com/2009/08/cross-page-posting-in-aspnet.html
there are two method how to use Cross Page Posting PostBack
The reason this is happening is simply because you did not set the postbackurl property correctly.
If CrossPagePosting2.aspx is at root of your program change postbackurl to ~/CrossPagePosting1.aspx
You do not need to add the <%# PreviousPageType %> directive when using postbackurl property. using PreviousPage.FindControl(id) will search the form elements that are posted using postbackurl property
Try this
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack && PreviousPage != null)
{
Page page = PreviousPage;
Label1.Text = ((TextBox)page.FindControl("TextBox1")).Text;
}
}

Fetch cached control in ASP.NET

I am using the following in ASP.NET webcontrols :
<%# OutputCache Duration="86400" VaryByParam="none" %>
This means that the control will be null on reload if it is already added to the cache. The problem is that on some page I want to hide this control and it would be great if this could be done from the MasterPage codebehind file(where it is loaded).
I have tried this :
if (Request.AppRelativeCurrentExecutionFilePath.ToLower().EndsWith("/sites/MySite/default.aspx") || Request.AppRelativeCurrentExecutionFilePath.ToLower().EndsWith("MySite.net"))
{
if(topGames_Mini1 != null)
{
//Load control
topGames_Mini1.visible=true;
}
}
else
{
Page.LoadControl("topGames_Mini1").Visible = false;
}
It will however throw the following exception in the else :
The file '/Bradspel/sites/MySite/community/topGames_Mini1' does not
exist.
you should better place the UserControl inside a Placeholder control. Then simply hide/show the Placeholder depending on your conditions.
The Placeholder does not render any tags for itself, so there is NO overhead of outer HTML tags.
I Assume you must have registered your UserControl in your Master page. So, place the userControl now inside a PlaceHolder control.
<asp:ContentPlaceHolder ID="MainContent" runat="server"><!-- Of Master Page -->
<asp:PlaceHolder ID="place1" runat="server">
<uc1:Test ID="Test1" runat="server" /><!-- Our User Control-->
</asp:PlaceHolder>
</asp:ContentPlaceHolder>
and in Code behind::
protected void Page_Load(object sender, EventArgs e)
{
if( _Some_Condition_)
place1.Visible = true;
else
// Hide PlaceHolder and thus all controls inside it
place1.Visible = false;
}

Keep a value accessible in Page.Init between postbacks in asp.net

Ok, I've already learned that in order to keep the dynamic (created) controls and their viewstate in asp.net we must (re)create them in the Page init event, so they already exist within the page's control hierarchy before the view state is loaded.
As it says in this article.
There is no problem to achieve this behaviour if the criteria to create these controls is outside the web environment, for example a database. But what should I do if what I use to decide how many dynamic controls I have to create is actually a value that is in the controls?
I try to explain it with an example, maybe it's clearer:
Let's say that we have a textbox and two buttons. In the textbox I write the number of how many dynamic controls I want to create, for example 4 checkbox. When I hit the button1 the controls should be created. No problem. Then I check some checkboxes and hit the button2 just to fire a postback. Now I should recreate the controls in the page init event, like we said before, in order to maintain the controls and their state.
And here comes the problem. Because of I'm in the init stage I have no viewstate so I'm no able to access the value in the textbox that tells me how many dynamic checkbox should I create.
I thought that storing the value in the session object would do the trick, but it doesn't. The session object is no accessible as well.
Where can I save the value that it'll be accessible from the init event too?
Thanks and sorry for the long post!
First thing - textbox value is not stored/retrieved from view state, you cannot get textbox value from viewstate.
Coming to actual problem, here is the sequence of (imp) events init -> load view state -> bind postback data -> page load. You can retrieve textbox value only after bind postback data event (which actually takes posted data and binds to the textbox control).
In init only option is to use Request.Form{"textboxid"] to get the textbox value.
You are on the right track.
If you use TextBox, you do not need another ViewState to keep track of how many controls has been created, because TextBox control has its own ViewState already.
You can use either Page_Init or Page_Load to load control back.
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"
Inherits="WebApplication2010.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox runat="server" ID="NumberTextBox" />
<asp:Button runat="server" ID="CreateControlButton"
OnClick="CreateControlButton_Click"
Text="Create Control" />
<br />
<asp:PlaceHolder runat="server" ID="PlaceHolder1"></asp:PlaceHolder>
</div>
</form>
</body>
</html>
using System;
using System.Web.UI;
namespace WebApplication2010
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
int ids;
if (Int32.TryParse(NumberTextBox.Text, out ids))
{
for (int i = 0; i < ids; i++)
{
Control ctrl = Page.LoadControl("WebUserControl.ascx");
ctrl.ID = i.ToString();
PlaceHolder1.Controls.Add(ctrl);
}
}
}
}
protected void CreateControlButton_Click(object sender, EventArgs e)
{
}
}
}
<%# Control Language="C#" AutoEventWireup="true"
CodeBehind="WebUserControl.ascx.cs"
Inherits="WebApplication2010.WebUserControl" %>
<asp:CheckBox runat="server" ID="CheckBox1" />
<asp:Button runat="server" ID="Button1" OnClick="Button_Click"
Text="Post Back" />
<asp:Label runat="server" ID="Label1" />
<br />
using System;
namespace WebApplication2010
{
public partial class WebUserControl : System.Web.UI.UserControl
{
protected void Button_Click(object sender, EventArgs e)
{
Label1.Text = " This control was posted back.";
}
}
}
This is the common paradox with page lifecycle when you work with code behind. For example, you save the editing customerid in viewstate but controls need to bind on init to be able to read their posted values.
Best real life solution is to do what bound controls do on postback, call explicilately the LoadPostBackData on IPostBackDataHandler interface that all controls implement, after you have created them in Page Load event.
You should create an extension method for Controls and call it when needed:
control.DataBind();
control.LoadPostedData(); // extension method

ASP.NET page with base class with dynamic master page not firing events

I am feeling that I have terribly wrong somewhere. I was working on a small asp.net app. I have some dynamic themes in the \theme folder and have implemented a page base class to load the master page on the fly. The master is having the ContentPlaceHolder like:
<asp:ContentPlaceHolder ID="cphBody" runat="server" />
Now I am adding pages that are derived from my base class and added the form elements. I know, Visual Studio has problem showing the page in the design mode. I have a dropdown box and wish to add the event of onselectedindexchange. But it is not working. the page is like this:
<%# Page Language="C#" AutoEventWireup="true" Inherits="trigon.web.Pages.MIS.JobStatus" Title="Job Status" AspCompat="true" CodeBehind="JobStatus.aspx.cs" %>
<asp:Content ID="Content1" ContentPlaceHolderID="cphBody" runat="Server">
<div id="divError" runat="server" />
<asp:DropDownList runat="server" id="jobType" onselectedindexchange="On_jobTypeSelection_Change"></asp:DropDownList>
</asp:Content>
I have also tried adding the event on the code behind like:
protected void Page_Load(object sender, EventArgs e)
{
jobType.SelectedIndexChanged += new System.EventHandler(this.On_jobTypeSelection_Change);
if (!IsPostBack)
{
JobStatus_DA da = new JobStatus_DA();
jobType.DataSource = da.getJobTypes();
jobType.DataBind();
}
}
protected void On_jobTypeSelection_Change(Object sender, EventArgs e)
{
//do something here
}
Can anybody help?
Regards,
set AutoPostBack="true" on your dropdown

ASP.NET linkbutton visible property issue

I'm using a public variable called IsAdmin in the code behind of an aspx page.
public partial class _news : System.Web.UI.Page
{
public bool IsAdmin = false;
protected void Page_Load(object sender, EventArgs e)
{
if (User.Identity.Name.Contains("admin"))
{
IsAdmin = true;
}
else
{
IsAdmin = false;
}
}
And i use the property Visible='<%#IsAdmin%>' to assign to panels which i want to show if the user is an admin in the aspx design of the page. Strangely it works for the linkbuttons i've put on the repeater.
<asp:Panel ID="Panel1" runat="server" Visible='<%#IsAdmin%>'>
<asp:LinkButton ID="LinkButton2" runat="server" PostBackUrl='<%# "news_edit.aspx? Action=edit&id=" + Convert.ToString( Eval("news_id")) %>Edit</asp:LinkButton>
<asp:LinkButton ID="LinkButton3" runat="server" PostBackUrl='<%# "news.aspx?Action=delete&id=" + Convert.ToString( Eval("news_id")) %>'>Delete</asp:LinkButton>
</asp:Panel>
and it works fine, however outside the repeater i've put another linkbutton without a panel
<asp:LinkButton ID="LinkButton4" runat="server" PostBackUrl="~/news_edit.aspx?action=new" Visible='<%#IsAdmin%>'>Add New Item</asp:LinkButton>
but the visible property doesn't work on it! I tried putting it inside a panel too and setting it's visible property but that too didn't work.
So i have following doubts
1)what is the issue?
2)what is the technical name when we use references like '<%#IsAdmin%>' in the design page
3)Does page load happen before page is rendered of after page is rendered?
Thanks
<%# %> is the syntax used for accessing databound fields. Since you are likely databinding the Repeater control at some point, these expressions will be evaluated.
Since you are likely not calling databind on the Panel and the Linkbuttons outside of the Repeater, these expressions will not be processed. You can probably change them to something like
<%= IsAdmin.ToString() %>
and get the result you want.
Check this great blog entry for more information on the differences.
Also, Page Load happens before the page is rendered. Rendering the page is the last thing that happens in the ASP.Net page lifecycle.

Resources