Which Button in a Template was clicked?
-
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 -
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. PualeeAh 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 -
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. PualeeYou 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 ownItemCommand
event handler which can check the passed inDataListCommandEventArgs
for theCommandName
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)
}
} -
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 ownItemCommand
event handler which can check the passed inDataListCommandEventArgs
for theCommandName
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)
}
}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
-
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
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'sItemCommand
event. The Click event handler's signature usesEventArgs
, but the ItemCommand event handler's signature usesDataListCommandEventArgs
, through which you'll get to theCommandName
andCommandArgument
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. -
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'sItemCommand
event. The Click event handler's signature usesEventArgs
, but the ItemCommand event handler's signature usesDataListCommandEventArgs
, through which you'll get to theCommandName
andCommandArgument
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.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"