none
How to reset the SelectedIndex to -1? RRS feed

  • Question

  • I have a webpage with a form at the top to filter results and a detailsview to show details of an item selected in the gridview which is below that. The problem is when someone has selected an item in the gridview and then goes to the form and sets some addition filters to narrow down the results the same index is still selected but it's a different record now. What I would like to do is when they click on the filter button to narrow there search results is reset the SelectedIndex for the page. That way no row will be selected in the gridview.
    Roger Muscarello
    Monday, September 19, 2011 5:31 PM

Answers

  • Once I changed the button to this it clears the selected item in the gridview and clears the detailsview.

    <asp:Button id="Button1" runat="server" PostBackUrl="trailstest.aspx" Text="Search" OnClick="GridView1_PageIndexChanged"/>


    Roger Muscarello
    • Marked as answer by rmuscarello Thursday, September 29, 2011 8:20 PM
    Thursday, September 29, 2011 8:20 PM

All replies

  • This is by-design, but you can change the SelectedIndex to -1 yourself in the PageIndexChanged event.

     


    Jim Cheshire -- Microsoft
    Monday, September 19, 2011 6:50 PM
    Moderator
  • What I like to do in a situation like that would be, for example, to set gridview.visible=false at any time other than when you know it will display the results being searched.



    ClarkNK, A.K.A. HomePage Doctor
    HomePageDoctor.com -- Expression Web database tutorials
    Ownertrades.com -- Created with FP, Access, Bots and Wizards
    LawOfAllTheLand.org -- Created with Expression, VWDExress, SQL Express, and ASP.NET.
    Arvixe -- My favored web host
    Monday, September 19, 2011 8:37 PM
  • I'm still learning this stuff.  I put this at the top of the .aspx page. It clears the detailsview but dosn't clear the grideview.

    <script runat="server">
    void GridView1_PageIndexChanged(object sender, EventArgs e)
    {
        GridView1.SelectedIndex = -1;
    }
    </script>

    AND then in the gridview tag I put

     

    <asp:GridView id="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="id,id1" DataSourceID="SqlDataSource1" ToolTip="Select a row to see more information on the trail." OnPageIndexChanged="GridView1_PageIndexChanged">


    Roger Muscarello
    Tuesday, September 20, 2011 6:01 PM
  • Changing the selectedindex to -1 isn't supposed to clear the gridview.  It simply says "no item is selected" (which is why the details view, which I assume is based on the selecteditem of the gridview, doesnt show any more).
    Tuesday, September 20, 2011 6:22 PM
  • I want to clear the selectedindex in the gridview but it's not doing that. When they do a filtered search.  The same row is highlighted on the selectedindex in the gridview even though the record may be different. So if they just hit the filtered search button again without doing anything else then the detailview gets populated with the detailed information based off of the selectedindex in the gridview. 
    Roger Muscarello

    • Edited by rmuscarello Tuesday, September 20, 2011 6:42 PM
    Tuesday, September 20, 2011 6:34 PM
  • The GridView.PageIndexChanged event is raised when one of the pager buttons (next, back) is clicked, which doesn't sound like what you wanted.  You need to identify the event (of the GridView, or of one of the other controls) that you want to cause the GridView's selected index to be set to -1, and tie it to that.

    Try the asp.net forum, where you will be welcome to paste in asp.net code, in addition to describing what you are doing: http://forums.asp.net/

    Tuesday, September 20, 2011 8:00 PM
  • I would just debug this and see, based upon your design, which event is most appropriate. Could be that you need to do this when you databind your GridView.

     


    Jim Cheshire -- Microsoft
    Tuesday, September 20, 2011 8:15 PM
    Moderator
  • When they click on this button is when I would like the selectedindex to be reset to -1 for the gridview.

    <asp:Button id="Button1" runat="server" PostBackUrl="trailstest.aspx" Text="Search" />


    Roger Muscarello
    Tuesday, September 20, 2011 8:28 PM
  • Once I changed the button to this it clears the selected item in the gridview and clears the detailsview.

    <asp:Button id="Button1" runat="server" PostBackUrl="trailstest.aspx" Text="Search" OnClick="GridView1_PageIndexChanged"/>


    Roger Muscarello
    • Marked as answer by rmuscarello Thursday, September 29, 2011 8:20 PM
    Thursday, September 29, 2011 8:20 PM