Solved

Grid view with Ajax button extender, return row clicked

Posted on 2011-09-20
9
534 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

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…
This article discusses how to implement server side field validation and display customized error messages to the client.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

752 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