Solved

Asp.Net: button on ascx

Posted on 2004-10-19
33
826 Views
Last Modified: 2013-11-25
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
Comment
Question by:HStrix
  • 19
  • 14
33 Comments
 
LVL 28

Expert Comment

by:mmarinov
ID: 12345957
Hi HStrix,

Have you register the event hadler like this

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

Regards!
B..M
0
 

Author Comment

by:HStrix
ID: 12345978
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
 
LVL 28

Expert Comment

by:mmarinov
ID: 12346050
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
 

Author Comment

by:HStrix
ID: 12346211
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
 

Author Comment

by:HStrix
ID: 12346249
My .Net version is => .Net version 1.0.3705
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12346292
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
 

Author Comment

by:HStrix
ID: 12346369
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
 
LVL 28

Expert Comment

by:mmarinov
ID: 12346404
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
 

Author Comment

by:HStrix
ID: 12346408
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
 

Author Comment

by:HStrix
ID: 12346635
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
 
LVL 28

Expert Comment

by:mmarinov
ID: 12346656
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
 

Author Comment

by:HStrix
ID: 12346662
Is "executing events" = "Trace Information"?
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12346684
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
 

Author Comment

by:HStrix
ID: 12346732
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
 

Author Comment

by:HStrix
ID: 12346786
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
 

Author Comment

by:HStrix
ID: 12346796
Hello mmarinow,
is this all you need?
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 28

Expert Comment

by:mmarinov
ID: 12346805
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
 
LVL 28

Expert Comment

by:mmarinov
ID: 12346827
HStrix,

can you post the Page_Load of the aspx ?
B..M
0
 

Author Comment

by:HStrix
ID: 12346859
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
 

Author Comment

by:HStrix
ID: 12346880
OK, the PostBack check should be added.
0
 

Author Comment

by:HStrix
ID: 12346926
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
 

Author Comment

by:HStrix
ID: 12347002
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
 
LVL 28

Expert Comment

by:mmarinov
ID: 12347033
HStrix,

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


B..M
0
 

Author Comment

by:HStrix
ID: 12347123
Ok, I did it,
but the result is still the same.
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12347159
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
 

Author Comment

by:HStrix
ID: 12347239
It was NOT successful.
Do you have a mail address where I can send the files?
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12347265
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
 

Author Comment

by:HStrix
ID: 12347557
OK, I've send the mail.
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12355498
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
 

Author Comment

by:HStrix
ID: 12356143
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
 
LVL 28

Accepted Solution

by:
mmarinov earned 500 total points
ID: 12356195
why do you need this line
<%@ OutputCache Duration="60" VaryByParam="*" %>
anyway ?

because if i remove it - i think everything is ok

B..M
0
 

Author Comment

by:HStrix
ID: 12356256
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
 
LVL 28

Expert Comment

by:mmarinov
ID: 12356273
You are welcome HStrix

Regards!
B..M
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now