carbonice
asked on
Server Control Page_Load event
I know how you can change properties of server controls, but how about running an event in an Onload event of the aspx page where the control resides? As it stands right now, If I do that, the control I am using is currently using the onload event in it's code and I can't use both. If I could just all a method in the .cs file, then I could do it from the Onload event on the aspx page....Anyone have any ideas??? If this doesn't make any sence let me know, I'm just trying to call a method in a .cs file from the aspx file.
Hrmmm. The following code worked for me - in the control
code I inserted a button and raised the SomeEvent from
its Click event.
Control control = null;
Type controlType = null;
EventInfo eventInfo = null;
Control control = this.FindControl("MyContro l");
Type controlType = control.GetType();
eventInfo = controlType.GetEvent("Some Event");
eventInfo.AddEventHandler( control, new
EventHandler(this.MyContro l_SomeEven t));
code I inserted a button and raised the SomeEvent from
its Click event.
Control control = null;
Type controlType = null;
EventInfo eventInfo = null;
Control control = this.FindControl("MyContro
Type controlType = control.GetType();
eventInfo = controlType.GetEvent("Some
eventInfo.AddEventHandler(
EventHandler(this.MyContro
ASKER
This is the aspx page:
<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="iso-8859 -1" Src="../scripts/WebForm1.a spx.cs" AutoEventWireup="false" Inherits="fixedwithmenu" Debug="true"%>
<%@ Reference Control="../scripts/schola r.ascx" %>
<%@ Register TagPrefix="cyberakt" Namespace="CYBERAKT.WebCon trols.Navi gation" Assembly="ASPnetMenu" %>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Confirmation</title >
<script language="c#" runat="server">
protected override void OnLoad(EventArgs e)
{
BasicMenu.BuildBasicMenu(0 , null);
FrmScholar tmp = (FrmScholar)Page.LoadContr ol("../scr ipts/schol ar.ascx");
thefrm.Controls.Add(tmp);
}
</script>
<%
String defpath = "../../main/";
Response.Write(String.Conc at("<link href='", defpath, "css/menuStyle.css' type='text/css' rel='stylesheet'>"));
%>
<link href="../../main/css/menuS tyle.css" type="text/css" rel="stylesheet">
<link href="../../main/css/defau lt2.css" type="text/css" rel="stylesheet">
</head>
in the onload event I am trying to initialize a control from an .ascx page, the following is the .cs file:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls ;
using CYBERAKT.WebControls.Navig ation;
/*
<%@ Register TagPrefix="demos" TagName="Header" Src="~/common/page_fragmen ts/header. ascx"%>
<demos:header id="pageHeader" SiteMapFile="menuDemos.xml " ProductLogo="logo_aspnetme nu.gif" runat="server"/>
*/
/// <summary>
/// Summary description for fixedwithmenu.
/// </summary>
public class fixedwithmenu : System.Web.UI.Page
{
protected CYBERAKT.WebControls.Navig ation.ASPn etMenu BasicMenu;
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
BuildBasicMenu(0, null); //initalize this to 0 and null for top level of menu
}
}
//recursive method to populate the menu
public void BuildBasicMenu(int level, MenuGroup Groupie)
{
//newItem.CssClass = "TopMenuItem";
//newItem.CssClassOver = "TopMenuItemOver";
BasicMenu.DefaultGroupCssC lass = "MenuGroup";
BasicMenu.DefaultItemCssCl ass = "MenuItem";
BasicMenu.DefaultItemCssCl assOver = "MenuItemOver";
BasicMenu.DefaultItemCssCl assDown = "MenuItemDown";
OleDbDataReader DBreader;
OleDbConnection DBconn = new OleDbConnection(String.Con cat("Provi der=Micros oft.Jet.Ol eDb.4.0; Data Source=",Server.MapPath("~ /../main/d b/site.mdb "),";")); //"C:\\Inetpub\\NEWFOUNDAT ION\\main\ \db\\site. mdb;"
OleDbCommand DBquery = new OleDbCommand("SELECT * FROM SiteLinks WHERE Link_Parent=@level ORDER BY Link_Order ASC", DBconn);
DBquery.Parameters.Add("@l evel",Conv ert.ToStri ng(level)) ;
DBconn.Open();
DBreader = DBquery.ExecuteReader();
CYBERAKT.WebControls.Navig ation.Menu Item newItem;
MenuGroup newGroup;
while(DBreader.Read())
{
if(Convert.ToBoolean(DBrea der["Link_ Visible"]) )//if true
{
if(Groupie == null)
newItem = BasicMenu.TopGroup.Items.A dd(); //First Time, Null group
else
newItem = Groupie.Items.Add();
//newItem.CssClass = "TopMenuItem";
//newItem.CssClassOver = "TopMenuItemOver";
newItem.Label = (string)DBreader["Link_Nam e"];
if(Convert.ToBoolean(DBrea der["Link_ Enabled"]) ) //If true
{
if(!Convert.ToBoolean(DBre ader["Link _UC"])) // if false
newItem.URL = (string)DBreader["Link_URL "];
else
newItem.URL = String.Concat("UnderConstr uction.asp x?","URL=" , (string)DBreader["Link_URL "], "&Name=", (string)DBreader["Link_Nam e"]);
}
//newItem.ID = String.Concat(Convert.ToSt ring(level ),Convert. ToString(t mpid),"ite m");
if(Convert.ToBoolean(DBrea der["Link_ HasChild"] ) && Convert.ToBoolean(DBreader ["Link_Ena bled"]) && Convert.ToBoolean(DBreader ["Link_Vis ible"]))// if all these previous are true
{
newGroup = newItem.AddSubGroup();
BuildBasicMenu(Convert.ToI nt32(DBrea der["Link_ ID"]), newGroup );
}
}//closing bracket for first IF
}
DBconn.Close();
}
In the Page_Load event in the CS file, the menu is loaded...The problem is that I can't get the two to both run, only 1 or the other. I am currently looking over the information that AerosSaga gave me, but perhapes, this would better explain the issue I am having...Thanks
<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="iso-8859
<%@ Reference Control="../scripts/schola
<%@ Register TagPrefix="cyberakt" Namespace="CYBERAKT.WebCon
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Confirmation</title
<script language="c#" runat="server">
protected override void OnLoad(EventArgs e)
{
BasicMenu.BuildBasicMenu(0
FrmScholar tmp = (FrmScholar)Page.LoadContr
thefrm.Controls.Add(tmp);
}
</script>
<%
String defpath = "../../main/";
Response.Write(String.Conc
%>
<link href="../../main/css/menuS
<link href="../../main/css/defau
</head>
in the onload event I am trying to initialize a control from an .ascx page, the following is the .cs file:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls
using CYBERAKT.WebControls.Navig
/*
<%@ Register TagPrefix="demos" TagName="Header" Src="~/common/page_fragmen
<demos:header id="pageHeader" SiteMapFile="menuDemos.xml
*/
/// <summary>
/// Summary description for fixedwithmenu.
/// </summary>
public class fixedwithmenu : System.Web.UI.Page
{
protected CYBERAKT.WebControls.Navig
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
BuildBasicMenu(0, null); //initalize this to 0 and null for top level of menu
}
}
//recursive method to populate the menu
public void BuildBasicMenu(int level, MenuGroup Groupie)
{
//newItem.CssClass = "TopMenuItem";
//newItem.CssClassOver = "TopMenuItemOver";
BasicMenu.DefaultGroupCssC
BasicMenu.DefaultItemCssCl
BasicMenu.DefaultItemCssCl
BasicMenu.DefaultItemCssCl
OleDbDataReader DBreader;
OleDbConnection DBconn = new OleDbConnection(String.Con
OleDbCommand DBquery = new OleDbCommand("SELECT * FROM SiteLinks WHERE Link_Parent=@level ORDER BY Link_Order ASC", DBconn);
DBquery.Parameters.Add("@l
DBconn.Open();
DBreader = DBquery.ExecuteReader();
CYBERAKT.WebControls.Navig
MenuGroup newGroup;
while(DBreader.Read())
{
if(Convert.ToBoolean(DBrea
{
if(Groupie == null)
newItem = BasicMenu.TopGroup.Items.A
else
newItem = Groupie.Items.Add();
//newItem.CssClass = "TopMenuItem";
//newItem.CssClassOver = "TopMenuItemOver";
newItem.Label = (string)DBreader["Link_Nam
if(Convert.ToBoolean(DBrea
{
if(!Convert.ToBoolean(DBre
newItem.URL = (string)DBreader["Link_URL
else
newItem.URL = String.Concat("UnderConstr
}
//newItem.ID = String.Concat(Convert.ToSt
if(Convert.ToBoolean(DBrea
{
newGroup = newItem.AddSubGroup();
BuildBasicMenu(Convert.ToI
}
}//closing bracket for first IF
}
DBconn.Close();
}
In the Page_Load event in the CS file, the menu is loaded...The problem is that I can't get the two to both run, only 1 or the other. I am currently looking over the information that AerosSaga gave me, but perhapes, this would better explain the issue I am having...Thanks
ASKER
Is there a way for me to call the BuildBasicMenu method from the cs page in the aspx page??
I guess you could always use inline scripting style in the .aspx page, but I'm not sure its gonna give you any different results.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
=\ kk, thanks for you help.
woops look like this won't be available until ASP.NET 2.0
http://www.dotnetjunkies.com/Tutorial/E80EC96F-1C32-4855-85AE-9E30EECF13D7.dcik
http://www.dotnetjunkies.com/Tutorial/E80EC96F-1C32-4855-85AE-9E30EECF13D7.dcik
sorry I was late on that carbonice
ASKER
Actually Aeros, I figured it out, I can call the method for the menu creation, BuildBasicMenu(int level, MenuGroup Groupie), from the aspx page, because it is code behind, the method is like it's on the page itself...Thanks a lot for you help though:
<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="iso-8859 -1" Src="../scripts/WebForm1.a spx.cs" AutoEventWireup="false" Inherits="fixedwithmenu" Debug="true"%>
<%@ Reference Control="../scripts/schola r.ascx" %>
<%@ Register TagPrefix="cyberakt" Namespace="CYBERAKT.WebCon trols.Navi gation" Assembly="ASPnetMenu" %>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Confirmation</title >
<script language="c#" runat="server">
protected override void OnLoad(EventArgs e)
{
BasicMenu.BuildBasicMenu(0 , null); *******************Changed To --------------> BuildBasicMenu(0, null); ********* Works just fine ******
FrmScholar tmp = (FrmScholar)Page.LoadContr ol("../scr ipts/schol ar.ascx");
thefrm.Controls.Add(tmp);
}
</script>
<%
String defpath = "../../main/";
Response.Write(String.Conc at("<link href='", defpath, "css/menuStyle.css' type='text/css' rel='stylesheet'>"));
%>
<link href="../../main/css/menuS tyle.css" type="text/css" rel="stylesheet">
<link href="../../main/css/defau lt2.css" type="text/css" rel="stylesheet">
</head>
<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="iso-8859
<%@ Reference Control="../scripts/schola
<%@ Register TagPrefix="cyberakt" Namespace="CYBERAKT.WebCon
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Confirmation</title
<script language="c#" runat="server">
protected override void OnLoad(EventArgs e)
{
BasicMenu.BuildBasicMenu(0
FrmScholar tmp = (FrmScholar)Page.LoadContr
thefrm.Controls.Add(tmp);
}
</script>
<%
String defpath = "../../main/";
Response.Write(String.Conc
%>
<link href="../../main/css/menuS
<link href="../../main/css/defau
</head>
ahhh.....glad to hear you got it working carbonice. So you can use inline to call, just not to create if you have a codebehind I suppose.
Regards,
Aeros
Regards,
Aeros
There are exceptions though. For instance, if you are wanting to perform a special function on a value for a server control you can call the function like so:
<!-- example with a datagrid template column -->
<asp:TemplateColumn HeaderText="COLUMNNAME">
<ItemTemplate>
<asp:Label runat="server" Text='<%# MySpecialFunctionName(Data
</ItemTemplate>
</asp:TemplateColumn>
In the codebehind, you would write your function like normal. However, I know there is a slight difference in VB.Net where you need to make the function Public for the calling function to be able to call it, but I don't know if there are similar needs in CS.