Cannot get index of datagrid row when I click on an image button in template column

Last post 12-07-2007 12:20 AM by naveenj. 2 replies.

Sort Posts:

  • Cannot get index of datagrid row when I click on an image button in template column

    12-05-2007, 9:53 AM
    • Loading...
    • freelandera
    • Joined on 12-04-2007, 2:25 PM
    • Canterbury, Kent, UK
    • Posts 50
    I have the following code. I'm trying to get the method moveUp to fire off when a little up arrow is clicked. It works fine as a buttoncolumn because then I can use
     e.Item.ItemIndex 
    to get the selected row index but how do I get an imagebutton within a template column to do this- it always comes back null at the moment. Btw, my datagrid is bound to an array list so I need the index of the row.
     
    Thanks in advance.
      
    1    <%@ Page Language="vb" %>
    2    <%@ Import Namespace="System.Web.Mail" %>
    3    <%@ Import Namespace="System.Data" %>
    4    <%@ Register TagPrefix="Application" TagName="Previous" src="PreviousEmployEntry.ascx" %>
    5    <script runat="server">
    6    
    7        Dim previousEmployments As ArrayList = New ArrayList()
    8        
    9        Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    10                     
    11           If Session("page6") Then
    12               
    13               previousEmployments = Session("previousEmploy")
    14               dgPrevious.DataSource = previousEmployments
    15               dgPrevious.DataBind()
    16               lblHelper.Visible = False
    17               
    18            ElseIf Not Page.IsPostBack And Session("page5") Then
    19               
    20               Session("previousEmploy") = previousEmployments
    21               dgPrevious.DataSource = previousEmployments
    22               dgPrevious.DataBind()
    23               lblHelper.Visible = True
    24   
    25           Else
    26   
    27           End If
    28          
    29           
    30       End Sub
    31   
    32       Sub show_form(ByVal Obj As Object, ByVal E As EventArgs)
    33           
    34           If (Obj.text = "Add New Employer") Then
    35               previousEntry.Visible = True
    36           Else
    37               previousEntry.Visible = True
    38               previousEntry.gap = True
    39               previousEntry.checkGap()
    40           End If
    41   
    42           
    43       End Sub
    44   
    45       Sub dataGrid_Click(ByVal s As Object, ByVal e As DataGridCommandEventArgs)
    46           
    47           
    48           Dim x As Integer = e.Item.ItemIndex
    49           lblerror.Text = "hello oo ooo " + x.ToString
    50           lblerror.visible = True
    51           
    52           If e.CommandName = "editdelete" Then
    53               
    54               editdelete(s, e)
    55               
    56           ElseIf e.CommandName = "up" Then
    57               
    58               moveUp(s, e)
    59               
    60           ElseIf e.CommandName = "down" Then
    61               
    62               moveDown(s, e)
    63                           
    64           End If
    65       End Sub
    66       
    67       Sub moveDown(ByVal s As Object, ByVal e As DataGridCommandEventArgs)
    68           
    69           Dim x As Integer = e.Item.ItemIndex
    70           lblerror.Text = x.ToString
    71           
    72           Try
    73               Dim oldRecord As PreviousEmployment = previousEmployments.Item(x)
    74           
    75               previousEmployments.Insert(x + 2, oldRecord)
    76               previousEmployments.RemoveAt(x)
    77               lblerror.Text = x.ToString + "New Item: (x + 1) " + previousEmployments.Item(x + 1).ToString() + " Old Item: (x - 1) " + previousEmployments.Item(x - 1).ToString()
    78           
    79           Catch ex As System.ArgumentOutOfRangeException
    80               
    81           Finally
    82            
    83               'Response.Redirect("emailform6.aspx")
    84               
    85           End Try
    86           
    87       End Sub
    88       
    89       Sub moveUp(ByVal s As Object, ByVal e As DataGridCommandEventArgs)
    90           
    91           Dim x As Integer = e.Item.ItemIndex
    92           lblerror.Text = "hellooooooooooo " + x.ToString
    93           
    94           Try
    95               Dim oldRecord As PreviousEmployment = previousEmployments.Item(x)
    96           
    97               previousEmployments.Insert(x - 1, oldRecord)
    98               previousEmployments.RemoveAt(x + 1)
    99               lblerror.Text = x.ToString + "New Item: (x-1) " + previousEmployments.Item(x - 1).ToString() + " Old Item: (x) " + previousEmployments.Item(x).ToString()
    100          
    101          Catch ex As System.ArgumentOutOfRangeException
    102              
    103          Finally
    104           
    105              Response.Redirect("emailform6.aspx")
    106              
    107          End Try
    108          
    109      End Sub
    110      
    111      Sub editdelete(ByVal s As Object, ByVal e As DataGridCommandEventArgs)
    112          
    113          Dim x As Integer = e.Item.ItemIndex
    114          
    115          If previousEmployments.Item(x).Gap() Then
    116              
    117              Dim pe As PreviousEmployment = previousEmployments.Item(x)
    118              previousEmployments.RemoveAt(x)
    119              If previousEmployments.Count = 0 Then
    120              
    121                  Session("page6") = False
    122              
    123              End If
    124              previousEntry.Visible = True
    125              previousEntry.gap = True
    126              previousEntry.checkGap()
    127              previousEntry.theRecord = pe
    128  
    129              
    130              
    131          Else
    132              
    133              Dim pe As PreviousEmployment = previousEmployments.Item(x)
    134              previousEmployments.RemoveAt(x)
    135          
    136              If previousEmployments.Count = 0 Then
    137              
    138                  Session("page6") = False
    139              
    140              End If
    141              previousEntry.Visible = True
    142              previousEntry.theRecord = pe
    143          End If
    144  
    145                  
    146      End Sub
    147          
    148          
    149      Sub NextPage_Click(ByVal Obj As Object, ByVal E As EventArgs)
    150          
    151              
    152          Session("page6") = True
    153          Response.Redirect("emailform7.aspx")   'Redirect the user            
    154            
    155      
    156      End Sub
    157      
    158      Protected Sub dgPrevious_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    159  
    160      End Sub
    161  </script>
    162  
    163  
    164  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    165  <html xmlns="http://www.w3.org/1999/xhtml">
    166  
    167              <tr>
    168                  <td>               
    169                     
    170                     <asp:DataGrid ID="dgPrevious" runat="server" AutoGenerateColumns="False" onItemCommand="datagrid_Click" cssclass="applicationinsettable2" OnSelectedIndexChanged="dgPrevious_SelectedIndexChanged">
    171                     <HeaderStyle HorizontalAlign="Center" Font-Size="13px" CssClass="header" />
    172                      <Columns>
    173                          
    174                          <asp:TemplateColumn>
    175                              <ItemTemplate>
    176                                          <asp:ImageButton runat="server" ImageUrl="arrowup.gif" CommandName="up" />
    177                              </ItemTemplate>
    178                          </asp:TemplateColumn>
    179                          <asp:ButtonColumn Text="Down" CommandName="down">
    180                              <ItemStyle BorderStyle="None" />
    181                          </asp:ButtonColumn>
    182                          <asp:BoundColumn DataField="DateFrom" HeaderText="From" >
    183                              <ItemStyle Wrap="True" />
    184                          </asp:BoundColumn>
    185                          <asp:BoundColumn DataField="DateTo" HeaderText="To" >
    186                              <ItemStyle Wrap="True" />
    187                          </asp:BoundColumn>
    188                          <asp:BoundColumn DataField="Employer" HeaderText="Employer" >
    189                              <ItemStyle Wrap="True" />
    190                          </asp:BoundColumn>
    191                          <asp:BoundColumn DataField="Position" HeaderText="Your Position" >
    192                              <ItemStyle Wrap="True" />
    193                          </asp:BoundColumn>   
    194                          <asp:BoundColumn DataField="Salary" HeaderText="Salary" >
    195                              <ItemStyle Wrap="True" />
    196                          </asp:BoundColumn>
    197                          <asp:BoundColumn DataField="Reason" HeaderText="Reason for leaving" >
    198                              <ItemStyle Wrap="True" />
    199                          </asp:BoundColumn> 
    200                          <asp:ButtonColumn Text="Edit/Delete" CommandName="editdelete"></asp:ButtonColumn> 
    201  
    202                      </Columns>
    203  
    204                     </asp:DataGrid>
    205   </td>
    206  </tr>
    207              
    208  
    209    		</table>
    210  
    211  
    212  	        
    213  
    214      </form>
    215  
    216  </body>
    217  </html>
    218  
    219  
    
     
    Lilalfyalien
  • Re: Cannot get index of datagrid row when I click on an image button in template column

    12-07-2007, 12:02 AM
    Answer

    Hi:

      Tow options:

    1. Add a click event handler for this ImageButton. From sender.Parent.Parent you can get the DataGridItem and thus index is got.

    2. Use CommandArgument to pass the index like this:

    CommandArgument='<%#Container.ItemIndex%>'

      In DataGrid's ItemCommand event handler, get the index via:

    e.CommandArgument

     

    Regards

    Sincerely,
    Allen Chen
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: Cannot get index of datagrid row when I click on an image button in template column

    12-07-2007, 12:20 AM
    Answer
    • naveenj
    • Joined on 07-02-2007, 5:09 AM
    • India
    • Posts 600

    Hi freelandera,

    try this.

    Inside the ItemCommandEvent of the datagrid.

    if(e.CommandName=="up)
    {

    DataGridItem dgr = ((ImageButton)sender).Parent.Parent as DataGridItem;

    }

    Regards,
    Naveen

    Regards,
    Naveen

    Please remember to click Mark as Answer on the post that helps you, and to click Unmark as Answer if a marked post does not actually answer your question.
    View Blog
Page 1 of 1 (3 items)
Microsoft Communities
Page view counter