Solved

how do i get the GridView current row number

Posted on 2006-10-19
2
1,450 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
Comment Utility


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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

762 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

12 Experts available now in Live!

Get 1:1 Help Now