Link to home
Start Free TrialLog in
Avatar of spmcmorrow
spmcmorrow

asked on

Capturing commandname and commandargument from imagebutton created dynamically

Hi,
I have a class file which dynamicall builds a table that includes imagebuttons.  I then call the method from my webpage and load the table control as in the following (i'm only showing relative code here):

machines.cs:

public table table1(int id)
{
.....
imagebutton button[0] = new imagebutton();
button[0].commandname = "myCommandName"

.......

tablecell[0].controls.add(button[0]);
tablerow[0].controls.add(tablecell[0]);
table1.rows.add(tablerow[0]);

return table1;
}
----------------------------------------------------------------------------
webform1.aspx.cs:

ImageButtonCommand(object sender, CommandEventArgs e)
{
  if(e.commandname = "MyCommand")
  {
     // fire some event
  }
}

So the crux my issue is how do I get the imagebutton created in machines.cs to fire the ImageButtonCommand event handler when clicked?  I looked up examples where they recommended adding the OnCommand attribute to the imagebutton (and I used to do that in VB.NET 1.x) but in C# under the 2.0 framework, it seems like the OnCommand attribute is gone.
Thanks in advance.

Avatar of disentropy
disentropy

You need to use the c# equivalent of the following VB code.

AddHandler button[0].Command, AddressOf ImageButtonCommand

If the ImageButtonCommand is not accesible from where you are calling the function, you will need to pass the "AddressOf ImageButtonCommand" as 'MyEvent' (variable name) into it and then use that in your AddHandler code.

AddHandler button[0].Command, MyEvent
Avatar of spmcmorrow

ASKER

Could you list the c# equivalent?

I also am looking at something like:

button[0].click += New ButtonClickEventHandler(button[0]_click);

    void button[0]_Click(object sender, ImageClickEventArgs e)
    {
        throw new Exception("The method or operation is not implemented.");
     
    }

I placed a stop on the event handler and ran the debugger, and it still does not call the handler.

Thanks,
This should help. It's an example of a c# class that uses an event handler:

namespace MyClass
    {
        public delegate void MyEventHandler(EventArgs e);

        public class MyClass
        {
            // Declare the event to be raised.
            public event MyEventHandler MyEvent;
 
            public void RaiseMyEvent()
            {
                // Method to raise the event.
                MyEvent(EventArgs.Empty);
            }
        } //MyClass
    } //namespace

    public class Test
        public void TestEvent()
        {
            //
            // Create the object that raises the event you want to handle.
            //
            MyClass myobj = new MyClass();
            //
            // Associate the handler with the event.
            //
            myobj.MyEvent += new MyEventHandler(MyHandler);
            //
            // Tell the object to raise the event. It will be handled by sub MyHandler.
            //
            myObj.RaiseMyEvent()
            //
            // Dis-associate the handler from the event.
            //
            myobj.MyEvent -= new MyEventHandler(MyHandler);
       }

        public void MyHandler()
        {
            // Code to be executed when the event is raised.
            MessageBox.Show("I caught the event!");
        }
    }
The issue I'm having here, is that you're defining the event in the class that consumes the object.
I'm defining over 40 imagebuttons and placing them into a single table.  When I create an instance of the object in a new class, how do I drill down to the individual imagebuttons and associate the event with them.
Here's a snippet of code, perhaps you could modify it to show me what you mean:

machines.cs:
---------------

namespace PFS1
{
public class Machines
{

    ImageButton[] Slot = new ImageButton[44];

    public Table Table_43CountLarge(int planid)
    {
         // create the imagebuttons
        int counter = 1;
        while(counter<44)
        {
            Slot[counter] = new ImageButton();
            Slot[counter].ID = Convert.ToString(counter);
            Slot[counter].AlternateText = "Slot "+Convert.ToString(counter);
            Slot[counter].ToolTip = "Slot " + Convert.ToString(counter);
            counter++;
        }

        // define the table
        Table m_Table = new Table();
        TableRow m_Row = new TableRow();

.. sorry, clicked submit by accident

       

    * Home
          o All Topics
                + Programming
                      # Languages
                            * .NET
                                  o ASP.NET
                                        + Viewing a Question


Search 1,572,222 Solutions             
Search Help       Restrict to this Topic Area       Advanced Search
Title: Capturing commandname and commandargument from imagebutton created dynamically
asked by spmcmorrow on 04/28/2006 07:50AM PDT
This question is worth  250 Points
      

Hi,
I have a class file which dynamicall builds a table that includes imagebuttons.  I then call the method from my webpage and load the table control as in the following (i'm only showing relative code here):

machines.cs:

public table table1(int id)
{
.....
imagebutton button[0] = new imagebutton();
button[0].commandname = "myCommandName"

.......

tablecell[0].controls.add(button[0]);
tablerow[0].controls.add(tablecell[0]);
table1.rows.add(tablerow[0]);

return table1;
}
----------------------------------------------------------------------------
webform1.aspx.cs:

ImageButtonCommand(object sender, CommandEventArgs e)
{
  if(e.commandname = "MyCommand")
  {
     // fire some event
  }
}

So the crux my issue is how do I get the imagebutton created in machines.cs to fire the ImageButtonCommand event handler when clicked?  I looked up examples where they recommended adding the OnCommand attribute to the imagebutton (and I used to do that in VB.NET 1.x) but in C# under the 2.0 framework, it seems like the OnCommand attribute is gone.
Thanks in advance.


Send to a Friend    Printer Friendly       
            
      
Comment from disentropy
Date: 04/28/2006 08:04AM PDT
      Comment       Accept

You need to use the c# equivalent of the following VB code.

AddHandler button[0].Command, AddressOf ImageButtonCommand

If the ImageButtonCommand is not accesible from where you are calling the function, you will need to pass the "AddressOf ImageButtonCommand" as 'MyEvent' (variable name) into it and then use that in your AddHandler code.

AddHandler button[0].Command, MyEvent

Comment from spmcmorrow
Date: 04/28/2006 10:18AM PDT
      Your Comment       

Could you list the c# equivalent?

I also am looking at something like:

button[0].click += New ButtonClickEventHandler(button[0]_click);

    void button[0]_Click(object sender, ImageClickEventArgs e)
    {
        throw new Exception("The method or operation is not implemented.");
     
    }

I placed a stop on the event handler and ran the debugger, and it still does not call the handler.

Thanks,

Comment from disentropy
Date: 04/28/2006 10:28AM PDT
      Comment       Accept

This should help. It's an example of a c# class that uses an event handler:

namespace MyClass
    {
        public delegate void MyEventHandler(EventArgs e);

        public class MyClass
        {
            // Declare the event to be raised.
            public event MyEventHandler MyEvent;
 
            public void RaiseMyEvent()
            {
                // Method to raise the event.
                MyEvent(EventArgs.Empty);
            }
        } //MyClass
    } //namespace

    public class Test
        public void TestEvent()
        {
            //
            // Create the object that raises the event you want to handle.
            //
            MyClass myobj = new MyClass();
            //
            // Associate the handler with the event.
            //
            myobj.MyEvent += new MyEventHandler(MyHandler);
            //
            // Tell the object to raise the event. It will be handled by sub MyHandler.
            //
            myObj.RaiseMyEvent()
            //
            // Dis-associate the handler from the event.
            //
            myobj.MyEvent -= new MyEventHandler(MyHandler);
       }

        public void MyHandler()
        {
            // Code to be executed when the event is raised.
            MessageBox.Show("I caught the event!");
        }
    }

Comment from spmcmorrow
Date: 04/28/2006 11:13AM PDT
      Your Comment       

The issue I'm having here, is that you're defining the event in the class that consumes the object.
I'm defining over 40 imagebuttons and placing them into a single table.  When I create an instance of the object in a new class, how do I drill down to the individual imagebuttons and associate the event with them.
Here's a snippet of code, perhaps you could modify it to show me what you mean:

machines.cs:
---------------

namespace PFS1
{
public class Machines
{

    ImageButton[] Slot = new ImageButton[44];

    public Table Table_43CountLarge(int planid)
    {
         // create the imagebuttons
        int counter = 1;
        while(counter<44)
        {
            Slot[counter] = new ImageButton();
            Slot[counter].ID = Convert.ToString(counter);
            Slot[counter].AlternateText = "Slot "+Convert.ToString(counter);
            Slot[counter].ToolTip = "Slot " + Convert.ToString(counter);
            counter++;
        }

        // define the table
        Table m_Table = new Table();
        TableRow m_Row = new TableRow();
        TableCell m_Cell = new TableCell();

        // for the sake of brevity, imagine I defined 50 more rows, i'll only do one here

        m_Cell.Controls.Add(Slot[0]);
        m_Row.Cells.Add(m_Cell);
        m_Table.Rows.Add(m_Row);

        return m_Table;
}

// now for the webform class

webform1.aspx.cs
---------------------------

public partial class Planograms_Plan_Build : System.Web.UI.Page
{
   private PFS1.Machines m_objMachine = new PFS1.Machines();

   // I already have a panel control defined on this page called pnl_Machine:
protected void Page_Load(object sender, EventArgs e)
    {
       pnl_Machine.Controls.Add(class_machines.Table_43CountLarge(1));
    }
}

So the pageload calls up the table built in machines.cs with the 43 imagebuttons.  The object I'm instantiating is a table that contains rows, then cells, then imagebuttons.  How do I associate the event handlers?

Thanks,
Jon
// let's try this one more time:

The issue I'm having here, is that you're defining the event in the class that consumes the object.
I'm defining over 40 imagebuttons and placing them into a single table.  When I create an instance of the object in a new class, how do I drill down to the individual imagebuttons and associate the event with them.
Here's a snippet of code, perhaps you could modify it to show me what you mean:

machines.cs:
---------------

namespace PFS1
{
public class Machines
{

    ImageButton[] Slot = new ImageButton[44];

    public Table Table_43CountLarge(int planid)
    {
         // create the imagebuttons
        int counter = 1;
        while(counter<44)
        {
            Slot[counter] = new ImageButton();
            Slot[counter].ID = Convert.ToString(counter);
            Slot[counter].AlternateText = "Slot "+Convert.ToString(counter);
            Slot[counter].ToolTip = "Slot " + Convert.ToString(counter);
            counter++;
        }

        // define the table
        Table m_Table = new Table();
        TableRow m_Row = new TableRow();
        TableCell m_Cell = new TableCell();

        // for the sake of brevity, imagine I defined 50 more rows, i'll only do one here

        m_Cell.Controls.Add(Slot[0]);
        m_Row.Cells.Add(m_Cell);
        m_Table.Rows.Add(m_Row);

        return m_Table;
}

// now for the webform class

webform1.aspx.cs
---------------------------

public partial class Planograms_Plan_Build : System.Web.UI.Page
{
   private PFS1.Machines m_objMachine = new PFS1.Machines();

   // I already have a panel control defined on this page called pnl_Machine:
protected void Page_Load(object sender, EventArgs e)
    {
       pnl_Machine.Controls.Add(class_machines.Table_43CountLarge(1));
    }
}

So the pageload calls up the table built in machines.cs with the 43 imagebuttons.  The object I'm instantiating is a table that contains rows, then cells, then imagebuttons.  How do I associate the event handlers?

Thanks,
Jon
Maybe someone else can help out with the C# code, but the just of it is that you need to

1)  pass the address of the event handler into you Table_43CountLarge event assign the command name and command argument for each imagebutton

2) create the event handler in your page and the code to handle the command arguments passed in.

namespace PFS1
{
public class Machines
{

    ImageButton[] Slot = new ImageButton[44];

    public Table Table_43CountLarge(int planid, **pass in event handler**)
    {
         // create the imagebuttons
        int counter = 1;
        while(counter<44)
        {
            Slot[counter] = new ImageButton();
            Slot[counter].ID = Convert.ToString(counter);
            Slot[counter].AlternateText = "Slot "+Convert.ToString(counter);
            Slot[counter].ToolTip = "Slot " + Convert.ToString(counter);
**set your command arguments
            Slot[counter].Command += new CommandEventHandler(MyEvent);
            Slot[counter].commandname = "myCommandName"
            Slot[counter].commandargument = Convert.ToString(planid) + "_" + Convert.ToString(counter);
**end set commmand arguments
            counter++;
        }

        // define the table
        Table m_Table = new Table();
        TableRow m_Row = new TableRow();
        TableCell m_Cell = new TableCell();

        // for the sake of brevity, imagine I defined 50 more rows, i'll only do one here

        m_Cell.Controls.Add(Slot[0]);
        m_Row.Cells.Add(m_Cell);
        m_Table.Rows.Add(m_Row);

        return m_Table;
}

// now for the webform class

webform1.aspx.cs
---------------------------

public partial class Planograms_Plan_Build : System.Web.UI.Page
{
   private PFS1.Machines m_objMachine = new PFS1.Machines();

   // I already have a panel control defined on this page called pnl_Machine:
protected void Page_Load(object sender, EventArgs e)
    {
       pnl_Machine.Controls.Add(class_machines.Table_43CountLarge(1));
    }
}

**Put this back in your page.
ImageButtonCommand(object sender, CommandEventArgs e)
{
  if(e.commandname = "MyCommand")
  {
     // fire some event
  }
}
Thanks,
Where you have ** pass in event handler ** what might that look like?

I tried compiling it as above and got an error that the event handler on machine.cs does not exist in the current context (because it's on webform1.aspx.cs, and presumably because I'm not passing in the event handler)?
ASKER CERTIFIED SOLUTION
Avatar of disentropy
disentropy

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial