Solved

Grid view with Ajax button extender, return row clicked

Posted on 2011-09-20
9
536 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

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…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

627 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