Solved

Grid view with Ajax button extender, return row clicked

Posted on 2011-09-20
9
525 Views
Last Modified: 2012-05-12
Hi Experts

I have a grid view control with an ajax toolkit confirm button extender, the code works fine but I want to store the selected row number from the data grid. As soon as the ajax confirm button appears the e value is set to the ajax control. Any ideas?
<asp:TemplateField HeaderText="Delete"  InsertVisible="False" Visible="False">
                         <HeaderStyle BackColor="#1973BB" ForeColor="White" HorizontalAlign="Center" />   
                        <ItemTemplate>
                           <asp:ImageButton ID="BookDelete" runat="server"
                                CausesValidation="False" 
                                ImageUrl="~/Images/icon-delete.jpg" OnClick="Button1_Click" />

                           <ajaxToolkit:ConfirmButtonExtender ID="ConfirmBookDelete" 
                    runat="server" TargetControlID="BookDelete"
                    ConfirmText="test"/>
                      </ItemTemplate>

Open in new window

0
Comment
Question by:MrDavidThorn
  • 5
  • 4
9 Comments
 
LVL 9

Expert Comment

by:BlueYonder
Comment Utility
To get a value from the row use the a hiddenfield
<asp:HiddenField runat="server" ID="hfId" Value='<%# Bind("Id") %>' />

to retrieve the value in code behind call hfId.value
0
 
LVL 9

Expert Comment

by:BlueYonder
Comment Utility
The row number can be obtained from e.Row.RowIndex.  The eventargs is GridViewRowEventArgs
0
 

Author Comment

by:MrDavidThorn
Comment Utility
sorry I dont quite understand, I have the Button1_Click routine  which is fired when the button is clicked, the ajax pop appears before the routine is executed if I use the  e value in Button1_Click  it returns the value from the ajax message box
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        
    End Sub

Open in new window

0
 
LVL 9

Expert Comment

by:BlueYonder
Comment Utility
Protected Sub Button1_Click (ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
Button1.Click
        e.Row.RowIndex;//this is the row number
    End Sub
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 9

Expert Comment

by:BlueYonder
Comment Utility
The event argument used is System.Web.UI.WebControls.GridViewRowEventArgs which contains the row number.  If you use just System.EventArgs, it will not contain the row number.
0
 

Author Comment

by:MrDavidThorn
Comment Utility
I get the error message

Method 'Protected Sub Button1_Click (sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs)' does not have a signature compatible with delegate 'Delegate Sub ImageClickEventHandler(sender As Object, e As System.Web.UI.ImageClickEventArgs)'.      

your code would work fine if I eant using the Ajax confirm button extender
0
 
LVL 9

Accepted Solution

by:
BlueYonder earned 500 total points
Comment Utility
1. Add this your GridView:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id"
            DataSourceID="SqlDataSource1" OnRowCreated="GridView1_RowCreated1" >

  <Columns>
            <asp:TemplateField HeaderText="myRowid">
            <ItemTemplate><asp:Label runat="server" />                    
            </ItemTemplate>          
            </asp:TemplateField>

...

</Columns>

2. Add this to your code page

  Protected Sub GridView1_RowCreated1(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
        If e.Row.RowType = DataControlRowType.DataRow Then

            ' Retrieve the Label from the first column.
            Dim myLabel As Label = CType(e.Row.Cells(0).Controls(0), Label)

            ' Set Label text equal to the rowIndex +1
            myLabel.Text = e.Row.RowIndex.ToString() + 1

        End If
    End Sub
0
 

Author Comment

by:MrDavidThorn
Comment Utility
the above method doesnt work
A) its on the OnRowCreated
B) I get the error message when using e.row.rownumber()

       'System.Web.UI.WebControls.GridViewCommandEventArgs.Friend ReadOnly Property Row As System.Web.UI.WebControls.GridViewRow' is not accessible in this context because it is 'Friend'.      

It works when I use debug mode but when I start the app it complains

Im new to Ajax so im obviously missing a trick!
0
 

Author Comment

by:MrDavidThorn
Comment Utility
After much searching I found the answer the imagebutton needs the select command name
     <asp:ImageButton ID="BookDelete" runat="server"
                                CausesValidation="False" CommandName="Select"
                                ImageUrl="~/Images/icon-delete.jpg" />


and then in my vb.net code Iv used
    Response.Write(gvBookNumber.Rows(gvBookNumber.SelectedIndex).Cells.Item(0).Text)

Many thanks!
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now