Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to catch a button event inside datagrid nested inside repeater 2

Status
Not open for further replies.

vatawna

Programmer
Feb 24, 2004
67
0
0
US
I have a datagrid nested inside a repeater. Does anyone know how to catch an event on an imagebutton inside datagrid?

Example:

<asp:Repeater>
<ItemTemplate>
<asp:DataGrid>
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:ImageButton>

Thanks.
 
You are going to have to write the handler for the button yourself. First you will have to use FindControl() to find the button, then use AddHandler(VB) to add the handler. I am not sure how it's done in C#.
 
same thing in C#, just different syntax. once you find the control you assign the handler to the event
Code:
var button = current.FindControl("Id of button") as Button;
if(button != null)
{
   button.Click += DoSomeWork;
}
where DoSomeWork looks like this
Code:
private void DoSomeWork(object o, EventArgs e)
{
     //do work here;
}
you could event inline this with a lamda.
Code:
button.Click += (o,e) => {do work here};

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
I tried both ways

1. In MyBase_Load
Dim btnViewer As New ImageButton
btnViewer = FindControl("btnViewer")
AddHandler btnViewer.Click, AddressOf ViewReport

2. In repeater_ItemDataBound
Dim btnViewer As New ImageButton
btnViewer = E.Item.FindControl("btnViewer")
AddHandler btnViewer.Click, AddressOf ViewReport

where E is RepeaterItemEventArgs

Both ways give me error "Object reference not set to an instance of an object"

Thanks
 
Since you have nested controls, you will have to find the datagrid first in the itemdatabound of the repeater. Then you need to find the button in the "found" grid. Make sense?
 
I tried that. It can't find the imagebutton. I even used GetEnumerator to loop through all controls within datagrid and output the names of the controls it found to see what it found - all the controls it found were datatable.

Example:

Dim NestedGrid As New DataGrid
NestedGrid = E.Item.FindControl("examGrid")

If Not NestedGrid Is Nothing Then
Dim Enu As System.Collections.IEnumerator = NestedGrid.Controls.GetEnumerator()

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top