Solved

how do i get the GridView current row number

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

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…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

739 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