buddy
buddy

Reputation: 428

how to get row index of listview?

i have a listview that is showing images from database the the code looks like this.

 <ItemTemplate>
         <td runat="server" style="background-color: #E0FFFF;color: #333333;">
          <asp:ImageButton ID="ImageButton1" runat="server" 
           ImageUrl='<%# "imageHandler.ashx?ID=" + Eval("ID")%>' />
          </td>
 </ItemTemplate>

the datasource looks like this

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
ConnectionString="<%$ ConnectionStrings:GalleryConnectionString %>" 
SelectCommand="SELECT [ID], [IMAGE] FROM [Icon]"></asp:SqlDataSource>

now i want to get row index of the selected image button for further instance how do i do this kindly guide me?

Upvotes: 1

Views: 5993

Answers (2)

buddy
buddy

Reputation: 428

i have done it using this code thanks all of stackoverflow(my teachers)

 protected void abc(object sender, ListViewCommandEventArgs e)
    {
        // if(e.CommandSource == System.Web.UI.WebControls.ImageButton)
        ListViewDataItem dataItem = (ListViewDataItem)e.Item;
        //int RowID = Convert.ToInt32(ListView1.DataKeys[dataItem.DisplayIndex].Value);
        int DispalyIndex = e.Item.DisplayIndex;
        int ItemIndex = e.Item.DataItemIndex;
        ImageButton imgbtn = (ImageButton)dataItem.FindControl("ImageButton1");
        if (imgbtn != null)
        {
            string imageurl = imgbtn.ImageUrl;
            if (imageurl != null && imageurl != string.Empty)
            {
                int equalindex = imageurl.IndexOf("=");
                int Totallength = (imageurl.TrimEnd()).TrimStart() .Length;
                int ImageID = Convert.ToInt32(imageurl.Substring(equalindex + 1, (Totallength -(equalindex+1))));
            }

        }
    }

Upvotes: 1

FirstCape
FirstCape

Reputation: 639

Make an event for when the imagebutton is click:

protected void ImageButton1_click(object sender, EventArgs e)
{
    ImgaeButton btnSender = (ImageButton)sender;
    ListViewItem lvItem = (ListViewItem)btnSender.NamingContainer;
    lvItem.DataItemIndex();
    lvItem.DisplayIndex();
}

The DataItemIndex gets the index of the data item that was bound.

The DisplayIndex gets the position of the data item as displayed in the ListView.

Upvotes: 2

Related Questions