This is a migrated thread and some comments may be shown as answers.

Find Control in code behind file & Add click event

3 Answers 546 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Awadh .
Top achievements
Rank 1
Awadh . asked on 10 Nov 2009, 09:47 AM
<rad:GridButtonColumn ButtonType="ImageButton" ImageUrl="../Images/Recreate.GIF" UniqueName="RecreateColumn"
                                         CommandName="Recreate">
                                        <HeaderStyle Font-Bold="True" Wrap="False"/>
                                        <ItemStyle Wrap="true" />
                                    </rad:GridButtonColumn> 


This is piece of code whose image buttons i need to find in code behind .cs file and need to add click event on this.

How can i do this ?


Thanks,
Awadh

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 10 Nov 2009, 10:48 AM
Hi Awadh,

You can attach ItemCommand event and check for the CommandName whether it is "Recreate" if you want to execute any server code on clicking the imagebutton. Here is that code for that.

CS:
 
    protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e) 
    { 
        if (e.CommandName == "Recreate"
        { 
            Response.Write("Recreate"); 
        } 
    } 

The following code shows how to attach clientside onclick event for imagebutton.

CS:
 
    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e) 
    { 
        if (e.Item is GridDataItem) 
        { 
            GridDataItem item = (GridDataItem)e.Item; 
            ImageButton imgButton = (ImageButton)item["RecreateColumn"].Controls[0]; 
            imgButton.Attributes.Add("onclick""clickEvent("+ item.ItemIndex +")"); // Also passing the index to event handler 
        } 
    } 

JavaScript:
 
<script type="text/javascript"
function clickEvent(index) 
    alert(index); 
</script> 

Regards,
Shinu.
0
Awadh .
Top achievements
Rank 1
answered on 10 Nov 2009, 02:25 PM

Thanks Shinu for ur reply...

Now i am able to find the control and make enable/disable.
BUT i need to write some code in .cs file on the click event of the control, not in the javascript. So how to do this in code behind class ?


Thanks,
Awadh

 

 

0
Princy
Top achievements
Rank 2
answered on 11 Nov 2009, 07:05 AM
Hello Awadh,

You can either use the ItemCommand event of the grid which will fire on the clicking the button as suggested by Shinu in the previous code:
c#:
 protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e) 
    { 
        if (e.CommandName == "Recreate"
        { 
            // which also fires on clicking the button 
        } 
    }  

Or you can add an event handler to the for the button as shown below:
c#:
 protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e) 
    { 
        if (e.Item is GridDataItem) 
        { 
            GridDataItem item = (GridDataItem)e.Item;            
            ImageButton imgButton = (ImageButton)item["RecreateColumn"].Controls[0]; 
            imgButton.Click += new ImageClickEventHandler(imgButton_Click);  
        } 
    } 
 
    void imgButton_Click(object sender, ImageClickEventArgs e) 
    { 
         
    } 

Thanks
Princy.
Tags
Grid
Asked by
Awadh .
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Awadh .
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Share this question
or