Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Which Button in a Template was clicked?

Which Button in a Template was clicked?

Scheduled Pinned Locked Moved ASP.NET
csharpjavascriptdatabasedesignsysadmin
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Pualee
    wrote on last edited by
    #1

    I am trying to populate a DataList with templated buttons mapping to categories in a DB. The buttons appear on the form exactly as desired, but I am having an issue determining which button was pressed (Buttons are not drawn individually in design mode, so do not have separate events). --Tried to insert ASP code here, forum wouldnt not seem to allow ASP code :( -- [asp:DataList ID="dlProductCategories" runat="server" Width="200px" EnableViewState="false" OnSelectedIndexChanged="dlProductCategories_SelectedIndexChanged" ] [ItemTemplate] [asp:Button ID="bttnCategory" runat="server" Width="175px" Text='[%# Eval("Name") %]' OnClick="bttnCategory_Click" CommandName='[%# Eval("Name") %]' CommandArgument='[%# Eval ("Name") %]'/] [br /] [/ItemTemplate] [HeaderTemplate] Select Product Category [/HeaderTemplate] [/asp:DataList] Now, I want to determine which button was pressed and fill a GridView with the appropriate category. I use the following event(s): protected void ButtonCategory_Click(object sender, EventArgs e) { } I suppose I could use the "DataListProductCategories_SelectedIndexChanged" instead, but either way, I cannot access the EventArgs e.CommandName or CommandArgument. :( I saw a tutorial where those were accessed in Javascript, but I want to work with those values in C#. The only public values for "sender" or "e" are Equals(), GetHashCode(), GetType(), ToString(). Is there a simple way to know which button was pressed?! Also, the button itself does not exist in the context of the ascx.cs file, just the click event. Pualee

    P M 2 Replies Last reply
    0
    • P Pualee

      I am trying to populate a DataList with templated buttons mapping to categories in a DB. The buttons appear on the form exactly as desired, but I am having an issue determining which button was pressed (Buttons are not drawn individually in design mode, so do not have separate events). --Tried to insert ASP code here, forum wouldnt not seem to allow ASP code :( -- [asp:DataList ID="dlProductCategories" runat="server" Width="200px" EnableViewState="false" OnSelectedIndexChanged="dlProductCategories_SelectedIndexChanged" ] [ItemTemplate] [asp:Button ID="bttnCategory" runat="server" Width="175px" Text='[%# Eval("Name") %]' OnClick="bttnCategory_Click" CommandName='[%# Eval("Name") %]' CommandArgument='[%# Eval ("Name") %]'/] [br /] [/ItemTemplate] [HeaderTemplate] Select Product Category [/HeaderTemplate] [/asp:DataList] Now, I want to determine which button was pressed and fill a GridView with the appropriate category. I use the following event(s): protected void ButtonCategory_Click(object sender, EventArgs e) { } I suppose I could use the "DataListProductCategories_SelectedIndexChanged" instead, but either way, I cannot access the EventArgs e.CommandName or CommandArgument. :( I saw a tutorial where those were accessed in Javascript, but I want to work with those values in C#. The only public values for "sender" or "e" are Equals(), GetHashCode(), GetType(), ToString(). Is there a simple way to know which button was pressed?! Also, the button itself does not exist in the context of the ascx.cs file, just the click event. Pualee

      P Offline
      P Offline
      Pualee
      wrote on last edited by
      #2

      Ah Ha! For anyone else with this problem: protected void bttnCategory_Click(object sender, EventArgs e) { System.Web.UI.WebControls.Button b = sender as System.Web.UI.WebControls.Button; if (b != null) { System.Windows.Forms.MessageBox.Show(b.Text); } } You can obviously do other things with the button at this point, but its easy now to know which button is pressed and act accordingly. Many thanks to this article: http://www.dotnetjunkies.com/Article/D481E737-BA40-472E-BEBD-E193FC68F8F2.dcik[^] -- modified at 12:09 Friday 17th February, 2006 I am still not sure how to interact with the "EventArgs e" parameter, but since my ASP code and underlying DB ensure that every button has a unique name, it is not an issue. However, if someone else uses the arguments parameteter, please feel free to share how it is used. Pualee

      1 Reply Last reply
      0
      • P Pualee

        I am trying to populate a DataList with templated buttons mapping to categories in a DB. The buttons appear on the form exactly as desired, but I am having an issue determining which button was pressed (Buttons are not drawn individually in design mode, so do not have separate events). --Tried to insert ASP code here, forum wouldnt not seem to allow ASP code :( -- [asp:DataList ID="dlProductCategories" runat="server" Width="200px" EnableViewState="false" OnSelectedIndexChanged="dlProductCategories_SelectedIndexChanged" ] [ItemTemplate] [asp:Button ID="bttnCategory" runat="server" Width="175px" Text='[%# Eval("Name") %]' OnClick="bttnCategory_Click" CommandName='[%# Eval("Name") %]' CommandArgument='[%# Eval ("Name") %]'/] [br /] [/ItemTemplate] [HeaderTemplate] Select Product Category [/HeaderTemplate] [/asp:DataList] Now, I want to determine which button was pressed and fill a GridView with the appropriate category. I use the following event(s): protected void ButtonCategory_Click(object sender, EventArgs e) { } I suppose I could use the "DataListProductCategories_SelectedIndexChanged" instead, but either way, I cannot access the EventArgs e.CommandName or CommandArgument. :( I saw a tutorial where those were accessed in Javascript, but I want to work with those values in C#. The only public values for "sender" or "e" are Equals(), GetHashCode(), GetType(), ToString(). Is there a simple way to know which button was pressed?! Also, the button itself does not exist in the context of the ascx.cs file, just the click event. Pualee

        M Offline
        M Offline
        Mike Ellison
        wrote on last edited by
        #3

        You don't need to set the onClick event for your buttons in the template. You do want to set the buttons' CommandName property though to a literal string (I think in your case, "Category" would make sense). When a button in your DataList's template is clicked, the parent DataList captures the child event and throws its own ItemCommand[^] event. You code your own ItemCommand event handler which can check the passed in DataListCommandEventArgs for the CommandName of the button clicked. Something like this:

        void MyItemCommandHandler(Object sender, DataListCommandEventArgs e)
        {
        if (e.CommandName == "Category")
        {
        // ...do something when 'Category' buttons are clicked...
        HandleCategoryButtonClick(e.CommandArgument)
        }
        }

        P 1 Reply Last reply
        0
        • M Mike Ellison

          You don't need to set the onClick event for your buttons in the template. You do want to set the buttons' CommandName property though to a literal string (I think in your case, "Category" would make sense). When a button in your DataList's template is clicked, the parent DataList captures the child event and throws its own ItemCommand[^] event. You code your own ItemCommand event handler which can check the passed in DataListCommandEventArgs for the CommandName of the button clicked. Something like this:

          void MyItemCommandHandler(Object sender, DataListCommandEventArgs e)
          {
          if (e.CommandName == "Category")
          {
          // ...do something when 'Category' buttons are clicked...
          HandleCategoryButtonClick(e.CommandArgument)
          }
          }

          P Offline
          P Offline
          Pualee
          wrote on last edited by
          #4

          I found a working solution before I saw your post, but in the interest of a more complete understanding... What you suggest is very similar to what I originally tried to do, however, I could never use e.CommandName or e.CommandArgument because "'System.EventArgs' does not contain a definition for 'CommandName'". Is it necessary to include more 'using' statements that I may have left out, or perhaps type case the EventArgs? If I can get it to work the way you mentioned, is their an advantage to your solution over the one I found (type casting the sender to button and using its text value)? Pualee

          M 1 Reply Last reply
          0
          • P Pualee

            I found a working solution before I saw your post, but in the interest of a more complete understanding... What you suggest is very similar to what I originally tried to do, however, I could never use e.CommandName or e.CommandArgument because "'System.EventArgs' does not contain a definition for 'CommandName'". Is it necessary to include more 'using' statements that I may have left out, or perhaps type case the EventArgs? If I can get it to work the way you mentioned, is their an advantage to your solution over the one I found (type casting the sender to button and using its text value)? Pualee

            M Offline
            M Offline
            Mike Ellison
            wrote on last edited by
            #5

            The important thing to realize with the post I made before, is that you are not coding directly for the templated button's Click event. You are instead coding for the DataList's ItemCommand event. The Click event handler's signature uses EventArgs, but the ItemCommand event handler's signature uses DataListCommandEventArgs, through which you'll get to the CommandName and CommandArgument properties. Does that make sense? The idea is that you would respond only to the events fired by the DataList, and not individual controls with the DataList's ItemTemplate.

            P 1 Reply Last reply
            0
            • M Mike Ellison

              The important thing to realize with the post I made before, is that you are not coding directly for the templated button's Click event. You are instead coding for the DataList's ItemCommand event. The Click event handler's signature uses EventArgs, but the ItemCommand event handler's signature uses DataListCommandEventArgs, through which you'll get to the CommandName and CommandArgument properties. Does that make sense? The idea is that you would respond only to the events fired by the DataList, and not individual controls with the DataList's ItemTemplate.

              P Offline
              P Offline
              Pualee
              wrote on last edited by
              #6

              Mike Ellison wrote:

              The Click event handler's signature uses EventArgs, but the ItemCommand event handler's signature uses DataListCommandEventArgs, through which you'll get to the CommandName and CommandArgument properties

              Ooooooooooh :omg::wtf::laugh: Pualee This forum really needs an "ah-ha" experience emote... i just use "omg wtf laugh"

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups