Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 129651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:49:22+00:00 2026-05-11T05:49:22+00:00

After doing a quick search I can’t find the answer to this seemingly simple

  • 0

After doing a quick search I can’t find the answer to this seemingly simple thing to do.

How do I Manually Select An Item in an Asp.Net ListView?

I have a SelectedItemTemplate, but I don’t want to use an asp:button or asp:LinkButton to select an item. I want it to be done from a URL. Like a QueryString, for example.

The way I imagine would be on ItemDataBound, check a condition and then set it to selected if true, but how do I do this?

For example:

protected void lv_ItemDataBound(object sender, ListViewItemEventArgs e) {    using (ListViewDataItem dataItem = (ListViewDataItem)e.Item) {       if (dataItem != null) {         if( /* item select condition */ ) {                 // What do I do here to Set this Item to be Selected?             // edit: Here's the solution I'm using :             ((ListView)sender).SelectedIndex = dataItem.DisplayIndex;              // Note, I get here and it gets set             // but the SelectedItemTemplate isn't applied!!!          }      }   } } 

I’m sure it’s one or two lines of code.

EDIT: I’ve updated the code to reflect the solution, and it seems that I can select the ListView’s SelectedItemIndex, however, it’s not actually rendering the SelectedItemTemplate. I don’t know if I should be doing this in the ItemDataBound event as suggested below.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T05:49:23+00:00Added an answer on May 11, 2026 at 5:49 am

    I looked at some of what’s going on in ListView under the hood and think this is probably the best approach.

    void listView_ItemCreated(object sender, ListViewItemEventArgs e) {     // exit if we have already selected an item; This is mainly helpful for     // postbacks, and will also serve to stop processing once we've found our     // key; Optionally we could remove the ItemCreated event from the ListView      // here instead of just returning.     if ( listView.SelectedIndex > -1 ) return;       ListViewDataItem item = e.Item as ListViewDataItem;     // check to see if the item is the one we want to select (arbitrary) just return true if you want it selected     if (DoSelectDataItem(item)==true)     {         // setting the SelectedIndex is all we really need to do unless          // we want to change the template the item will use to render;         listView.SelectedIndex = item.DisplayIndex;         if ( listView.SelectedItemTemplate != null )         {             // Unfortunately ListView has already a selected a template to use;             // so clear that out             e.Item.Controls.Clear();             // intantiate the SelectedItemTemplate in our item;             // ListView will DataBind it for us later after ItemCreated has finished!             listView.SelectedItemTemplate.InstantiateIn(e.Item);         }     } }  bool DoSelectDataItem(ListViewDataItem item) {     return item.DisplayIndex == 0; // selects the first item in the list (this is just an example after all; keeping it simple :D ) } 

    NOTES

    • ListView selects the template an item will use after it’s DataBinding event fires. So if the SelectedIndex is set before then, no more work is necessary
    • Setting the SelectedIndex anywhere after DataBinding works, you just don’t get the SelectedItemTemplate. For that you have either rebind the data; or reinstantiate the SelectedItemTemplate on the ListViewItem. be sure to clear the ListViewItem.Controls collection first!

    UPDATE I have removed most of my original solution, since this should work better and for more cases.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 103k
  • Answers 103k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It's a strictness declaration. Basically, it means that it must… May 11, 2026 at 8:24 pm
  • Editorial Team
    Editorial Team added an answer Retrieving the selector used to call the plugin: jQuery.fn.myPlugin =… May 11, 2026 at 8:24 pm
  • Editorial Team
    Editorial Team added an answer I don't think you'd need to know the peak hours;… May 11, 2026 at 8:24 pm

Related Questions

After doing a quick search I can't find the answer to this seemingly simple
I have a development vm which is running sql server as well as some
This hangs in Php (5.2.6-Win32 + Oracle10g) is it a bug, or I'm doing
In a web application, I'm hitting some troubles with dynamic content - whereby a
This should a quick question for some easy rep. I'm doing some PHP Website

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.