Solved

IndexChanged event handler as class method

Posted on 2004-08-12
8
243 Views
Last Modified: 2008-01-09
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.WebForm2" %>
<!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_defaultClientScript" content="JavaScript">
            <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
      </HEAD>
      <body>
            <form id="Form1" method="post" runat="server">
                  &nbsp;
                  <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.WebControls.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.SelectedIndexChanged
                        += new System.EventHandler(this.myLB_SelectedIndexChanged);
            }

            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...
0
Comment
Question by:RichardFox
  • 4
  • 3
8 Comments
 
LVL 8

Expert Comment

by:daffodils
ID: 11787694
Move the portion where you attach the delegate to Page Initialization function .. something like the InitializeComponent() function in Visual Studio 2003. And also the function to the 'myLB_SelectedIndexChanged' to WebForm2.aspx.cs..


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_SelectedIndexChangedMain' and call your class function from within that
override protected void OnInit(EventArgs e)
{
      InitializeComponent();
      base.OnInit(e);
}

private void InitializeComponent()
{  
              this.myLB.SelectedIndexChanged += new System.EventHandler(this.myLB_SelectedIndexChangedMain);
}

private void myLB_SelectedIndexChangedMain(object sender, System.EventArgs e)
{
           // Call your class function here
}
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 11788203
Hi RichardFox,

i think the problem is that when you call
this.myLB.SelectedIndexChanged
                    += new System.EventHandler(this.myLB_SelectedIndexChanged);

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
0
 

Author Comment

by:RichardFox
ID: 11788382
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.WebControls.Panel mypanel, WebForm2 pg)
            {

                 pg.myControls.myLB.SelectedIndexChanged
                  += new System.EventHandler(pg.myControls.myLB_SelectedIndexChanged);
                                }

            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.
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 11788404
RichardFox,

No, I mean to use

public ControlSet1(System.Web.UI.WebControls.Panel mypanel, System.Web.UI.Page pg)


B..M
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:RichardFox
ID: 11788442
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).SelectIndexChanged
   += new System.EventHandler((Page).(Control).(Control)_SelectedIndexChanged);

So if I just pass System.Web.UI.Page  to the ControlSet1 constructor, this object does not see my class control members.
0
 

Author Comment

by:RichardFox
ID: 11788451
If you can get a simple example working, then you will amaze me. I am beginning to suspect it is impossible.
0
 
LVL 28

Accepted Solution

by:
mmarinov earned 500 total points
ID: 11788540
RichardFox,

here is a working example:
    public class CustomDDL : 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);        
        }
    }

    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.WebControls.Panel mypanel)
        {

            myTAB = new Table();
            TableCell tc = new TableCell();
            TableRow tr = new TableRow();
            myLB = new ListBox();
            myLB.Items.Add("OFF");
            myLB.Items.Add("ON");
            myLB.AutoPostBack = true;
            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);

            myLB.SelectedIndexChanged
                += new System.EventHandler(myLB_SelectedIndexChanged);
        }

        private void myLB_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            if (myLB.SelectedItem.Value == "ON")
                myLAB.Visible = true;
            else
                myLAB.Visible = false;
        }
    }

the change is that i set the event handler directly to the myLB control and set the AutoPostBack = true so the .net knows that will have to submit the page when the index is change

Regards
B..M
0
 

Author Comment

by:RichardFox
ID: 11788610
It works! You're good.

Thanks - Rich
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Hangfire / asp.net sample 3 50
Syntax error 9 45
Server Error 11 47
Javascript alert is skipped before a Response.Redirect is executed in vb.net asp.net 9 26
I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 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

20 Experts available now in Live!

Get 1:1 Help Now