Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how do i get the GridView current row number

Posted on 2006-10-19
2
Medium Priority
?
1,463 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 Ageil earned 750 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

722 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