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

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

Related

I am creating a website in ASP.Net using MasterPage

I am creating a website in ASP.Net using MasterPage . I need Help for using different page title on each page .
eg: My Company : Home
and My Compant : offerings on my second page how to do using a MasterPage
Master
...
<title>
<asp:ContentPlaceHolder ID="TitleContentPlaceHolder" runat="server"></asp:ContentPlaceHolder>
</title>
...
Content
...
<asp:Content ContentPlaceHolderID="TitleContentPlaceHolder" runat="server" ID="TitleContent">
<asp:Literal runat="server" ID="TitleLabel"></asp:Literal>
</asp:Content>
...
Content codeBehind
protected void Page_Load(object sender, EventArgs e)
{
...
TitleLabel.Text = "Some title";
...
}
there is a section on your child page as follows
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
You can put anything in-between which you write in the head section.
<%# Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" Title="Untitled Page" %>
here you can set the title in the page directive
or on the code behind
protected void Page_Load(object sender, EventArgs e)
{
Page.Title = "Master Page ";
}
You can use codebehind to give title to your content pages..
protected void Page_Load(object sender, EventArgs e)
{
Page.Title="your title";
}
This is the easiest way which you can follow..

how to call function from html file in asp.net

i'm new to asp.net
i want to call function from my html file
I have a html file containing a list of menu
Menu 1
and i have a file name home.aspx, i want to call a function in home.aspx.cs with that hyperlink
I have tried this but still can not
Call a C# function in ASP.NET when clicking on a HTML link
sorry for my English, English is not my native language
please help me
is my first code name home.aspx
<%# Page Language="C#" AutoEventWireup="true" CodeFile="home.aspx.cs" Inherits="user_home" %>
<% Response.WriteFile("tools/menu.htm"); %>
and this is home.aspx.cs
public partial class user_home : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e){
}
and this is my menu.htm
<div id="menu">
<ul>
<li>Menu1</li>
</ul>
</div>
sorry for trouble
Menu 1
You should use runat="server" attribute.
Are you sure you are not looking for a LinkButton???
This control renders in the client as:
Click me...
ASPX
<asp:LinkButton Text="Click me..." runat="server" ID="myLink" Click="myLink_Click" />
Code behind
protected void myLink_click(object sender, EventArgs e)
{
// write here your cool stuff as a response of the click event
}
If you insist to use an HtmlControl, then:
In ASPX
Menu 1
IN code behind
protected void myLink2_Click(object sender, EventArgs e)
{
// write here your cool stuff as a response of the click event
}

ASP.NET Web Forms multiple views and the lifecycle of controls that are not Visible

Given a scenario where a page/control should display different views (like tabs) in different circumstances (query string argument, postback from a control, user setting retrieved from the database) I would normally put a control like MultiView or Placeholder and switch the active view index or the Visible property like this:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="TestWebApplication.WebForm2" %>
<%# Register Src="SomeControl.ascx" TagName="SomeControl" TagPrefix="uc1" %>
<!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:PlaceHolder runat="server" ID="phControl" Visible="false">
<uc1:SomeControl ID="SomeControl1" runat="server" />
</asp:PlaceHolder>
<asp:PlaceHolder runat="server" ID="phNotControl" Visible="false">
Some other content
</asp:PlaceHolder>
</div>
</form>
</body>
</html>
And I would switch the views in a code behind depending on the logic like this:
using System;
namespace TestWebApplication
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
phControl.Visible = false;
phNotControl.Visible = true;
}
}
}
This is the control:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="SomeControl.ascx.cs" Inherits="TestWebApplication.SomeControl" EnableViewState="false" %>
I am the control
And the code behind of the control:
using System;
namespace TestWebApplication
{
public partial class SomeControl : System.Web.UI.UserControl
{
protected void Page_Init(object sender, EventArgs e)
{
Response.Write("I am the control's Init and I am executing heavy operations <br />");
}
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("I am the control's Load <br />");
}
protected void Page_PreRender(object sender, EventArgs e)
{
Response.Write("I am the control's PreRender and I am only called if the control is Visible <br />");
}
}
}
However what if the control in question has some relatively heavy work to do (calling the database) in its init and load events? They get executed even if the control is not Visible and will slow down the page. One possible solution I see is moving all the work in the PreRender method of the child control because it is not executed for controls which are not visible. However this will not work if the control accepts user input from some dynamically populated control (think of dropdownlist) because the logic that populates the dropdown with options will not be executed.
I've come up with the following solutions:
Move the heavy work in the PreRender method and wrap it in an if(!IsPostBack). Then let the ViewState hold the possible values for the dropdown and ASP.NET will restore it. The downside is that it is using ViewState and bumping up the size of the page.
Create the control dynamically with LoadControl and add it in the placeholder from code behind. This will trigger catch up events and the code that populates the dropdown can be put in the Init method where it belongs. The downside is that the layout is defined in the code behind. Looking at the markup one cannot see what controls are on the page and looking at the code behind it is not clear where the control will appear. It also makes styling and other "design" operations more difficult.
Leave it like it is. The downside is that there will be additional database queries and if there are a lot of views these would add up.
My actual question is how I solve this problem without putting stuff in the ViewState, without multiple queries and without dragging my layout into the code behind.
Could it be a solution to make an AJAX call for your heavy database work?
Then you could display the View and in the background load the data you need and display it. The page would load really fast and you could display some indication that data is currently being loaded.
How about creating a public method on in your user controls, such as InitControl(). Do all the heavy work within InitControl() instead on page load. Then, on parent page, when page is being shown, call InitControl.
using System;
namespace TestWebApplication
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
phControl.Visible = false;
phNotControl.Visible = true;
SomeControl1.InitControl();
}
}
}
Don't do any work within OnLoad event of your user controls.

Disabling Update panel from content page

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);
}

How To Call Child user Control Events on Parent Pages

I have two User Controls. And one aspx page which is loading the controls. Code is given Below
Home.aspx:
<%# Page Title="" Language="C#" AutoEventWireup="true" CodeBehind="Home.aspx.cs" Inherits="MYApp.Home" %>
<asp:Panel ID="pnlTabs" runat="server"></asp:Panel>
Home.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
base.LoadUserControl(UC_Tabs, pnlTabs);
}
Tabs.ascx:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="UC_Tabs.ascx.cs" Inherits="MyApp.UserControls.UC_Tabs" %> <asp:Button ID="btnPhotoTab" runat="server" Text="Photos" CssClass="tab_style" style="border-right:none;" onclick="btnPhotoTab_Click" />
<asp:Panel ID="pnlPhotos" runat="server"></asp:Panel>
Tabs.ascx.cs:
protected void btnActivityFeed_Click(object sender, EventArgs e)
{
Home HomePage = (Home)this.Page;
LoadUserControl("UC_PhotoSearch.ascx", pnlPhotos);
}
public Control LoadUserControl(string ControlName, Control Container)
{
Control UControl = null;
if (ControlName != null)
{
UControl = this.LoadControl("~/UserControls/" + ControlName);
UControl.ID = UControl.GetType().Name;
Container.Controls.Add(UControl);
}
return UControl;
}
UC_PhotoSearch.ascx:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="UC_PhotoSearch.ascx.cs" Inherits="MyApp.UserControls.UC_PhotoSearch" %>
<input id="txtSearch" name="txtSearch" type="text" runat="server" />
<asp:Button ID="btnSearch" runat="server" Text="Search" onclick="btnSearch_Click" />
<span id="PhotoSearchedData" runat="server"></span>
UC_PhotoSearch.ascx.cs:
protected void btnSearch_Click(object sender, EventArgs e)
{
//Photo Search Code here
}
The above is all my code.
Now I am loading the Tabs control on Home page and
Photo search Control on Tabs control
Now I click on Search Button But the
btnSearch_Click in not firing and even the page load event is also not firing.
How can I able to Access the btnSearch_Click event from home page.
If I used the following on Tabs control then the button click is working fine.
<%# Register Src=”UC_PhotoSearch.ascx” TagName=”PhotoSearch” TagPrefix=”uc1″ %>
<uc1:PhotoSearch ID=”PhotoSearch″ runat=”server” />
So what is wrong with dynamic loading of controls.
Please Help me
Not sure if I understand you quite, but I think if you load your controls in the Page's PreInit event and not Page_load your events will fire.

Resources