Solved

how do i get the GridView current row number

Posted on 2006-10-19
2
1,458 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
[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
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
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 we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

628 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