Solved

Grid view with Ajax button extender, return row clicked

Posted on 2011-09-20
9
527 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
ID: 36567238
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
ID: 36567251
The row number can be obtained from e.Row.RowIndex.  The eventargs is GridViewRowEventArgs
0
 

Author Comment

by:MrDavidThorn
ID: 36567333
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
ID: 36567365
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 9

Expert Comment

by:BlueYonder
ID: 36567372
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
ID: 36567415
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
ID: 36567444
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
ID: 36568154
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
ID: 36568569
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
jQuery is a JavaScript library that greatly simplifies JavaScript programming. AJAX is an acronym formed from "Asynchronous JavaScript and XML."  AJAX refers to any communication between client and server, when the human client does not observe a…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

910 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

23 Experts available now in Live!

Get 1:1 Help Now