Calling a button click event from a menu item click

Last post 06-30-2009 1:04 PM by tsivinsky. 15 replies.

Sort Posts:

  • Calling a button click event from a menu item click

    03-17-2008, 12:30 PM
    • Member
      42 point Member
    • alan93
    • Member since 11-14-2006, 3:58 PM
    • Posts 42

    I'm trying to transition from MFC to this and its going none too smooth.

    I have this where the button calls a java script function. I want the menu item to do the same. This was easy in MFC.

        protected void Page_Load(object sender, EventArgs e)
        {
            Button1.Attributes.Add("OnClick", "return getFile();");
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            
        }
        protected void OnMenuItemClick(object sender, MenuEventArgs e)
        {
            
        }
     
  • Re: Calling a button click event from a menu item click

    03-17-2008, 1:05 PM
    • All-Star
      74,705 point All-Star
    • NC01
    • Member since 08-26-2005, 7:33 PM
    • Posts 13,868

    Handle the MenuItemDataBound event of the Menu and attach it in there. This link has a nice example showing adding an image URL to a menu item: http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.menueventargs.aspx

    NC...

  • Re: Calling a button click event from a menu item click

    03-17-2008, 1:41 PM
    • Member
      42 point Member
    • alan93
    • Member since 11-14-2006, 3:58 PM
    • Posts 42

    NC01:

    Handle the MenuItemDataBound event of the Menu and attach it in there. This link has a nice example showing adding an image URL to a menu item: http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.menueventargs.aspx

    NC...

    MenuItemDataBound- "Occurs when a menu item is bound to data. This event is commonly used to modify a menu item before it is rendered in a Menu control."

    Thanks for the response but I am missing something here.

    1.  I don't see wht this as to do with calling a button event from a menu item

    2. I don't see how putting an image on a menu item has anything to do with calling a button function from a menu item.

    Maybe if you could explain "it" and "there" more for me?

  • Re: Calling a button click event from a menu item click

    03-17-2008, 2:54 PM
    • All-Star
      74,705 point All-Star
    • NC01
    • Member since 08-26-2005, 7:33 PM
    • Posts 13,868

    Sure (not tested but this should work)

    protected void yourMenu_MenuItemDataBound(object sender, MenuEventArgs e)
    {
     // Add a client-side onclick event handler to the "Home" menu item...
     if ( e.Item.Text == "Home" )
     {
       e.Item.Attributes.Add("onclick", "return getFile();");
     }
    }

    NC...

  • Re: Calling a button click event from a menu item click

    03-17-2008, 3:03 PM
    • Member
      42 point Member
    • alan93
    • Member since 11-14-2006, 3:58 PM
    • Posts 42

    I'll try that

  • Re: Calling a button click event from a menu item click

    03-17-2008, 3:07 PM
    • Member
      42 point Member
    • alan93
    • Member since 11-14-2006, 3:58 PM
    • Posts 42

    Item does not have an Attributes prop.

  • Re: Calling a button click event from a menu item click

    03-17-2008, 3:19 PM
    • All-Star
      74,705 point All-Star
    • NC01
    • Member since 08-26-2005, 7:33 PM
    • Posts 13,868

    Well look here http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.menuitem.aspx Maybe you can find a property that will work for you.

    NC...

     

  • Re: Calling a button click event from a menu item click

    03-17-2008, 3:40 PM
    • Member
      42 point Member
    • alan93
    • Member since 11-14-2006, 3:58 PM
    • Posts 42

    looks like menu events and button events are meant to be forever separate.

  • Re: Calling a button click event from a menu item click

    03-19-2008, 11:22 AM
    • Member
      42 point Member
    • alan93
    • Member since 11-14-2006, 3:58 PM
    • Posts 42

    I'm trying to figure out why this doesn't work.

    button calls javascript menu item doesn't.

    <asp:Button ID="Button1" runat="server" Style="left: 437px; position: absolute; top: 258px"

    Text="Button" Width="69px" OnClick="Button1_Click" />

    parallel menu item code:

    <asp:Menu ID="Menu1" runat="server" onmenuitemclick="OnMenuItemClick" >

    with this:

        protected void Page_Load(object sender, EventArgs e)
        {
            Button1.Attributes.Add("OnClick", "return getFile();");
            Menu1.Attributes.Add("onmenuitemclick", "return getFile();");
     
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
           
        }
        protected void OnMenuItemClick(object sender, MenuEventArgs e)
        {
      
        }

  • Re: Calling a button click event from a menu item click

    03-19-2008, 11:38 AM
    • All-Star
      74,705 point All-Star
    • NC01
    • Member since 08-26-2005, 7:33 PM
    • Posts 13,868

    I just tried this which I copied from http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.menu.menuitemclick(VS.80).aspx

    <asp:menu id="Menu1"
       staticdisplaylevels="2"
       staticsubmenuindent="10"
       orientation="Vertical"
       onmenuitemclick="OnMenuItemClick"
       runat="server">

    protected void OnMenuItemClick(object sender, MenuEventArgs e)
    {
     this.Response.Write("Selected " + e.Item.Text + "<br>");
    }

    It does not look that much different from what you posted. Maybe you need to read up on the control and try the examples.
    See http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.menu(VS.80).aspx

    NC...

  • Re: Calling a button click event from a menu item click

    03-19-2008, 12:48 PM
    • Member
      42 point Member
    • alan93
    • Member since 11-14-2006, 3:58 PM
    • Posts 42

    Actually I have read quite a bit here

     http://quickstarts.asp.net/QuickStartv20/util/classbrowser.aspx?assembly=System.Web,%20Version=2.0.0.0,%20Culture=neutral,%20PublicKeyToken=b03f5f7f11d50a3a&namespace=System.Web.UI.WebControls&class=Button

     and here:

    http://quickstarts.asp.net/QuickStartv20/util/classbrowser.aspx?assembly=System.Web,%20Version=2.0.0.0,%20Culture=neutral,%20PublicKeyToken=b03f5f7f11d50a3a&namespace=System.Web.UI.WebControls&class=Menu

    Even so, your last post was totally irrelevant to my OP which was: 

    "I have this where the button calls a java script function. I want the menu item to do the same"

    So I am not trying to execute a menu item event, but call a javascript from the menu item the same way I do from the button. I had no idea ASP.NET was so limited in this regards and lacked this kind of flexibility.

    Developers should not have to spend so many days trying to figure out that something is nearly impossible that used to be so commonplace in the previous framework.

     

     

  • Re: Calling a button click event from a menu item click

    03-19-2008, 1:21 PM
    • All-Star
      74,705 point All-Star
    • NC01
    • Member since 08-26-2005, 7:33 PM
    • Posts 13,868

    alan93:

    Even so, your last post was totally irrelevant to my OP which was:

    "I have this where the button calls a java script function. I want the menu item to do the same"

    This is the second time that it sounds like you are getting angry with me for attempting to help you. Sorry if you can't do something that you want to do and I can't help you on it.

    alan93:

    So I am not trying to execute a menu item event, but call a javascript from the menu item the same way I do from the button. I had no idea ASP.NET was so limited in this regards and lacked this kind of flexibility.

    I guess that you can't do that, since a MenuItem does not seem to have anything to attach client-side event handlers to.

    alan93:

    Developers should not have to spend so many days trying to figure out that something is nearly impossible that used to be so commonplace in the previous framework.

    What previous Framework are you referring to? To my knowledge, this is the first that has a menu control built in. I know that version 1.1 did not. The beauty of ASP.NET is that you can build your own controls exactly to your specifications, so maybe you should construct your own since this one seems to fall short. Here is a one that might get you started: http://www.codeproject.com/KB/custom-controls/webmenu.aspx

    NC...

  • Re: Calling a button click event from a menu item click

    03-20-2008, 11:27 AM
    • Member
      42 point Member
    • alan93
    • Member since 11-14-2006, 3:58 PM
    • Posts 42

    NC01:

    alan93:

    Even so, your last post was totally irrelevant to my OP which was:

    "I have this where the button calls a java script function. I want the menu item to do the same"

    This is the second time that it sounds like you are getting angry with me for attempting to help you. Sorry if you can't do something that you want to do and I can't help you on it.

     Maybe if your responses were more succinctly on topic and you didn't suggest "people need to read more" , you would find yourself accusing people of being "angy" less often and would spend less time being defensive.

  • Re: Calling a button click event from a menu item click

    03-20-2008, 11:50 AM
    • All-Star
      74,705 point All-Star
    • NC01
    • Member since 08-26-2005, 7:33 PM
    • Posts 13,868

    And maybe if you would actually read the links posted you would not need to get angry.

    NC...

     

  • Re: Calling a button click event from a menu item click

    03-26-2008, 3:23 PM
    • Member
      2 point Member
    • amcevoy
    • Member since 03-26-2008, 6:59 PM
    • Posts 1

    This worked for us. Not sure if it will work for you, have a look and see what you think:

    Issue: Unable to trigger a trappable event with arguments from behind an ASP.Net 2.0 Menu control. Works fine on the top level menu items, but not on sub-items.

    Basically, we have a list of commands arranged in a menu structure. Each time a command is sent, a javascript confirm box needs to be displayed that subsequently either processes a click event on a button or not. I think that the begining and end scenarios tie up with what your looking for. It's VB.Net, but should be extensible to a language of your choice.

    Step one: Menu items

    To each item we added the following:

    Dim oMenuItem as New MenuItem

    oMenuItem.Text = "Whatever"

    oMenuItem.NavigateUrl = "javascript:validate('" & OutGoingCommand & "');"

    Step two: JavaScript Function on Page:

    function validate(zMessage)

    {

    // show confirm dialogue

    if (confirm('Are you sure...?'))

    {

    document.getElementById('cmdCommandButton').click();

    }

    }

    Step 3: Code behind

    Write whatever code you need in the _Click event of cmdCommandButton.

    Worked for us...

    Cheers,

    DS-9 Development Team.

Page 1 of 2 (16 items) 1 2 Next >