Click here to Skip to main content
15,898,752 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi guys ..
I have a page with a drop down list, a button and a grid view. Depending on the drop down list option chosen, the grid view gets filled from the database. The problem I am facing arises when multiple entries spanning more than 1 page are returned - on clicking the second or another page index, the page loads again BUT the grid view page index and returned rows remain the same.

The code is as follows (I only submit the relevant code as there are many other details - irrelevant to this - on the page) :

.aspx code

XML
<table style=" width:100%; height:23px; margin:0px; padding:0px; ">
       <tr>
           <td style=" width:16.67%; text-align:center; height:23px;" >
               EMPLOYEE NAME
           </td>
           <td style=" width:33.34%; text-align:center; height:23px;">
               <asp:DropDownList ID="DropDownList1" ToolTip="Employee Name" Height="23px" Width="100%" runat="server">
               </asp:DropDownList>
           </td>

XML
</tr><tr>
    <td colspan="2" style=" width:50%; height:23px; text-align:center; padding:10px;">
        <asp:Button ID="Button1" runat="server" Text="Search" onclick="Button1_Click" />
    </td>

        <tr>
            <td style=" width:50%; height:200px; border-color:Black; border-style:inset;border-width:1px; text-align:center;">
                <asp:GridView ID="GridView1" Height="200px" Width="100%" runat="server" 
                    AutoGenerateColumns="False" PageSize="8" AllowPaging="True" OnPageIndexChanging="GridView1_PageIndexChanging"
                    onselectedindexchanged="GridView1_SelectedIndexChanged">
                    <Columns>
                        <asp:BoundField DataField="coType" HeaderText="Company Type" 
                            SortExpression="coType" />
                        <asp:BoundField DataField="meetDate" DataFormatString="{0:d}" HeaderText="Meeting Date (mm/dd/yy)" 
                            SortExpression="meetDate" />
                        <asp:BoundField DataField="clientname" HeaderText="Co Name" 
                            SortExpression="clientname" />
                        <asp:BoundField DataField="pocName1" HeaderText="CR Name1" 
                            SortExpression="pocName1" />
                        <asp:BoundField DataField="coStatus" HeaderText="Comp. Status" 
                            SortExpression="coStatus" />
                        <asp:BoundField DataField="prodname" HeaderText="Prod Name" 
                            SortExpression="prodname" />
                        <asp:BoundField DataField="targetDate" DataFormatString="{0:d}" HeaderText="Target Date(mm/dd/yy)" 
                            SortExpression="targetDate" />
                        <asp:BoundField DataField="empLoginId" HeaderText="RM Id" 
                            SortExpression="empLoginId" />
                    </Columns>
                </asp:GridView>  
            </td>
            </tr>


The .aspx.cs code is as follows -

C#
protected void Page_Load(object sender, EventArgs e)
                  {
                     if (!Page.IsPostBack)
                      {
                          DropDownList1.DataSource = ds;
                          DropDownList1.DataTextField = "empFname";
                          DropDownList1.DataValueField = "empLoginId";
                          DropDownList1.DataBind();
           
                         using (SqlConnection con1 = new SqlConnection())
                          {
                              con1.Open();
                              string u = Session["User"].ToString().ToLower();
                              // Create new DataAdapter
                              using (SqlDataAdapter a = new SqlDataAdapter("SELECT [targetDate],[coType], [meetDate], [clientname], [pocName1], [coStatus], [prodname], [empLoginId] FROM [coMet] WHERE (([coType] = @coType) AND ([empLoginId]= @empLoginId))", con1))
                              {
                                  a.SelectCommand.Parameters.AddWithValue("@empLoginId", u);
                                  a.SelectCommand.Parameters.AddWithValue("@coType", "CLIENT");

                              // Use DataAdapter to fill DataTable
                                  DataTable t = new DataTable();
                                  a.Fill(t);

                                  GridView1.DataSource = t; // <-- From your designer
                                  GridView1.DataBind();
                                  con1.Close();
                                  if (t.Rows.Count > 0)
                                  {
                                      GridView1.Visible = true;
                                  }
                              }
                          }
                     }
                 }
           
                protected void Button1_Click(object sender, EventArgs e)
                 {
                      set = "1";
                      string u = DropDownList1.SelectedValue.ToString().ToUpper();
                      using (SqlConnection con1 = new SqlConnection())
                      {
                          con1.Open();
                          // Create new DataAdapter
                          using (SqlDataAdapter a = new SqlDataAdapter("SELECT [targetDate],[coType], [meetDate], [clientname], [pocName1], [coStatus], [prodname], [empLoginId] FROM [coMet] WHERE (([coType] = @coType) AND (([empLoginId]= @empLoginId) or ([empLoginId]= @empLogin)))", con1))
                          {
                              a.SelectCommand.Parameters.AddWithValue("@empLoginId", u);
                              a.SelectCommand.Parameters.AddWithValue("@empLogin", u.ToString().ToLower());
                              a.SelectCommand.Parameters.AddWithValue("@coType", "CLIENT");

                              // Use DataAdapter to fill DataTable
                              DataTable t = new DataTable();
                              a.Fill(t);

                              GridView1.DataSource = t; // <-- From your designer
                              GridView1.DataBind();
                              con1.Close();
                              if (t.Rows.Count > 0)
                              {
                                  GridView1.Visible = true;
                              }
                          }
                      }
               }
           
            protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
              {
                 GridView1.PageIndex = e.NewPageIndex;
                  Button1_Click(sender, e);
             }

           
            protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
             {
              }



Somewhere ... there is some error - probably even a silly one ... but i'm an amateur coder :)
so please help out .. :)

p.s. the new sqlconnection part as been edited out .. for obvious reasons .. so that is not an error ....
Posted
Updated 27-Jun-13 4:07am
v3

try this :

C#
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
             {
                GridView1.PageIndex = e.NewPageIndex;
                GridView1.DataBind();
            }
 
Share this answer
 
Comments
vsdaking 27-Jun-13 7:53am    
i'll give it a try ... but that already happens when Button1_Click() is called ...
vsdaking 27-Jun-13 8:11am    
no bro ... in fact, the grid view disappears altogether. DataBind() will definitely be required in the block ... but after we give it a data source i believe ..
but thanks nonetheless .. :)
any other ideas ?
[no name] 27-Jun-13 8:16am    
make sure you have allow paging property of grid view is true.
vsdaking 27-Jun-13 8:18am    
done .. but the problem still persists
vsdaking 27-Jun-13 8:21am    
i noticed, this seems to happen on my pages where the grid views are filled up based on some values selected in drop down lists. Elsewhere, your solution is perfect. Are we supposed to do write some special code in this kind of a case ?
C#
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    Gridview1.DataBind();
}
 
Share this answer
 
Comments
vsdaking 27-Jun-13 8:13am    
I tried that Code_Hunter143. The grid view did not return any data only. We need to specify the data source too i guess. Thanks .. and any other suggestions ?
Charan_Kumar 5-Jul-13 7:15am    
make sure that page index event is firing or not????
try with debugger...
Close your gridview in an update panel ,may be it will work
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900