user3127986
user3127986

Reputation: 388

reset page in a ListView C#

I have an appointement listview with a dropdownlist (doctor).

I have 3 pages for 3 different doctors. When I am listing the first doctor name and I do next page (I am in page 2 for doctor 1) and I change the dropdownlist selection to doctor 2.

I was expecting to be at page 1 for doctor 2. Now I am at doctor 2 but page 2.

Any idea on how to reset the paging for a listview ?

Here is the aspx code:

 <asp:LinqDataSource ID="LinqDataSource2" runat="server"></asp:LinqDataSource>
 <asp:LinqDataSource ID="LinqDataSource1" runat="server" ContextTypeName="AptDataContext" 
     EnableUpdate="True" EntityTypeName="" TableName="Appointement" 
     Where="IsAvailable = True && dateApt >= DateTime.Now && doctorId = Doctor.Id &&
            doctorid = 1"> 

    <WhereParameters>
       <asp:ControlParameter 
           Name="doctor_name" 
           ControlID="DropDownList1" 
           PropertyName="SelectedValue"
           Type="String" />
    </WhereParameters>
 </asp:LinqDataSource>

 <div class="center">
    <asp:Label ID="lblDocName" runat="server" Text="Choose a doctor name"> </asp:Label>

    <div class="value-right"> 
       <asp:DropDownList ID="DropDownList1" runat="server" Width="180px" AutoPostBack="true" 
          OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
          DataTextField="Doctor_name"  >
       </asp:DropDownList> 
    </div>  
 </div>
 <br/><br/>
 <asp:Label ID="lblChooseApt" runat="server" Text="Choose a date"> </asp:Label>
 <br/>
 <asp:ListView ID="ListView1" runat="server" DataKeyNames="rdvId" DataSourceID="LinqDataSource1" 
      InsertItemPosition="LastItem" Autopostback="true" 
      OnPagePropertiesChanged="ListView1_PagePropertiesChanged"  >
    <AlternatingItemTemplate>
    <tr style="">
        <td>
             <asp:Label ID="doctorNameLabel" runat="server" 
                  Text='<%# Eval("Doctor.Doctor_Name") %>' />
        </td>
        <td>
            <asp:Label ID="dateAptLabel" runat="server" Text='<%# Eval("dateApt", 
                       "{0:dd/MM/yyyy}") %>' />
        </td>
        <td>
            <asp:Label ID="houreAptLabel" runat="server" Text='<%# Eval("houreApt"
                     , @"{0:hh\:mm}") %>' />
        </td>
        <td>
           <asp:CheckBox id="MyCheckBox" runat="server"
                value='<%# Eval("aptId") %>'
                AutoPostBack="true"
                OnCheckedChanged="Check_Clicked" />
        </td>

        <asp:HiddenField ID="doctorIdLabel" runat="server" Value='<%# Eval("doctorId") %>' />
    </tr>
    </AlternatingItemTemplate>
    <tr runat="server">
        <td runat="server" style="">
            <asp:DataPager ID="DataPager1" runat="server" Autopostback="true" >
                 <Fields>
                     <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" 
                       ShowLastPageButton="True" />
                 </Fields>
            </asp:DataPager>
        </td>
     </tr>

Here is the code I put in the page_load code behind. How do we trap that this event happen DropDownList1_SelectedIndexChanged(object sender, EventArgs e) ?

    if (IsPostBack &&  indexChanged)  <== I need to know how to trap the   
                            DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataPager pgr = ListView1.FindControl("DataPager1") as DataPager;
        if (pgr != null && ListView1.Items.Count != pgr.TotalRowCount)
        {
            pgr.SetPageProperties(0, pgr.MaximumRows, false);
            bIndexDdlChanged = false;
        }
    }

Upvotes: 0

Views: 1442

Answers (1)

user3127986
user3127986

Reputation: 388

Here is how I fix this issue:

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    int indexDdl = DropDownList1.SelectedIndex;
    indexDdl++;
    Session["doctorId"] = indexDdl;

    if (IsPostBack)                                                          <==new code added
    {
        DataPager pgr = ListView1.FindControl("DataPager1") as DataPager;    <==new code added
        if (pgr != null && ListView1.Items.Count != pgr.TotalRowCount)       <==new code added
        {
            pgr.SetPageProperties(0, pgr.MaximumRows, false);                <==new code added
        }
    }

    ListAppointement(indexDdl);
}

Upvotes: 2

Related Questions