Solved

how do i get the GridView current row number

Posted on 2006-10-19
2
1,451 Views
Last Modified: 2008-01-09
I have a template column where two buttons are placed one is delete message and other is reply for a message, i have placed a invisible label that holds the message id, how do i get the value of the lblMessageId of the row when the button is clicked. i always get the first row's lblMessageId from the code i have used below

--------------------

<asp:TemplateField ItemStyle-VerticalAlign="Top"  HeaderText= "Details">
  <ItemTemplate>
    <%#Container.DataItem("MSG_Detail")%><br />                                      
    <asp:LinkButton ID="btnViewMsg" OnClick="ViewMsg_Clicked" Text = "Reply"  runat="server"/>  
    <asp:LinkButton ID="btnDeleteMsg" OnClick="DeleteMsg_Clicked" Text = "Delete"  runat="server"/>
    <asp:Label ID="lblMsgID" Text='<%#Container.DataItem("ID")%>' runat="server" Visible="false"></asp:Label>
  </ItemTemplate>                                                                                
  <HeaderStyle HorizontalAlign="Left" />                                        
</asp:TemplateField>    

--------------------

    Protected Sub DeleteMsg_Clicked(ByVal sender As Object, ByVal e As EventArgs)
        Try
            For Each _row As GridViewRow In gvInbox.Rows
                Dim _lnk As LinkButton = _row.Cells(0).Controls(1)
                If (Not (_lnk Is Nothing)) Then
                    Dim lbl As Label = DirectCast(_row.FindControl("lblMsgID"), Label)
                    Dim _FAFInboxDB As New FAFInboxDB
                    _FAFInboxDB.DeleteMessage(lbl.Text)
                End If
            Next
        Catch ex As Exception
        End Try
    End Sub
0
Comment
Question by:sammy_chinnu
2 Comments
 
LVL 12

Expert Comment

by:deanvanrooyen
ID: 17771737


you might need to look at OnRowCommand="Gridview_OnRowCommand"
also there are some wierd issue with LinkButton on the post back,maybe use button, this binds off the status field in the db


eg

                 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Font-Size="X-Small"
                        Height="80px" OnRowCommand="Gridview_OnRowCommand" Width="786px" Caption="Not Delivered">
                        <Columns>                            

<asp:ButtonField DataTextField="status" HeaderText="Status" CommandName="status" Text="go to detail">
                                <ItemStyle HorizontalAlign="Left" Width="120px" />
                                <HeaderStyle BackColor="Silver" BorderColor="Silver" HorizontalAlign="Left" Width="120px" />
                            </asp:ButtonField>


    protected void Gridview_OnRowCommand(Object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "status")
        {
            // Convert the row index stored in the CommandArgument
            // property to an Integer.
            int index = Convert.ToInt32(e.CommandArgument);
            // Retrieve the row that contains the button clicked
            // by the user from the Rows collection.
            GridViewRow row = this.GridView1.Rows[index];
            int id  = Convert.ToInt32(row.Cells[0].Text);

        }
    }
0
 
LVL 27

Accepted Solution

by:
Sammy earned 250 total points
ID: 17771811
You can use CommandArgument and CommandName properties

<asp:LinkButton runat="server" ID="btnViewMsg" ToolTip="View Message" Text="Reply" CommandArgument='<%#Eval("Id") %>'</LinkButton>

Sub CustomersGridView_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
      _FAFInboxDB.DeleteMessage(index)         'Delete the Message
'You can test with Response.Write(index) to see the ID
End Sub

You dont have to add any hidden controls or anything like that.

take a deeper look here http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand.aspx

HTH
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

Title # Comments Views Activity
asp.net bundle 8 47
Json and asp.net mvc 2 45
Please explain "Multi-Tenant Services" 5 64
ADO.NET ENTITY DATA MODEL 3 32
Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

895 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

18 Experts available now in Live!

Get 1:1 Help Now