2 Replies - 2036 Views - Last Post: 03 August 2011 - 05:55 AM Rate Topic: -----

#1 tehwabbit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 18-November 10

C# Adding events to a dynamically created control

Posted 03 August 2011 - 02:48 AM

Hi,


I am currently creating a dynamic menu in which the menuItems either have a URL or a command.

private string parseMenuLink(XmlNode menuItem)
    {
        string rc = "";
        if (menuItem["commandname"] != null)
        {
        
        rc = "<a onclick=\"MenuClicked" + menuItem["commandname"].InnerText + "')\">" + menuItem["text"].InnerText + "</a>";
        else if (menuItem["url"] != null)
        {
            rc = "<a href=\"" + menuItem["url"].InnerText + "\">" + menuItem["text"].InnerText + "</a>";
        }
        else
        {

            rc = menuItem["text"].InnerText;
        }




This works fine. However if there is command I will need an event handler to parse the command upon clicking.

I have made several attempts at this, most the time nothing happens and the other time, the first command launched straight away. Below is the current code I am using. I know it's wrong, but I'm basing it loosely around a tutorial and partly around my Java knowledge.

Here are the two fields I am setting:
public delegate void MenuItemClickEventHandler(object sender, MenuItemClickEventArgs e);

public event MenuItemClickEventHandler MenuClicked;




below is the snippet I am using for event handling, but not sure where to place it,tried it in a few places and it doesn't work as expected.
if (this.MenuClicked != null)
            {
            MenuItemClickEventArgs args = new MenuItemClickEventArgs();
            args.Command = menuItem["commandname"].InnerText;

            
            this.ImagoLauncher_MenuClicked(this, args);
            }




here is my event args class
 public class MenuItemClickEventArgs : EventArgs
    {
        // this is a string value I will set using a dropdownlist
        public string Command { get; set; }

    }



If it's a command, it will have to be parsed by my parse command method:

private void menu_MenuClicked(object sender, MenuItemClickEventArgs e)
    {
        
        if (e.Command == "login")
        {
            //other stuff
            Response.Redirect("login.aspx");
        }

        ...



I think I'm kind of on the right lines???? If all else fails, I will try a new approach, but would be nice to get this working first (Or at least try)

Is This A Good Question/Topic? 0
  • +

Replies To: C# Adding events to a dynamically created control

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: C# Adding events to a dynamically created control

Posted 03 August 2011 - 04:57 AM

I am going to tell you the truth. Dynamic controls are a pain in the ass in ASP.Net just because of the way the internet works(being stateless).

Looks like you are trying to create a menu from an XML file? If that is correct, you can try out this tutorial. It will show you how to create an ASP.Net menu using an XML file.
Was This Post Helpful? 0
  • +
  • -

#3 tehwabbit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 18-November 10

Re: C# Adding events to a dynamically created control

Posted 03 August 2011 - 05:55 AM

Yes, I am creating a menu via an XML file, but due to the complexity of the generated XML, I already have a method for that.

If I set the ID of the <a href> to the CommandName from XML, would it be possible to use this ID to parse into
private void menu_MenuClicked(object sender, MenuItemClickEventArgs e){
if (e.Command == "foo")
        {
            
            Response.Redirect("blah.aspx");
..othercode
        }
    



Short of setting the method up as a method and not event handler, I'm not sure how I would go about it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1