Tutorial :ASP.NET: Access DataItem from an Event Handler



Question:

I have a data-bound, templated control, and inside the templated area I create a control with an event handler, like so:

<tnl:DisplayTree ID="DisplayTree1" runat="server" KeyPropertyName="Id"   ParentPropertyName="ParentDemographic" DataSourceID="DemographicObjectSource">  <ItemTemplate>      <asp:CheckBox ID="DemogSelector" runat="server" OnCheckedChanged="DemogSelector_OnCheckedChanged" /> &nbsp;      <asp:Label ID="InlineEditLabel" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "Name") %>'></asp:Label>      </ItemTemplate>  </tnl:DisplayTree>  

Within the event handler, I would like to be able to detect the Key of the item for which the control was created. For example:

protected void DemogSelector_OnCheckedChanged(object sender, EventArgs e)  {      CheckBox selector = (CheckBox)sender;      DisplayTree.TreeNode treeNode = (DisplayTree.TreeNode)selector.Parent.Parent.Parent.Parent;      Label1.Text += (int)treeNode.Key + ", ";  }  

As you can see, this approach requires close knowledge of the hierarchy within my DisplayTree.TreeNode class (i.e. I have to know that sender.Parent.Parent.Parent.Parent is where I'll find the DisplayTree.TreeNode object). I would like to make it a bit more robust, so that if my TreeNode hierarchy changes or something, I can access the key without difficulty. What's the best way for me to make this Key available from within the event handler?


Solution:1

Best way is to add a custom attribute to your checkbox

<asp:CheckBox ID="DemogSelector" runat="server" oncheckedchanged="DemogSelector_CheckedChanged" AutoPostBack="true" key='<%# Eval("Id") %>'/>  

And then access it using

string key = (sender as CheckBox).Attributes["key"];  


Solution:2

One potential way is to add an extension method to the checkbox, which finds the TreeNode for you, it could be implemented as some loop which recursively searches the parents until the TreeNode is found.

That way you just call selector.FindTreeNode(), of course this would fail on any checkbox called outside of your structure.


Solution:3

The best way is to create your own event arguments class that inherits from EventArgs and expose it as a property there. Since you're using a custom or third-party control, I don't have any advice on how to set the property on the event args. A custom events args would look like this:

public class TreeNodeCheckedChangedEventArgs : EventArgs  {      TreeNodeCheckedChangedEventArgs(int nodeKey)      {          NodeKey = nodeKey;      }        int NodeKey { get; private set; }    }  


Solution:4

You can set the value attribute on the checkbox

<asp:CheckBox ID="DemogSelector" runat="server" oncheckedchanged="DemogSelector_CheckedChanged" AutoPostBack="true" value='<%#DataBinder.Eval(Container.DataItem, "Id") %>' />  

And retrieve server side

string value = ((CheckBox)sender).Attributes["value"];  


Solution:5

I´m using kind of a similar solution as others, adding my code if it helps:

In markup:

    <ItemTemplate>          <input type="checkbox"             value="<%# Container.DataItemIndex %>" name="CheckedItems" />      </ItemTemplate>  

And in eventhandler:

string checkedItemRowIds = Request.Form["CheckedItems"];  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »