单击时页面索引不会更改 [英] Page Index Not Changing on Click

查看:63
本文介绍了单击时页面索引不会更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好..

我有一个带有下拉列表,按钮和网格视图的页面。根据所选的下拉列表选项,网格视图将从数据库中填充。当返回超过1页的多个条目时,我遇到的问题出现了 - 在点击第二页或另一页索引时,页面再次加载但是网格视图页索引和返回的行保持不变。



代码如下(我只提交相关代码,因为还有许多其他细节 - 与此无关 - 在页面上):



.aspx代码



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

<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>




</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>





.aspx.cs代码如下 -





The .aspx.cs code is as follows -

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)
             {
              }







某处...有一些错误 - 可能甚至是一个愚蠢的...但我是一个业余编码器:)

所以请帮助... :)



ps新的sqlconnection部分已被编辑出来......原因很明显..所以这不是错误....




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 ....

推荐答案

试试这个:



try this :

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
             {
                GridView1.PageIndex = e.NewPageIndex;
                GridView1.DataBind();
            }


protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    Gridview1.DataBind();
}


在更新面板中关闭gridview,可能会有效
Close your gridview in an update panel ,may be it will work


这篇关于单击时页面索引不会更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆