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

Assigning properties to an image button when a button outside of the grid is clicked

6 Answers 148 Views
Grid
This is a migrated thread and some comments may be shown as answers.
William
Top achievements
Rank 1
William asked on 02 Mar 2009, 02:34 PM
 Protected Sub ButtonAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)  
 
 Session("employeeIsBeingAdded") = True  
 End Sub  
 
Protected Sub BureausGrid_ItemCreated(ByVal sender As Object, ByVal e As GridItemEventArgs) Handles BureausGrid.ItemCreated  
 
If Session("bureauIsBeingSavedOrAdded") = True Then  
                If TypeOf e.Item Is GridDataItem Then  
 
                    Dim dataitem As GridDataItem = CType(e.Item, GridDataItem)  
                    Dim viewButton As ImageButton = CType(dataitem("viewColumn").Controls(0), ImageButton)  
   
                  viewButton.Attributes("onclick") = "return confirm('Quit editing this employee?')"  
           
                End If  
End Sub 
I have an image button that I need to assign an onclick property to when a button outside of the grid is clicked. I was going to use the ItemCreated event and determine whether to assign the property based on a session variable that gets assigned when the button outside the grid is clicked, but the ItemCreated event is executed before the handler for the button, so the session variable is not read. I was wondering if anyone had any thoughts about getting around this?

Thanks

6 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 03 Mar 2009, 06:28 AM

Hi,

Try the following code snippet to achieve the desired scenario.

ASPX:
 
<telerik:GridTemplateColumn UniqueName="viewColumn" > 
                      <ItemTemplate> 
                          <asp:ImageButton ID="ImageButton1" ImageUrl="~/Images/Image1.gif" runat="server" /> 
                      </ItemTemplate> 
                    </telerik:GridTemplateColumn> 

VB:
    Protected Sub Button2_Click(ByVal sender As ObjectByVal e As EventArgs)  
         For Each item As GridDataItem In RadGrid1.MasterTableView.Items  
             Dim viewButton As ImageButton = DirectCast(item("viewColumn").FindControl("ImageButton1"), ImageButton)  
             viewButton.Attributes.Add("OnClick""return confirm('Quit editing this employee?')")  
              
        Next  
     End Sub 
 

Thanks
Shinu




0
William
Top achievements
Rank 1
answered on 03 Mar 2009, 02:01 PM

Thanks a lot Shinu. I'm creating the grid dynamically in the page load and I'm not sure if I can do this with a template column. Is there a way to locate the image button if it's not in a template column?

 

Thanks

 

0
Shinu
Top achievements
Rank 2
answered on 04 Mar 2009, 04:11 AM
Hi William,

I suppose you are using a GridButtonColumn(ButtonType = ImageButton). If so you can use a similar approach as shown below.

VB:
 
Protected
 Sub Button2_Click(ByVal sender As ObjectByVal e As EventArgs) 
     For Each item As GridDataItem In RadGrid1.MasterTableView.Items 
         Dim viewButton As ImageButton = DirectCast(item("viewColumn").Controls(0), ImageButton) 
         viewButton.Attributes.Add("OnClick""return confirm('Quit editing this employee?')"
     Next 
     
 End Sub 

If this does not help consider sending your code behind.

Regards
Shinu
0
William
Top achievements
Rank 1
answered on 04 Mar 2009, 09:27 PM
Thanks Shinu. Unfortunatley I'm getting an object reference not set to an instance of an object error at

For

 

Each item As GridDataItem In BureausGrid.MasterTableView.Items

This is the code behind related to the button column in the creation of the grid.

Thanks again

 

Protected Sub CreateGrids()  
 
Dim BureausGrid As RadGrid = New RadGrid  
BureausGrid.DataSourceID = "BureausDataSource" 
BureausGrid.ID = "BureausGrid" 
BureausGrid.AutoGenerateColumns = False 
 
Dim buttonColumn1 As GridButtonColumn = New GridButtonColumn  
BureausGrid.MasterTableView.Columns.Add(buttonColumn1)  
 
buttonColumn1.ButtonType = GridButtonColumnType.ImageButton  
buttonColumn1.ImageUrl = "~/DesktopModules/......." 
buttonColumn1.HeaderText = "View" 
buttonColumn1.Text = "View" 
buttonColumn1.CommandName = "ViewBureau" 
buttonColumn1.UniqueName = "viewColumn" 
Me.PlaceHolder2.Controls.Add(BureausGrid)  
 
End sub  
 

0
Accepted
Shinu
Top achievements
Rank 2
answered on 05 Mar 2009, 06:37 AM
Hi William,

I created Grid dynamically in the same manner as you did. I implemented the code given below to assign properties to the Imagebutton and it is working fine. I have pasted the entire code here. Just give a try with that and see if it helps.

ASPX:
 <asp:ScriptManager ID="ScriptManager1" runat="server"
        </asp:ScriptManager> 
        
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthWindConnectionString %>" 
            SelectCommand="SELECT [ProductID], [ProductName], [SupplierID] FROM [Alphabetical list of products]"
        </asp:SqlDataSource> 
        <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder> 
        <br /> 
        <br /> 
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /> 


VB:
 
 
     Protected Sub Page_Init(ByVal sender As ObjectByVal e As System.EventArgs) 
         CreateGrids() 
         
     End Sub 
     
     Private Sub CreateGrids() 
         Dim BureausGrid As New RadGrid() 
         BureausGrid.DataSourceID = "SqlDataSource1" 
         BureausGrid.ID = "BureausGrid" 
        BureausGrid.AutoGenerateColumns = False 
         
        
         
         Dim buttonColumn1 As New GridButtonColumn() 
         BureausGrid.MasterTableView.Columns.Add(buttonColumn1) 
         
         buttonColumn1.ButtonType = GridButtonColumnType.ImageButton 
         buttonColumn1.ImageUrl = "~/RadControls/Skins/Hay/Grid/AddRecord.gif" 
         buttonColumn1.HeaderText = "View" 
         buttonColumn1.Text = "View" 
         buttonColumn1.CommandName = "ViewBureau" 
         buttonColumn1.UniqueName = "viewColumn" 
         Me.PlaceHolder1.Controls.Add(BureausGrid) 
         
     End Sub 
     
     Protected Sub Button1_Click(ByVal sender As ObjectByVal e As EventArgs) 
         Dim BureausGrid As RadGrid = DirectCast(PlaceHolder1.FindControl("BureausGrid"), RadGrid) 
         If BureausGrid IsNot Nothing Then 
             For Each item As GridDataItem In BureausGrid.MasterTableView.Items 
                 Dim viewButton As ImageButton = DirectCast(item("viewColumn").Controls(0), ImageButton) 
                 viewButton.Attributes.Add("OnClick""return confirm('Quit editing this employee?')"
             Next 
         End If 
     End Sub 
 
 


Regards
Shinu
0
William
Top achievements
Rank 1
answered on 05 Mar 2009, 10:00 PM
Excellent! That works. Thanks Shinu.
Tags
Grid
Asked by
William
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
William
Top achievements
Rank 1
Share this question
or