RichardFox
asked on
IndexChanged event handler as class method
I am trying to use an index changed method within a class as follows:
WebForm1.aspx:
<%@ Page language="c#" Codebehind="WebForm2.aspx. cs" AutoEventWireup="false" Inherits="DMSInventory.Web Form2" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm2</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScri pt" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:Panel id="Panel1" runat="server"></asp:Panel >
</form>
</body>
</HTML>
WebForm1.aspx.cs:
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls. Panel Panel1;
private void Page_Load(object sender, System.EventArgs e)
{
ControlSet1 myControls = new ControlSet1(Panel1);
}
}
And my ControlSet1 class which creates some controls on the panel:
public class ControlSet1
{
public System.Web.UI.WebControls. ListBox myLB;
public System.Web.UI.WebControls. Label myLAB;
public System.Web.UI.WebControls. TableRow myTR;
public System.Web.UI.WebControls. Table myTAB;
public ControlSet1(System.Web.UI. WebControl s.Panel mypanel)
{
myTAB = new Table();
TableCell tc = new TableCell();
TableRow tr = new TableRow();
myLB = new ListBox();
myLB.Items.Add("OFF");
myLB.Items.Add("ON");
tc.Controls.Add(myLB);
tr.Cells.Add(tc);
myTAB.Rows.Add(tr);
tc = new TableCell();
myTR = new TableRow();
myLAB = new Label();
myLAB.Text = "This text label should be toggled off and on";
tc.Controls.Add(myLAB);
myTR.Cells.Add(tc);
myTAB.Rows.Add(myTR);
mypanel.Controls.Add(myTAB );
this.myLB.SelectedIndexCha nged
+= new System.EventHandler(this.m yLB_Select edIndexCha nged);
}
private void myLB_SelectedIndexChanged( object sender, System.EventArgs e)
{
if (myLB.SelectedItem.Value == "ON")
myLAB.Visible = true;
else
myLAB.Visible = false;
}
}
Can someone tell me why the myLB_SelectedIndexChanged( ) function is not working? The compiler did not complain about the IndexChanged() function being a class method so I assume that is ok...
WebForm1.aspx:
<%@ Page language="c#" Codebehind="WebForm2.aspx.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm2</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScri
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:Panel id="Panel1" runat="server"></asp:Panel
</form>
</body>
</HTML>
WebForm1.aspx.cs:
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.
private void Page_Load(object sender, System.EventArgs e)
{
ControlSet1 myControls = new ControlSet1(Panel1);
}
}
And my ControlSet1 class which creates some controls on the panel:
public class ControlSet1
{
public System.Web.UI.WebControls.
public System.Web.UI.WebControls.
public System.Web.UI.WebControls.
public System.Web.UI.WebControls.
public ControlSet1(System.Web.UI.
{
myTAB = new Table();
TableCell tc = new TableCell();
TableRow tr = new TableRow();
myLB = new ListBox();
myLB.Items.Add("OFF");
myLB.Items.Add("ON");
tc.Controls.Add(myLB);
tr.Cells.Add(tc);
myTAB.Rows.Add(tr);
tc = new TableCell();
myTR = new TableRow();
myLAB = new Label();
myLAB.Text = "This text label should be toggled off and on";
tc.Controls.Add(myLAB);
myTR.Cells.Add(tc);
myTAB.Rows.Add(myTR);
mypanel.Controls.Add(myTAB
this.myLB.SelectedIndexCha
+= new System.EventHandler(this.m
}
private void myLB_SelectedIndexChanged(
{
if (myLB.SelectedItem.Value == "ON")
myLAB.Visible = true;
else
myLAB.Visible = false;
}
}
Can someone tell me why the myLB_SelectedIndexChanged(
Hi RichardFox,
i think the problem is that when you call
this.myLB.SelectedIndexCha nged
+= new System.EventHandler(this.m yLB_Select edIndexCha nged);
this means the class it self, but not the page
try to pass as a property to the class the page and then assighn to this property Page the event handler
B..M
i think the problem is that when you call
this.myLB.SelectedIndexCha
+= new System.EventHandler(this.m
this means the class it self, but not the page
try to pass as a property to the class the page and then assighn to this property Page the event handler
B..M
ASKER
B..M, this doesn't work, because the class object is not instantiated and I am trying to refer to it inside the constructor. In other words, if I use:
public class WebForm2 : System.Web.UI.Page
{
protected System.Web.UI.WebControls. Panel Panel1;
public ControlSet1 myControls;
private void Page_Load(object sender, System.EventArgs e)
{
myControls = new ControlSet1(Panel1,this);
}
}
and
public class ControlSet1
{
public System.Web.UI.WebControls. ListBox myLB;
public ControlSet1(System.Web.UI. WebControl s.Panel mypanel, WebForm2 pg)
{
pg.myControls.myLB.Selecte dIndexChan ged
+= new System.EventHandler(pg.myC ontrols.my LB_Selecte dIndexChan ged);
}
public void myLB_SelectedIndexChanged( object sender, System.EventArgs e)
{...
}
}
myControls is not initialized yet, I am still inside the constructor.
I am trying to figure a workaround and put the control which I want an event handler on, my listbox, on the main page. This is not ideal because I was trying to keep a set of controls and code related to those controls inside a single class, but I guess it's not possible.
public class WebForm2 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.
public ControlSet1 myControls;
private void Page_Load(object sender, System.EventArgs e)
{
myControls = new ControlSet1(Panel1,this);
}
}
and
public class ControlSet1
{
public System.Web.UI.WebControls.
public ControlSet1(System.Web.UI.
{
pg.myControls.myLB.Selecte
+= new System.EventHandler(pg.myC
}
public void myLB_SelectedIndexChanged(
{...
}
}
myControls is not initialized yet, I am still inside the constructor.
I am trying to figure a workaround and put the control which I want an event handler on, my listbox, on the main page. This is not ideal because I was trying to keep a set of controls and code related to those controls inside a single class, but I guess it's not possible.
RichardFox,
No, I mean to use
public ControlSet1(System.Web.UI. WebControl s.Panel mypanel, System.Web.UI.Page pg)
B..M
No, I mean to use
public ControlSet1(System.Web.UI.
B..M
ASKER
But the
System.Web.UI.Page pg
object does not know about the controls inside of my ControlSet1 class, and I have to add an event handler with the format:
(Page).(Control).SelectInd exChanged
+= new System.EventHandler((Page) .(Control) .(Control) _SelectedI ndexChange d);
So if I just pass System.Web.UI.Page to the ControlSet1 constructor, this object does not see my class control members.
System.Web.UI.Page pg
object does not know about the controls inside of my ControlSet1 class, and I have to add an event handler with the format:
(Page).(Control).SelectInd
+= new System.EventHandler((Page)
So if I just pass System.Web.UI.Page to the ControlSet1 constructor, this object does not see my class control members.
ASKER
If you can get a simple example working, then you will amaze me. I am beginning to suspect it is impossible.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It works! You're good.
Thanks - Rich
Thanks - Rich
I donm't think that you can call an event handler function from a class like that.. you can call a class function from within the event handler. But the event handler needs to be defined and attached to the control in the main WebForm itself.
So attach the delegate like 'myLB_SelectedIndexChanged
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.myLB.SelectedIndexCha
}
private void myLB_SelectedIndexChangedM
{
// Call your class function here
}