CodeVerge.Net Beta


   Explore    Item Entry    Members      Register  Login  
NEWSGROUP
.NET
Algorithms-Data Structures
Asp.Net
C Plus Plus
CSharp
Database
HTML
Javascript
Linq
Other
Regular Expressions
VB.Net
XML

Free Download:




Zone: > NEWSGROUP > Asp.Net Forum > windows_hosting.hosting_open_forum Tags:
Item Type: NewsGroup Date Entered: 10/14/2003 9:14:15 AM Date Modified: Subscribers: 0 Subscribe Alert
Rate It:
(NR, 0)
XPoints: N/A Replies: 0 Views: 17 Favorited: 0 Favorite
Can Reply:  No Members Can Edit: No Online: Yes
1 Items, 1 Pages 1 |< << Go >> >|
Wernand
Asp.Net User
problem with usercontrols event handlers please help! (Advanced)10/14/2003 9:14:15 AM

0/0

Hi, All

Before... my English is not so good so forgive me.

I have a problem with the use of different usercontrols, and there events.
I have made a simple script with this problem because the original script is simple to big to place in this Question Box.

The following files I am using for this sample script:

// the three usercontrols
control1.ascx
// containing: a button with the name: Text="Button on control 1?
control2.ascx
// containing: a button with the name: Text="Button on control 2?
control3.ascx
// containing: a button with the name: Text="Button on control 3?

// the three code behind classes
control1.cs
control2.cs
control3.cs

// the main class
simpleclass.cs

// and the default start page
default.aspx
=====================================================

I have used an enumeration to give the different usercontrols a unique name.

public enum ControlName { Control1, Control2, Control3 };

I have made a delegate to define an event

public delegate void ControlChangeEventHandler(ControlName c);

The job for this event is simply: change the current usercontrol to another one

I have created an interface

public interface IControls
{
event ControlChangeEventHandler OnControlChange;
void SetControl(ControlName c);
}

Look at the script below to see what the SetControl does.

**************************************************

After compiling and loading the default.aspx you see a button, which belongs to the first usercontrols
this button has the text: ?Button on control 1?.

I have made a response.write : HttpContext.Current.Response.Write("<script language='javascript'>alert('" + str + "');</script>"); which is able to start an alert box on the client, this is only placed in the different events to see which is firing and which not.

When I click on this button the following steps are executed:

? protected override void OnInit(EventArgs e)
? protected override void CreateChildControls()
? private void Page_Load(object obj, EventArgs e)
? and finally: private void Control1_button_Click(object sender, EventArgs e)
? So far it works. in Control1_button_Click the event this.OnControlChange(ControlName.Control2); is executed, the purpose is that this event removes the ControlName.Control1 and replace this with ControlName.Control2.
? Now the button from the second usercontrol (control2.ascx) is loaded in the page.
? Next step: pressing on this second button the events OnInit, CreateChildControls, Page_Load are fired but NOT the click handler : private void Control2_button_Click(object sender, EventArgs e)
? in this handler show below in the script, the ?this.OnControlChange(ControlName.Control3);? must be executed, to place the third button on the page. But the Control2_Button_Click is never reached, so I get not the third button on the page but? &#61516; the first one. Maybe is there a simple solution, but after several week searching on the internet, and put many ours in it, without results.

I want by pressing the first button from the control1.ascx, see the second button from the usercontrol control2.ascx and by pressing this one I wanted to see the third button from usercontrol3.ascx.

I hope that there is an expert who can help me with this problem.


**************************************************
=========================================================
The scripts: simpleclass.cs
=========================================================

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public delegate void ControlChangeEventHandler(ControlName c);
public enum ControlName { Control1, Control2, Control3 };

public interface IControls
{
event ControlChangeEventHandler OnControlChange;
void SetControl(ControlName c);
}

namespace testplace
{
public class simpleclass : Page, IControls
{
protected PlaceHolder placeholder1;
public event ControlChangeEventHandler OnControlChange;
private Control control1;
private Control control2;
private Control control3;

public void SetControl(ControlName c)
{
for (int i = 0; i < placeholder1.Controls.Count; i++)
placeholder1.Controls.RemoveAt(i);

switch (c)
{
case ControlName.Control1:
placeholder1.Controls.Add(control1);
break;
case ControlName.Control2:
placeholder1.Controls.Add(control2);
break;
case ControlName.Control3:
placeholder1.Controls.Add(control3);
break;
}
}

protected override void CreateChildControls()
{
base.CreateChildControls ();

alert("CreateChildControls");

this.OnControlChange += new ControlChangeEventHandler(this.SetControl);

testplace.Control1 c1 = new testplace.Control1();
c1.button.Click += new EventHandler(Control1_button_Click);
this.control1 = c1.GetControl1;

testplace.Control2 c2 = new testplace.Control2();
c2.button.Click += new EventHandler(Control2_button_Click);
this.control2 = c2.GetControl2;

testplace.Control3 c3 = new testplace.Control3();
c3.button.Click += new EventHandler(Control3_button_Click);
this.control3 = c3.GetControl3;

placeholder1.Controls.Add(control1);

}

private void Page_Load(object obj, EventArgs e)
{
alert("Page_Load");
}

protected override void OnInit(EventArgs e)
{
this.Load += new EventHandler(Page_Load);
alert("OnInit");
base.OnInit (e);
}

private void alert(string str)
{
HttpContext.Current.Response.Write("<script language='javascript'>alert('" + str + "');</script>");
}

private void Control1_button_Click(object sender, EventArgs e)
{
alert("Control1_Button_Clicked");
this.OnControlChange(ControlName.Control2);
}

private void Control2_button_Click(object sender, EventArgs e)
{
alert("Control2_Button_Clicked");
this.OnControlChange(ControlName.Control3);
}

private void Control3_button_Click(object sender, EventArgs e)
{
alert("Control3_Button_Clicked");
}
}
}

=========================================================
The scripts: control1.cs
=========================================================

using System;
using System.Data;
using System.Drawing;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Web.Security;

namespace testplace
{
public class Control1 : Page, INamingContainer
{
private Control ctrl1;
public Button button;

public Control GetControl1
{
get { return ctrl1; }
}

public Control1()
{
ctrl1 = LoadControl("control1.ascx");
InitControl(ctrl1);
}

protected void InitControl(Control c)
{
button = (Button) c.FindControl("Button");
}
}
}

=========================================================
The scripts: control2.cs
=========================================================

using System;
using System.Data;
using System.Drawing;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Web.Security;

namespace testplace
{
public class Control2 : Page, INamingContainer
{
private Control ctrl2;
public Button button;

public Control GetControl2
{
get { return ctrl2; }
}

public Control2()
{
ctrl2 = LoadControl("control2.ascx");
InitControl(ctrl2);
}

protected void InitControl(Control c)
{
button = (Button) c.FindControl("Button");
}
}
}

=========================================================
The scripts: control3.cs
=========================================================

using System;
using System.Data;
using System.Drawing;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Web.Security;

namespace testplace
{
public class Control3 : Page, INamingContainer
{
private Control ctrl3;
public Button button;

public Control GetControl3
{
get { return ctrl3; }
}

public Control3()
{
ctrl3 = LoadControl("control3.ascx");
InitControl(ctrl3);
}

protected void InitControl(Control c)
{
button = (Button) c.FindControl("Button");
}
}
}

=========================================================
The scripts: control1.ascx
=========================================================
<asp:Button Runat="server" ID="Button" Text="Button on control 1" />
=========================================================
The scripts: control2.ascx
=========================================================
<asp:Button Runat="server" ID="Button" Text="Button on control 2" />
=========================================================
The scripts: control3.ascx
=========================================================
<asp:Button Runat="server" ID="Button" Text="Button on control 3" />



=========================================================
The scripts: default.aspx
=========================================================

<%@ Page language="c#" Codebehind="simpleclass.cs" AutoEventWireup="false" Inherits="testplace.simpleclass" %>
<html>
<body>
<form runat="server">
<asp:PlaceHolder Runat="server" ID="placeholder1"/>
</form>
</body>
</html>

Regards and Cheers,

Wernand Hoefsloot
The Netherlands
.NET Developer
1 Items, 1 Pages 1 |< << Go >> >|


Free Download:

Books:
Expert ASP.NET 2.0 Advanced Application Design: Advanced Application Design Authors: Dominic Selly, Andrew Troelsen, Tom Barnaby, Pages: 459, Published: 2005
Programming ASP.NET: Building Web Applications and Services Using C and VB.NET. Authors: Jesse Liberty, Dan Hurwitz, Pages: 988, Published: 2003

Web:
CodeProject: ASP.NET User Controls - Notify One Control Of Changes ... I have followed exactly as it has been described in the article... but it's the first time I did event handlers, so ... please help. ...
SA Developer .NET - Problem with firing events from dynamically ... i'm having a problem with one of my custom usercontrol that i'm loading at ... I have to click twise to fire up this event, please help ...
How to access form controls from a UserControl Button - .NET C# it. Hope anybody could help. BTW I'm a newbie ;) Thanks in advance. ... //Event Handler for the UserControl's custom event. ...
Silverlight Tutorial Part 6: Using User Controls to Implement ... We can implement the "CloseBtn_Click" event handler method in the ..... get the UserControl to work in a little more detail please check out my entry on the ...
RE: PreInit event in usercontrol If there're anything else we can help, please feel free to post here. ... For your scenario, I think the event handler problem may caused by ...
CodeProject: WebParticles: Developing and Using Web User Controls ... Using Web User Controls in SharePoint WebParts; Author: Tony Rabun, MCITP; Section: SharePoint ... In fact the event handler doesn't work. Pls help me asp. ...
TheMSsForum.com >> VB >> Returning gridview's selectedindexchanged ... Please help... many thanks Miguel Tag: Returning gridview's ..... Dynamic menu items and event handlers Hi, I am trying to dynamically generate a menu, ...
CodeProject: Understanding Publisher Subscriber model using user ... This article explains/describes publisher and subscriber event-handling model with the help of a user control.; Author: kodandapani, Kodanda Pani; ...
Google Answers: VB.NET new usercontrol events There are good examples in the MSDN documentation as well, and these make a great place to start for more advanced event handling topics. ...
SetTimer inside a UserControl questions [Archive] - VBForums dead in the water doing NADA. Any help or pointers? Thanks in advance .... These features allow event handling on transparent UserControls that was not ...




Search This Site:










randomly kicked to login page

3.0.12 error on site settings page

saving state information between server requests

dnn 4 modules

great idea

dnn update notifications

navigation with an access site-map provider

vb .net frames

ldap authentication

dnn "adult" website

mind-bending problem with class library function

installed/setup dnn * getting blank browser page

looking for dynamic button control...or so.

tell a friend

best skin designer

owc error, anyone help

administrator disconnected

installation error

treeview set font to bold

master page control code

page access

no ajax project templates in vs2005 on vista

impersonation failure going from 1.0 to 1.1

help with login page

which forum module?

apply theme with vb codefile.

altering variables from sql then displaying

allowdelete?

retrieve current node text from sitemapdatasource?

how to add a new module to dnn 212

 
All Times Are GMT