How to get the row index of a selected row in listview control

Can someone please tell me how to get the index of a selected row in a listview.

1
mylok
10/3/2008 12:25:47 PM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

9 Replies
1729 Views

Similar Articles

[PageSpeed] 29

 <ItemTemplate>

<asp:LinkButton runat="server" class="linkButton" id="xxxxx" Text='<%#Eval("documenttype") %>' CommandName="OpenFile" CommandArgument='<%#Eval("DocumentKey") %>'></asp:LinkButton>

</ItemTemplate>

what you have to do is, in itemcommand event of ListView, you have to get the value of row index

if(e.CommandName == "corrospondingcommaandname")

{
           // here your code ///
}


Please Mark as Answred If This Blog has Helped You.


Regards
Mitesh Darji
http://www.indianic.com
http://www.mitatdotnet.blogspot.com
0
Mitesh_Darji
10/3/2008 12:42:35 PM

check this Link:

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listview.itemcommand.aspx 


Please Mark as Answred If This Blog has Helped You.


Regards
Mitesh Darji
http://www.indianic.com
http://www.mitatdotnet.blogspot.com
0
Mitesh_Darji
10/3/2008 12:45:05 PM

Mitesh_Darji:

 <ItemTemplate>

<asp:LinkButton runat="server" class="linkButton" id="xxxxx" Text='<%#Eval("documenttype") %>' CommandName="OpenFile" CommandArgument='<%#Eval("DocumentKey") %>'></asp:LinkButton>

</ItemTemplate>

what you have to do is, in itemcommand event of ListView, you have to get the value of row index

if(e.CommandName == "corrospondingcommaandname")

{
           // here your code ///
}

Thank for reply, but I am new in asp.net. How can I get the value of row index? I know I can bind something in CommandArgument, but how can I find the selected row index?

0
mylok
10/3/2008 12:50:19 PM

give this as commandargument. 

CommandARgument = '<%# Container.ItemIndex%>'


Give a man a fish and you feed him for a day. Teach a man to fish and you feed him forever.
0
ramireddyindia
10/3/2008 1:10:54 PM

 if(e.CommandName == "OpenFile")

{
        string id = e.row.rowIndex.tostring(); or e.RowIndex();
}


Please Mark as Answred If This Blog has Helped You.


Regards
Mitesh Darji
http://www.indianic.com
http://www.mitatdotnet.blogspot.com
0
Mitesh_Darji
10/3/2008 1:21:20 PM

ramireddyindia:

give this as commandargument. 

CommandARgument = '<%# Container.ItemIndex%>'

What is Container.ItemIndex? I am new in asp.net. My table column like:    CustomerID, Name, Address, Picture...            I know CustomerID != selected row index

Can you give me some idea what exactly I need to bind in CommandArgument.

0
mylok
10/3/2008 2:09:42 PM

Mitesh_Darji:

 if(e.CommandName == "OpenFile")

{
        string id = e.row.rowIndex.tostring(); or e.RowIndex();
}

There is not ListViewRow or RowIndex in ListView. How can I do?

0
mylok
10/3/2008 2:35:19 PM

Anyone can help?

0
mylok
10/3/2008 5:35:11 PM

Use the ListView.SelectedIndex() property

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listview.selectedindex.aspx

VB.Net

Protected Sub DeleteButton_Click(ByVal sender As Object, ByVal e As EventArgs)

  ' Check if an item is selected to delete it.
  If ContactsListView.SelectedIndex >= 0 Then
    ContactsListView.DeleteItem(ContactsListView.SelectedIndex)
  Else
    Message.Text = "No contact was selected."
  End If

End Sub
C#  
protected void DeleteButton_Click(object sender, EventArgs e)
{
  //Check if an item is selected to delete it.
  if (ContactsListView.SelectedIndex >= 0)
    ContactsListView.DeleteItem(ContactsListView.SelectedIndex);
  else
    Message.Text = "No contact was selected.";
}
 
There are no stupid questions, only easy answers. However, the quality of assistance you receive (if any) is directly proportional to the quality of your request.

p.s. Please remember to "mark as answer" all posts that answer your question.
0
CharlesF
10/3/2008 6:18:23 PM
Reply:

Similar Artilces:

How to delete row from GridView control
Hello,  I am working on an ASP.NET 2.0 app, and I am trying to wire up a "Delete" button to a gridview control on a form. Here is the code for the ASP :  <asp:GridView ID="gvInConditions" runat="server" AutoGenerateColumns="true" OnRowDeleting="gvInConditions_RowDeleting" DataKeyNames="Condition_ID" >         <Columns>                <asp:CommandField ButtonType="Button" SelectText=&...

Add new controls to a gridview row; when editing that row using the edit row feature in the gridview control.
Hi all  I have a questions that looks hard / complex to me, and i know you can help me in this. Q {  I have row in a Gridview and i have an edit option to it. I press the edit button, to edit the values in that row and update data all the way back to the database. Now when i press the edit button, i want a drop down list to be displayed that contain values for a particular field in my grid view. (for now only plain text boxes appear.) } I hope you understand my question, all i need to do is replace the text box controls with a dropdown list when i am editing a row in ...

Problem in Using DropDownList Control to select a selected row of SqlDataSource that is under GridView Control
Hi all,In my SQL Server Management Studio Express, I have a database "ChemDatabase" that has a table "LabData"  formatted in the following way: SampleID int PRIMARY KEY, SampleName nvarchar(25), ChemicalName, nvarchar(25), Concentration decimal(6.2) with 5 rows of records in this .dbo table. In my Visual Web Developer Express, I created a website "RPD-TCE" for extracting a row of record from the LabData table by using a DropDownList, a GridView and a SqlDataSource.  I want to click DropDownList to list one number of S...

To get the row index of selected row in grid view
Hi I have a grid view with a column of checkboxes. if I check a checkbox all the remaining checkboxes should be unchecked except the current one.protected void chkHeadCoach_CheckedChanged(object sender, EventArgs e) {foreach (GridViewRow rowitem in grdv.Rows) {CheckBox chkHeadCoach = (CheckBox)(rowitem.FindControl("chkHeadCoach"));if (chkHeadCoach.Checked) {if(rowitem.RowIndex != //row index of the row I checked now) {chkHeadCoach.Checked = false; } } } how can I get the row index of the row I checked now? }Thanks & Regards,Abhiram Reddy Mekha. AbhiramReddyMekh...

Getting the row index of a command in gridview control
In my gridview I have a textbox and a save icon which allows a user to enter a date and click save which saves the date to the database for that record pointed to in the row. The problem I have is that I don’t know how to get the row to process the save in the codebehind. Normally I would set the command-argument equal to the product ID and then in the codebehind handle the event where the commandname matches the commandname for the save icon. I have the problem then though of not knowing which row caused the event. If I use a buttonfield, I know it sets the commandargument to the row index...

How to set the row number and get the row number for ("selected row) in Datagrid
I face a problem that i can't get the selected row of the datagrid in both server side script (VB.net) and client side script (javascript) As i can't use the button column / hyperlink column in the datagrid. Thus , i need to handle the change the background color of the selected row and also to get the selected row number for Update/Delete. Thanks in advance. This depends on how do you perform the selection. Do you want to implement chaning of the SelectedRow at the client side without any postback? If you have no issues with postback, then you can do s little trick here: 1. ...

How to get selected index of dropdownlistboxs present in two different rows of gridview
I have a gridview, in that i have dropdownlistbox as one column. Without editing the grid view i want to know the selected index of dropdownlistbox for each row. Simply itearte through the Rows collection of your GridView. Then, use the FindControl method of the GridViewRow reference and retrieve a reference to your DropDownList. From here, you can then retrieve your SelectedIndex. This can be done within the GridView.DataBound event or later.Thanks, EdMicrosoft MVP - ASP/ASP.NET...

I can't get value of control in the selected row of a Gridview
I'm trying to perform an update using a sqldatasource from a gridview after a selection is made from a dropdown and a linkbutton "lbnUpdate" is clicked. I assigned  "select" as the command name of the linkbutton so I was assuming that I could then reference the gridviews selected row from the code behind but I guess I made a bad assumption. During debug there is "nothing" in the variable I use to capture the gridview selected row. I will post what I have. If I set AutoGenereateSelect button to true I can get the SelectedIndexChanged event...

How to get selected index of dropdownlistboxs present in two different rows of gridview
I have a gridview, in that i have dropdownlistbox as one column.  Without editing the grid view i want to know the selected index of dropdownlistbox for each row. See http://forums.asp.net/t/1170088.aspx.Thanks, EdMicrosoft MVP - ASP/ASP.NET...

I want to select top 3 rows except the first from 4 rows of a Table. It should select from top 2 row,top3 row and top4 row.
 Hi, Let say I've 4 rows in a table. I want to select top 3 rows except the first from 4 rows of a Table. It should select from top 2 row,top3 row and top4 row. Thanks in advance Asghar Ali Mohammed http://www.aliwebdev.com(Web Designer and Developer)Do not forget to "MARK AS ANSWER" on the post that helped you. If you can use Linq, you can use Skip. http://msdn.microsoft.com/en-us/vcsharp/aa336757.aspxThanks, EdMicrosoft MVP - ASP/ASP.NET Hii dear..U can fire the Query select Top(3) from Table1 order by id desc.. "A conclusion is whe...

Scroll row by row in a datawindow control
Hi. I have a tabular datawindow object with one group and all my data is displayed in the header group and the detail band is not visible. when i run my application and i want to scroll the data the datawindow control scrolls the data page by page and i need it row by row. How can i do this? thank you in advanced You will have to move your data into the detail band. "Row" in datawindow is contents of the detail band. Igor "Fernando Pe�a" <Fernando_Pe0xL1A4za> wrote in message news:943886BF87C590640077873785256A65.0077874785256A65@webforums.....

Selecting rows using Row Index
Hi,I am using SQL Server 2000. I need to select specific rows from my table. Like, select all rows from table whose row index is between 10 and 20. Does anyone has any idea how this could be accomplished?  Select * from Products where row_index between 10 AND 20 What should I do that?    Hi,there is no row_index in the sql server 2000 so you can't directly write your statement for that you can do this waySelect TOP 6 *From  (Select TOP 12 *          From YourTableName          Order By SomeOrder)Order By SomeOrd...

Get row index of found row
How do I find the row index after searching a Dataset Table? For example, <code> Dim recId As String = Request.QueryString("r") Dim r As DataRow Dim tbl As DataTable = ds.Tables("drivers") tbl.PrimaryKey = New DataColumn() {tbl.Columns("recId")} If NOT IsNothing(tbl.Rows.Find(recId)) Then                r = tbl.Rows.Find(recId) End If </code> Next, I want to know the row index of the row, r, so the user can move foward and back, from this point, through the rows in the table, in order to display...

C#:How to select a row in ListView and change the background color to blue which row is selected
 Hi,This is a part of code,I want to write the method name is select to do what I wanted.I don't know how to judgement which row is selected,and how to change the background color. ------------------------------------------------------------------------------------------------------------------------------ <ItemTemplate>            <tr style="background-color: #FFFBD6;color: #333333;">                <td>   ...

Getting the focus row to follow the selected row..
These days I've been looking for solutions to the simpler, less critical but annoying problems. One that my users have noticed is that if they select a row (by clicking on a column in that row) my application will select the row and the focus moves to the column they clicked in the row. However, if the user clicks somewhere that isn't a column but is still in the detail band area for that row, my application will select the row, but the focus will remain on the column and row that it did previously. Is there a way to get the focus to follow the selected row? Thanks in advance...

How To: Reference controls in footer row from an item row
I'm familiar with using the e.Item.FindControl command when manipulating controls from an item row or footer row when you are actually in (editing) that row. I'm also familiar with determining what "Type" of row I'm in. What I'm having troubles with is referencing a control in the footer row of a datagrid while I'm actually in (editing) an item row. Can this be done? Thanks Mike...

ListView control with multiple select command controls
 Hi there, I have a listview control, on which I have two buttons 1) Wesite 2)Email Me. Note the listview only has the Item and Layout Templates as I will not be editing or inserting. I have assigned both the controls the CommandName="select" so that they both fire the SelectedIndexChanged event. There I can access the website address and email address held in datakeys. In the listviews SelectedIndexChanged event I need to work out which control fired the event. Ie if it was the website button then I will redirect the user to the website... Code in VB please. ...

Gridview paging enabled. Has checkbox to select the row . If user select the checkbox on one page and move to 2nd page to select the rows there also to export the selected rows to excel. But doing so
HiI am using Gridview and that has paging enabled. Using checkbox to select the particular row of the gridview to export the selected rows to the excel . Everything is working fine except for one case.Let user selected the "check" box for, five rows which user would like to export to excel (two of rows on page 1 of results and 3 of them on page 2 of results), however it only exports the 3 row details from page 2, because it seems to de-select the 2 rows from page 1 when I move to page 2 and vice versa. The code i am using is as :protected void BtnExcel_Click(object sender, EventArgs e)    {        binddata();        getinfo();        string excel = string.Empty;        foreach (GridViewRow row in GridView1.Rows)        {            CheckBox cb = (CheckBox)row.FindControl("rowlevelcheck");            if (cb.Checked)            {                excel += "'" + Convert.ToString(GridView1.DataKeys[row.RowIndex].Value) + "',";              &n...

Getting the control id of a control inside gridview control
 uhmm.. a little bit confusing regarding my subject because i dont know how to say it...by the way my problem is how can i get the id of a control (e.g. textbox) inside the gridview... and the textbox is in the templatefield... is there any way i can get its id??  hoping i could get a reply as soon as possible... Regards,MhaeyPlease remember to click “Mark as Answer” on the post that helps you.. =) What do you want's to do by getting the id of the Text box? Do you want's to find particular Textbox with certain id ? Kamran ShahidSr. Software Engineer(MCP,MCAD.net,MC...

Updating and binding control to other controls within the same row of a GridView
HiIn the EditTemplate of my GridView I show 2 DropDownLists. When the user selects the first dropdownlist, I need it to update the contents of the second dropdownlist. I have my Subs that do this fine when not inside the GridView, but what is the correct way to do it whilst they are in the GridView?This is my current attempt that returns the error 'Object reference not set to an instance of an object'. I presume its related to correctly targeting the DropDownList in question? This is my GridView:<asp:GridView ID="GridView1" runat="server"  &...

get row index of row from datatable from primary key
How get row index of particular row from datatable for eg.        i have create table of following column and fill rows in that and bind it into gridview            now i want to get row index of selected row from column LedgerDetailID , how and reply me             DataTable tbl = new DataTable();            tbl.Columns.Add(new DataColumn("Index", System.Type.GetType("System.Int32"))); ...

Gridview Rows Backcolor getting reset when selecting a row
I have a list of rows in my gridview that list one or more entries per date.  I have coded the RowDataBound event so that all of the rows for a given date have the same backcolor.  Day 1 is white, Day 2 is yellow, Day 3 is white, day 4 is yellow - where each day could have multiple rows.  On each row, I also have an Edit imagebutton that allows each row to be edited via a separate panel and not in-line grid editing.  When I click on the Edit imagebutton, I set the gridviews selectedindex to that row and then using the skin for the grid, the selected row backco...

Finding selected row index by row command method....
hi... I have a gridview..in that there is a template column....which template column contains a button... I want when the button will be clicked that time rowcommand()  event will be triggered and and i need to find the index of that particular row on which i have clicked.............   i don't want to add  command field (Like select,delete,cancel) You can use a ButtonField instead of a TemplateColumn and then you can simply set the CommandName property e.g. <asp:ButtonField CommandName="Whatever" HeaderText="Value" Text="Value" ...

Selecting a gridview row and keeping the last selected row in a gridview highlighted after mouse moves over a new row.
What is the best way to show a gridview row highlighted once selected and keep it highlighted once the mouse moves over a different row? Thanks, Steve hi,you can use <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="#FFFFFF" /> plz let me know if i misunderstood you Thanks for your help. One problem; however, is that after the row has been selected and I check the "grid.SelectedIndex" it has a value of -1 and not the index of the row that was selected on the grid. What do you recommend? Thanks, Steve hi, sorry i din...

Web resources about - How to get the row index of a selected row in listview control - asp.net.presentation-controls

Portal:Law/Nominate/Selected article - Wikipedia, the free encyclopedia
Add the article in to a subpage of this portal, such as Portal:Law/Selected article/# , replacing the "#" with the number above the current highest. ...

F8: Facebook’s FbStart Initiative Will Give Selected Developers A $30K Kick-Start
... successful applications is likely a whole lot easier with $30,000 in free tools and services to start off with, and that’s exactly what selected ...

LEGO Shop - The LEGO Technic team has selected the winner... - Facebook
The LEGO Technic team has selected the winner of the Co-Creation Challenge! LEGO will be producing a limited run of 20,000 of this stunning set.... ...

baby365- Grand Doctor Ma Selected Students for iPad on the iTunes App Store
Get baby365- Grand Doctor Ma Selected Students on the App Store. See screenshots and ratings, and read customer reviews.

Selected Cheney Vice Presidential Photographs
... for photos found within these albums are Paul Morse, Karen Ballard, Kimberlee Hewitt, Shealah Craighead, and Chris Greenberg. These selected ...

[iOS Advice] How To Clarify Your Email Replies By Quoting Selected Text - YouTube
Follow me on Twitter: http://www.twitter.com/macmixing Like my Facebook page: http://www.facebook.com/macmixing RATE, COMMENT, SUBSCRIBE! :-) ...

Local farmers to sell their fresh dates at selected supermarkets
Fresh dates will be on sale this summer under the label of Local Harvest at selected supermarkets and Spinney's.

Animal of the Day: Lucius Has Been Selected as the U.K.’s ‘Happiest Cat’
He’s got one blue eye, one green eye and a heart of gold. British mobile phone company O2 recently held some “cat auditions” to find “the UK’s ...

Michael Sam becomes first openly gay player selected in an NFL Draft
Defensive end Michael Sam of the University of Missouri became the first openly gay player to be selected in a National Football League Draft ...

Artist facing child pornography charges selected for MCA exhibition
An artist facing child pornography charges has been selected to show work in a prestigious exhibition at the Museum of Contemporary Art Australia. ...

Resources last updated: 2/16/2016 12:14:21 PM