[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1081
  • Last Modified:

Get a reference back to the Repeater's e.Item.DataItem from within sub control's OnClick event

I have a repeater that contains an ImageButton in its ItemTemplate.

I've created a delegate to that ImageButton's OnClick event.

I'm not sure though how to reference the e.Item.DataItem of the current row of the repeater in my OnClick event.

Example:

 

    public void rptOrders_ItemDataBound(Object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            // Get a reference to the LinkButton that sits inside this repeater
            LinkButton lnkbtnOrder = (LinkButton)e.Item.FindControl("imgbtnOrder");

            // Create delegate for the ImageButton's OnClick event
            lnkbtnReOrder.OnClientClick += new ImageClickEventHandler(this.imgbtnOrder_OnClick);
        }
     }

Now here's thedelegate's  OnClick method where I want to handle stuff for the current roew in the repeater and acces it's DataItem:

    public void imgbtnOrder_OnClick(object sender, ImageClickEventArgs e)
    {
 
       //Get a reference here somehow back to the repeater's current e.Item.DataItem
    }

 

 

 
0
CoffeeAddict
Asked:
CoffeeAddict
  • 5
  • 4
1 Solution
 
moseackCommented:
Since you're playing with the databinding logic allready, I belive a nice solution will be to use "closures" (delegates with parameters):

public void rptOrders_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
	if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
	{
		// Get a reference to the LinkButton that sits inside this repeater
		LinkButton lnkbtnOrder = (LinkButton)e.Item.FindControl("imgbtnOrder");
 
		// Create delegate for the ImageButton's OnClick event
		lnkbtnOrder.Click +=
			delegate(object sender1, EventArgs e1)
			{
				object dataItem = e.Item.DataItem;
				imgbtnOrder_OnClick(sender1, e1, dataItem);
			};
	}
}
 
public void imgbtnOrder_OnClick(object sender, EventArgs e, object dataItem)
{
}

Open in new window

0
 
CoffeeAddictAuthor Commented:
Interesting, I'm just staring to get the hang of delegates.  Have not use closures yet so thanks.  I'll try that out.  Did not know you can pass basically delegate params in addition to just the params of the delegate method that it's expecting.
0
 
moseackCommented:
In my example "imgbtnOrder_OnClick" is just another method.
The delegate is declared anonymously in lines 10-14 (there it's signature is not flexible).
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
CoffeeAddictAuthor Commented:
Ah, I see.  So why would you use an anonymous delegate as opposed to an eventhandler listed for LinkButton object itself in this case?  Is it just another way to do it?  For example, why not use ImageClickEventHandler out of the box.  Just curious is all.

So you're just adding another param to the onclick method and passing in the DataItem.  I thougth about doing that.  But have not use anonymous delegates for this yet.  Cool.
0
 
moseackCommented:
Actually the method is anonymous, but the delegate type is "ImageClickEventHandler" (it has to be since C# is still strongly typed). The compiler knows how to infer the delegate type from the statement.

0
 
CoffeeAddictAuthor Commented:
I'm getting the following error during runtime expecting a certain type of  delegate

Error    4    No overload for 'imgbtnOrder_OnClick' matches delegate 'System.Web.UI.ImageClickEventHandler'

0
 
CoffeeAddictAuthor Commented:
here is my ImageLink definition in my mark-up:
<asp:ImageButton ID="imgbtnOrder" OnClick="imgbtnOrder_OnClick" runat="server" />
0
 
moseackCommented:
Remove the OnClick from the aspx, since you're attaching an event handler in code behind.
0
 
CoffeeAddictAuthor Commented:
Interesting.  Here's a way to do it without dealing with any of the ItemDataBound BS:

<asp:LinkButton id="linkbtnOrder" CommandArgument='<%#Eval("id")%>' OnClick="lnkbtnOrder_OnClick" ,,,, />

And then all i have to do is in my onclick event ignore the entire repeater events and just directly access the ID that was passed from that control:

protected void imgbtnOrder_OnClick(object sender, EventArgs e)
{
    ImageButton button = (ImageButton)sender;
    if(Int32.TryParse(button.CommandArgument, out id)
    {
        // now I have the ID passed
    }
}

I could then pass the ID to a method that would return the object I need.

This is just a different way.  While I'm not getting the object by casting initially, i'm obtaining the ID and thus bypassing all the pain in the ass dealings with the ItemDataBound of the repeater.  
0
 
rcouretCommented:
The Accepted solution does not work!  Would be awesome if it did.

e.Item.DataItem may be available while the delegate (anonymous method) is being defined, but is null when the anonymous method is actually called.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now