Solved

Server Control Page_Load event

Posted on 2004-09-17
12
401 Views
Last Modified: 2008-03-06
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.
0
Comment
Question by:carbonice
  • 7
  • 4
12 Comments
 
LVL 9

Expert Comment

by:hismightiness
ID: 12088123
Hopefully someone will tell me I am wrong in a minute, but I don't think you can use inline code AND a codebehind - which this sounds like what you want to do if I am reading your question right.

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(DataBinder.Eval(Container, "DataItem.FIELDNAME")) %>'></asp:Label>
      </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.
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12088151
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12088188
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("MyControl");

    Type controlType = control.GetType();
   
    eventInfo = controlType.GetEvent("SomeEvent");

    eventInfo.AddEventHandler(control, new
EventHandler(this.MyControl_SomeEvent));
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 1

Author Comment

by:carbonice
ID: 12102588
This is the aspx page:

<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="iso-8859-1" Src="../scripts/WebForm1.aspx.cs" AutoEventWireup="false" Inherits="fixedwithmenu" Debug="true"%>
<%@ Reference Control="../scripts/scholar.ascx" %>
<%@ Register TagPrefix="cyberakt" Namespace="CYBERAKT.WebControls.Navigation" 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.LoadControl("../scripts/scholar.ascx");
      thefrm.Controls.Add(tmp);
}
 </script>
<%
String defpath = "../../main/";

      Response.Write(String.Concat("<link href='", defpath, "css/menuStyle.css' type='text/css' rel='stylesheet'>"));
%>
<link href="../../main/css/menuStyle.css" type="text/css" rel="stylesheet">
<link href="../../main/css/default2.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.Navigation;
/*
<%@ Register TagPrefix="demos" TagName="Header" Src="~/common/page_fragments/header.ascx"%>
<demos:header id="pageHeader" SiteMapFile="menuDemos.xml" ProductLogo="logo_aspnetmenu.gif" runat="server"/>
*/
/// <summary>
/// Summary description for fixedwithmenu.
/// </summary>
public class fixedwithmenu : System.Web.UI.Page
{
  protected CYBERAKT.WebControls.Navigation.ASPnetMenu 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.DefaultGroupCssClass = "MenuGroup";
            BasicMenu.DefaultItemCssClass = "MenuItem";
            BasicMenu.DefaultItemCssClassOver  = "MenuItemOver";
            BasicMenu.DefaultItemCssClassDown = "MenuItemDown";
            OleDbDataReader DBreader;
            OleDbConnection DBconn = new OleDbConnection(String.Concat("Provider=Microsoft.Jet.OleDb.4.0; Data Source=",Server.MapPath("~/../main/db/site.mdb"),";")); //"C:\\Inetpub\\NEWFOUNDATION\\main\\db\\site.mdb;"
            OleDbCommand DBquery = new OleDbCommand("SELECT * FROM SiteLinks WHERE Link_Parent=@level ORDER BY Link_Order ASC", DBconn);
            DBquery.Parameters.Add("@level",Convert.ToString(level));
            DBconn.Open();
            DBreader = DBquery.ExecuteReader();
            
    CYBERAKT.WebControls.Navigation.MenuItem newItem;
    MenuGroup newGroup;            
            while(DBreader.Read())
            {
                  if(Convert.ToBoolean(DBreader["Link_Visible"]))//if true
                  {
                        if(Groupie == null)
                              newItem = BasicMenu.TopGroup.Items.Add(); //First Time, Null group
                        else
                              newItem = Groupie.Items.Add();
                  
                    //newItem.CssClass = "TopMenuItem";
                //newItem.CssClassOver = "TopMenuItemOver";
                        newItem.Label = (string)DBreader["Link_Name"];
                        
                        if(Convert.ToBoolean(DBreader["Link_Enabled"])) //If true
                        {
                              if(!Convert.ToBoolean(DBreader["Link_UC"])) // if false
                                    newItem.URL = (string)DBreader["Link_URL"];
                              else
                                    newItem.URL = String.Concat("UnderConstruction.aspx?","URL=" , (string)DBreader["Link_URL"], "&Name=", (string)DBreader["Link_Name"]);
                        }

                  //newItem.ID = String.Concat(Convert.ToString(level),Convert.ToString(tmpid),"item");
                  if(Convert.ToBoolean(DBreader["Link_HasChild"]) && Convert.ToBoolean(DBreader["Link_Enabled"]) && Convert.ToBoolean(DBreader["Link_Visible"]))//if all these previous are true
                  {
                        newGroup = newItem.AddSubGroup();
                        BuildBasicMenu(Convert.ToInt32(DBreader["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
0
 
LVL 1

Author Comment

by:carbonice
ID: 12102604
Is there a way for me to call the BuildBasicMenu method from the cs page in the aspx page??
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12102814
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.
0
 
LVL 17

Accepted Solution

by:
AerosSaga earned 500 total points
ID: 12102827
well thats not gonna work since your already using codebehind.  I guess your only option would be javascript at this point.

0
 
LVL 1

Author Comment

by:carbonice
ID: 12102960
=\ kk, thanks for you help.
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12102972
woops look like this won't be available until ASP.NET 2.0

http://www.dotnetjunkies.com/Tutorial/E80EC96F-1C32-4855-85AE-9E30EECF13D7.dcik
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12102980
sorry I was late on that carbonice
0
 
LVL 1

Author Comment

by:carbonice
ID: 12103026
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.aspx.cs" AutoEventWireup="false" Inherits="fixedwithmenu" Debug="true"%>
<%@ Reference Control="../scripts/scholar.ascx" %>
<%@ Register TagPrefix="cyberakt" Namespace="CYBERAKT.WebControls.Navigation" 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.LoadControl("../scripts/scholar.ascx");
     thefrm.Controls.Add(tmp);
}
 </script>
<%
String defpath = "../../main/";

     Response.Write(String.Concat("<link href='", defpath, "css/menuStyle.css' type='text/css' rel='stylesheet'>"));
%>
<link href="../../main/css/menuStyle.css" type="text/css" rel="stylesheet">
<link href="../../main/css/default2.css" type="text/css" rel="stylesheet">
</head>
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12103056
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
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
What .NET URL re-routing tool did I use? 2 54
Message not shown 5 49
Webform example, External Authentication Services, OAUTH, asp.net 4.6.2 1 29
Code works but it's slow 28 70
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question