• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 849
  • Last Modified:

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
0
HStrix
Asked:
HStrix
  • 19
  • 14
1 Solution
 
mmarinovCommented:
Hi HStrix,

Have you register the event hadler like this

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

Regards!
B..M
0
 
HStrixAuthor Commented:
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);
}
---
0
 
mmarinovCommented:
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
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
HStrixAuthor Commented:
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.
0
 
HStrixAuthor Commented:
My .Net version is => .Net version 1.0.3705
0
 
mmarinovCommented:
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
0
 
HStrixAuthor Commented:
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?
0
 
mmarinovCommented:
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
0
 
HStrixAuthor Commented:
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.
0
 
HStrixAuthor Commented:
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?
0
 
mmarinovCommented:
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
0
 
HStrixAuthor Commented:
Is "executing events" = "Trace Information"?
0
 
mmarinovCommented:
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
0
 
HStrixAuthor Commented:
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();                  
            }
      }
}
0
 
HStrixAuthor Commented:
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.
0
 
HStrixAuthor Commented:
Hello mmarinow,
is this all you need?
0
 
mmarinovCommented:
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
0
 
mmarinovCommented:
HStrix,

can you post the Page_Load of the aspx ?
B..M
0
 
HStrixAuthor Commented:
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.
0
 
HStrixAuthor Commented:
OK, the PostBack check should be added.
0
 
HStrixAuthor Commented:
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();
            }
0
 
HStrixAuthor Commented:
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)
            {
......
0
 
mmarinovCommented:
HStrix,

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


B..M
0
 
HStrixAuthor Commented:
Ok, I did it,
but the result is still the same.
0
 
mmarinovCommented:
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
0
 
HStrixAuthor Commented:
It was NOT successful.
Do you have a mail address where I can send the files?
0
 
mmarinovCommented:
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
0
 
HStrixAuthor Commented:
OK, I've send the mail.
0
 
mmarinovCommented:
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
0
 
HStrixAuthor Commented:
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.


0
 
mmarinovCommented:
why do you need this line
<%@ OutputCache Duration="60" VaryByParam="*" %>
anyway ?

because if i remove it - i think everything is ok

B..M
0
 
HStrixAuthor Commented:
Thank you very much mmarinow for your great help.

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

Thanks again

  HStrix

0
 
mmarinovCommented:
You are welcome HStrix

Regards!
B..M
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 19
  • 14
Tackle projects and never again get stuck behind a technical roadblock.
Join Now