Link to home
Start Free TrialLog in
Avatar of HStrix
HStrix

asked on

Asp.Net: button on ascx

Hello experts,
in my Asp.Net (C#) application
I'm using an ascx file as page header (written in C# too).
The page looks like:
---
<table class="Site_Header" width="100%" border="0">
<tr>
<td class="SiteHeader" style="WIDTH: 100px" width="100">
<A href="MyHome.aspx"><IMG alt="My homepage" src="images/home.gif" border="0"></A>&nbsp;
</td>
<td><asp:button id="btnAbout" Width="99px" runat="server" Text="About" Height="23px"></asp:button>
</td>
<td vAlign="bottom" align="right" width="200">
<asp:label id="Info" Width="51px" runat="server"></asp:label>
</td>
<td vAlign="bottom" align="right">
<asp:label id="Greeting" runat="server"></asp:label>
</td>
</tr>
</table>
---
private void btnAbout_Click(object sender, System.EventArgs e)
{ ... }
---
In the page load of the ascx I fill Info and Greeting.
That all works, except that clicks on btnAbout doesn't fire the event.
I couldn't detect the cause.

If anyone knows a solution please supply appropriate information.

   Thank you very much!

     HStrix
Avatar of mmarinov
mmarinov

Hi HStrix,

Have you register the event hadler like this

this.btnAbout.Click += new System.EventHandler(this.btnAbout_Click);

Regards!
B..M
Avatar of HStrix

ASKER

Yes, I did it as follows:
---
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
this.btnAbout.Click += new System.EventHandler(this.btnAbout_Click);
}
---
mmarinov,

when you press the button - is there a page submitting ? if - yes check is there aspnet_client directory within your IIS root directory

also do you have AutoEventWireup in the @ Page directive set to true

Regards!
B..M
Avatar of HStrix

ASKER

I set AutoEventWireup="true" in the aspx and the ascx page.
But it is strange,
sometimes the event gets fired and most of the time not.
Clicking btnAbout the Page_Load event of the aspx gets the control.
Avatar of HStrix

ASKER

My .Net version is => .Net version 1.0.3705
HStrix,

do you mean that you can not see the execution of your onclick event in your ascx or the page is not submitted at all
if you want check this events order http://dotnet.dwteam.com/demos/pageevents.aspx

B..M
Avatar of HStrix

ASKER

As I mentioned above,
sometimes I see it and mostly not.
I set breakpoint at the end of Page_Load in the aspx
and at the end of Page_Load in the ascx
and at private void btnAbout_Click(object sender, System.EventArgs e).
I used the site you suggested.
What should I look for?
HStrix,

when you click on the button control you will see what is the order of executing events when you use user control
i think that this can gives you an idea what is going on

B..M
Avatar of HStrix

ASKER

The Page_Load of the aspx page gets always fired.

The Page_Load of the ascx page gets sometimes fired;
after this the click event gets fired.
That's what I do not understand.
Avatar of HStrix

ASKER

I'm not sure if I use
   http://dotnet.dwteam.com/demos/pageevents.aspx
as it should be.
My sequence was (is this OK?):
- call my aspx
- click on btnAbout
- redirect to http://dotnet.dwteam.com/demos/pageevents.aspx
But the result of that page contains lots of information.
Which one is of interest?
HStrix,

No, this is not the purpos of the link i've send you - this link is to see what is the order of executing the events when you use user control
it will be better if you can post your code here

B..M
Avatar of HStrix

ASKER

Is "executing events" = "Trace Information"?
HStrix,

yes, the order of executing events is showed in the trace information
see that it is changed after you click on the button

B..M
Avatar of HStrix

ASKER

SiteHeader.ascx
-------------------
<%@ Control Language="c#" AutoEventWireup="true" Codebehind="SiteHeader.ascx.cs" Inherits="FileBrowser.SiteHeader" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<%@ OutputCache Duration="60" VaryByParam="none" %>
<LINK href="css/ThePhile.css" rel="stylesheet">
<table class="Site_Header" width="100%" border="0">
      <tr>
            <td class="SiteHeader" style="WIDTH: 100px" width="100"><A href="MyHome.aspx"><IMG alt="My homepage" src="images/home.gif" border="0"></A>&nbsp;
            </td>
            <td><asp:button id="btnAbout" Width="99px" runat="server" Text="About" Height="23px"></asp:button></td>
            <td vAlign="bottom" align="right" width="200"><asp:label id="Info" Width="51px" runat="server"></asp:label></td>
            <td vAlign="bottom" align="right"><asp:label id="Greeting" runat="server"></asp:label></td>
      </tr>
</table>
<asp:label id="SystemInfo" runat="server"></asp:label>
==================
SiteHeader.ascx.cs
---------------------
namespace FileBrowser
{
      using System;
      using System.Data;
      using System.Drawing;
      using System.Web;
      using System.Web.UI.WebControls;
      using System.Web.UI.HtmlControls;

      /// <summary>
      ///            Summary description for SiteHeader.
      /// </summary>
      public abstract class SiteHeader : System.Web.UI.UserControl
      {
            protected System.Web.UI.WebControls.Label Info;
            protected System.Web.UI.WebControls.Label Greeting;

            private string strJavaScript;          
            private string strBrowser;              
            private string strBrowserText;          
            private string strMajorVersion;        
            private string strMinorVersion;        
            private string strVersion;
            protected System.Web.UI.WebControls.Label SystemInfo;
            protected System.Web.UI.WebControls.Button btnAbout;              
            private string strCLR_Version;          

            private void Page_Load(object sender, System.EventArgs e)
            {
                  // Put user code to initialize the page here
                  Greeting.Text = "Welcome, ";
                  if (Context.User.Identity.IsAuthenticated)
                        Greeting.Text += Context.User.Identity.Name;
                  else
                        Greeting.Text += "Guest User";

                  // My info    
                  Info.Text = "Hellooo!";      
                  detectEnvironment();        
                  SystemInfo.Text = strBrowserText;
                  SystemInfo.Text += "<br>You are running .Net version " + strCLR_Version;
                  SystemInfo.Text += "<br>" + strJavaScript;
            }

            private void detectEnvironment()
            {
                  // check if javascript is enabled:
                  if (Request.Browser.JavaScript == true)
                  {
                        strJavaScript = "Your browser settings support JavaScript." ;
                  } else
                  {
                        strJavaScript = "Your browser settings do not support JavaScript." ;                        
                  }
                  //
                  strBrowser = Request.Browser.Browser.ToString();
                  strBrowserText = "You're using the browser '" + strBrowser + "',";
                  strMajorVersion = Request.Browser.MajorVersion.ToString();
                  strMinorVersion = Request.Browser.MinorVersion.ToString();
                  strVersion = Request.Browser.Version.ToString();
                  strCLR_Version = Request.Browser.ClrVersion.ToString();
                  strBrowserText += " its version is:'" + strMajorVersion + "." + strMinorVersion + "';";
                  if (strBrowser != "IE")
                  {
                        strBrowserText += "<br>you're NOT using the Internet Explorer.";
                  } else
                  {
                        //strBrowserText += "<br>you're using the Internet Explorer.";
                  }
            }

            #region Web Form Designer generated code
            override protected void OnInit(EventArgs e)
            {
                  //
                  // CODEGEN: This call is required by the ASP.NET Web Form Designer.
                  //
                  InitializeComponent();
                  base.OnInit(e);
            }
            
            ///            Required method for Designer support - do not modify
            ///            the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                  this.Load += new System.EventHandler(this.Page_Load);
                  this.btnAbout.Click += new System.EventHandler(this.btnAbout_Click);
            }
            #endregion

            private void btnAbout_Click(object sender, System.EventArgs e)
            {
                                   // Currently a .Net dll is used here:
                  ClassLib.N4Web.Class4Web objWeb = new ClassLib.N4Web.Class4Web();
                  objWeb.evtHello();                  
            }
      }
}
Avatar of HStrix

ASKER

start.aspx
----------
<%@ Page language="c#" Codebehind="start.aspx.cs" AutoEventWireup="true" Inherits="Web.MyStart" %>
<%@ Register TagPrefix="Controls" TagName="Header" Src="SiteHeader.ascx" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
      <HEAD>
            <title>Startseite</title>
            <link href="file:///c:\Inetpub\wwwroot\web\css\myCss.css" type="text/css" rel="stylesheet">
            <meta content="Microsoft Visual Studio 7.0" name="GENERATOR">
            <meta content="C#" name="CODE_LANGUAGE">
            <meta content="JavaScript" name="vs_defaultClientScript">
            <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
      </HEAD>
      <body MS_POSITIONING="GridLayout">
            <form id="BrowseFiles" method="post" runat="server">
                  <CONTROLS:HEADER id="Title" runat="server"></CONTROLS:HEADER>
                  <!-- --><br>
                  <br>
                  <asp:button id="btnUseClass" runat="server" Height="23px" Width="99px" Text="Button2Class"></asp:button>
                  <!-- --><br>
                  <!-- -->
............................
            </form>
      </body>
</HTML>
---
The button btnUseClass has exactly the same code as the button in the ascx, it is ALWAYS executed.
Avatar of HStrix

ASKER

Hello mmarinow,
is this all you need?
HStrix,

first of all  you can use this:

          private void Page_Load(object sender, System.EventArgs e)
          {
            if ( !IsPostBack)
            {
               // Put user code to initialize the page here
               Greeting.Text = "Welcome, ";
               if (Context.User.Identity.IsAuthenticated)
                    Greeting.Text += Context.User.Identity.Name;
               else
                    Greeting.Text += "Guest User";

               // My info    
               Info.Text = "Hellooo!";      
               detectEnvironment();        
               SystemInfo.Text = strBrowserText;
               SystemInfo.Text += "<br>You are running .Net version " + strCLR_Version;
               SystemInfo.Text += "<br>" + strJavaScript;
            }
          }


second, what actually these lines do:

               ClassLib.N4Web.Class4Web objWeb = new ClassLib.N4Web.Class4Web();
               objWeb.evtHello();          

 ?
B..M
HStrix,

can you post the Page_Load of the aspx ?
B..M
Avatar of HStrix

ASKER

They are accessing a dll written in Vb.Net (as Windows ClassLibrary).
It only contains the following sample code
---
Namespace N4Web
    Public Class Class4Web
        Public Sub evtHello()
            MsgBox("Hello, from DLL 'Class4Web")
        End Sub
    End Class
End Namespace
---
The project name is ClassLib.
Avatar of HStrix

ASKER

OK, the PostBack check should be added.
Avatar of HStrix

ASKER

Here it is:
-----------
            private string folderPath;
            protected System.Web.UI.WebControls.Button btnUseClass;
            private string realFolderPath;
      
            private void Page_Load(object sender, System.EventArgs e)
            {
                  // extract from the querystring the path to scan
                  folderPath = Request.Params["Images"];
                  if(folderPath == null || folderPath == "/")
                        folderPath = Request.ApplicationPath.ToString();

                  // if the Folder is not "/" but ends with "/"  -> remove last "/"
                  else if (folderPath.EndsWith("/"))
                        folderPath = folderPath.Substring(0, folderPath.Length - 1);

                  realFolderPath = Server.MapPath(folderPath);
                  // path mapping
                  FolderDescription.Text = "Virtual folder: " + folderPath + "<br>Physical folder: " + Server.MapPath(folderPath);

                  // scan the specified folder
                  FillFoldersAndFilesTables();
            }
Avatar of HStrix

ASKER

The aspx code starts with
-----
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
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 System.IO;

namespace Web
{
      /// <summary>
      public class MyStart : System.Web.UI.Page
      {
            protected System.Web.UI.WebControls.Table Table1;
            protected System.Web.UI.WebControls.Label StatusMessage;
            protected System.Web.UI.WebControls.Label FolderStyle;
            protected System.Web.UI.WebControls.Label FolderDescription;

            protected System.Web.UI.WebControls.Table Table2;
            protected System.Web.UI.WebControls.Label FileStyle;
            protected System.Web.UI.WebControls.Table TableMain;

            protected System.Web.UI.WebControls.Button btnUseClass;

            private string folderPath;
            private string realFolderPath;
      
            private void Page_Load(object sender, System.EventArgs e)
            {
......
HStrix,

put if ( !IsPostBack ) and in the Page_load event for aspx and see what will     happened


B..M
Avatar of HStrix

ASKER

Ok, I did it,
but the result is still the same.
HStrix,

it is just a desparete try ( i don't see anything else )
modify this
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
this.btnAbout.Click += new System.EventHandler(this.btnAbout_Click);
}
to
private void InitializeComponent()
{
this.btnAbout.Click += new System.EventHandler(this.btnAbout_Click);
this.Load += new System.EventHandler(this.Page_Load);
}

if it is not working i can not do anything without the code
i'm out of ideas
B..M
Avatar of HStrix

ASKER

It was NOT successful.
Do you have a mail address where I can send the files?
HStrix,

if the compleate code is not so long it is better to post it here
other wise the email is at my member profile

B..M
Avatar of HStrix

ASKER

OK, I've send the mail.
HI there, the problem was resolved :) - it was caused because of this line

<%@ OutputCache Duration="60" VaryByParam="none" %>
you must set it like this
<%@ OutputCache Duration="60" VaryByParam="*" %>

aslo you can change ( on both start.aspx and SiteHeader.ascx ) AutoEventWireup set to false

B..M
Avatar of HStrix

ASKER

Thanks mmarinow,
the result of my test is:
- after starting the app the click works
- the next click does not work
- the next click works
- all other clicks do not work
- after a while the click works again
- then the click does not work
it looks that there is a time dependency.

In my next test, I made the following modification:
<%@ OutputCache Duration="1" VaryByParam="*" %>
After this modification it almost works,
sometimes I need to click a second time, then it works again.
I think there is still a 'small' point to be solved.

It always works, when my clicks are not "too" fast after clicking the OK button of the message box.


ASKER CERTIFIED SOLUTION
Avatar of mmarinov
mmarinov

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of HStrix

ASKER

Thank you very much mmarinow for your great help.

That IS the solution.
I cannot remember where this line came from.

Thanks again

  HStrix

You are welcome HStrix

Regards!
B..M