0

我正在使用 GridView 和自定义分页来显示数据。在我显示删除记录的链接 (linkBut​​ton) 的列之一中,我将记录的 ID 传递给后端函数。我为第 1 页中的所有记录正确获取命令参数值,但从第 2 页开始,我得到与第 1 页相同的值,而不是第 2 页记录的值。

下面是 ASP 代码

 <form id="form1" runat="server">
<div class="FilterDiv">Page Size:
<asp:DropDownList ID="ddlPageSize" runat="server" AutoPostBack="true" OnSelectedIndexChanged="PageSize_Changed">
<asp:ListItem Text="4" Value="4" />
<asp:ListItem Text="2" Value="2" />
<asp:ListItem Text="1" Value="1" />
</asp:DropDownList>
</div>

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" CssClass="standard-table" 
 AllowSorting="true" OnSorting="SortResultBy" OnRowDataBound="GridViewRowEventHandler">
<HeaderStyle CssClass="standard-tableHead" />
  <RowStyle CssClass="standard-tableBody" />
<Columns>
  <asp:BoundField DataField="newscode" HeaderText="News Code" SortExpression="newscode" />
  <asp:BoundField DataField="newstitle" HeaderText="News Title" SortExpression="newstitle" />
  <asp:BoundField DataField="newsdesc" HeaderText="News Description"  ItemStyle-Width="40%" />
  <asp:BoundField DataField="Created" HeaderText="Created Date" SortExpression="Created" />
  <asp:BoundField DataField="status" HeaderText="Status" />
  <asp:TemplateField HeaderText="Actions">
    <ItemTemplate>
     <asp:HyperLink ID="Hyperlink2" runat="server" Text='Details' NavigateUrl='<%# Eval("newsid","newsdetails.aspx?idnews={0}") %>'>
     </asp:HyperLink> &nbsp;
     <asp:HyperLink ID="Hyperlink1" runat="server" Text='Edit' NavigateUrl='<%# Eval("newsid","editnews.aspx?idnews={0}") %>'>
     </asp:HyperLink> &nbsp;
     <asp:LinkButton ID="NewsDeletelink" OnCommand="DeleteNewsbyID" CommandArgument='<%# Eval("newsid") %>' runat="server" EnableViewState="true"> Delete </asp:LinkButton>

    </ItemTemplate>
  </asp:TemplateField>
</Columns>
</asp:GridView>
<div class="pagination">
<asp:Repeater ID="rptPager" runat="server">
<ItemTemplate>
<asp:LinkButton ID="lnkPage" runat="server" Text = '<%#Eval("Text") %>' CommandArgument = '<%# Eval("Value") %>' Enabled = '<%# Eval("Enabled") %>' OnClick = "Page_Changed"></asp:LinkButton>  
</ItemTemplate>
</asp:Repeater>
</div> 

</form>

数据库中的记录是根据 SP 的 PageIndex 和 PageSize 获取的,并且数据绑定到 gridview,如下所示。

SqlCommand myCommand = new SqlCommand("getAllNews", myConnection);
        myCommand.CommandType = CommandType.StoredProcedure;
        myCommand.Parameters.Add("@sortby", SqlDbType.NChar).Value = SortBy;
        myCommand.Parameters.AddWithValue("@PageIndex", pageIndex);
        myCommand.Parameters.AddWithValue("@PageSize", int.Parse(ddlPageSize.SelectedValue));
        myCommand.Parameters.Add("@RecordCount", SqlDbType.Int, 4);
        myCommand.Parameters["@RecordCount"].Direction = ParameterDirection.Output;
        //SqlDataAdapter dataAdapter = new SqlDataAdapter("select * from news order by " + SortValue, myConnection);
        SqlDataAdapter dataAdapter = new SqlDataAdapter(myCommand);
        // create the DataSet
        DataSet dataSet = new DataSet();
        // fill the DataSet using our DataAdapter
        dataAdapter.Fill(dataSet, "news");

        GridView1.DataSource = dataSet;

        GridView1.DataBind();
        int recordCount = Convert.ToInt32(myCommand.Parameters["@RecordCount"].Value);
        this.PopulatePager(recordCount, pageIndex);
protected void GridViewRowEventHandler(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if (e.Row.Cells[4].Text == "1")
        {
            e.Row.Cells[4].Text = "<a>Published</a> ";
        }
        else
        {
            e.Row.Cells[4].Text = "<a>Draft</a> ";
        }
        if (e.Row.Cells[2].Text.Length >= 50)
        {
            e.Row.Cells[2].Text = e.Row.Cells[2].Text.Substring(0, 50) + "...";
        }

    }
}

有人能弄清楚为什么我在所有页面中都得到第 1 页的值吗?

4

1 回答 1

1

我知道这有点老了,但我也遇到了类似的问题。

为了获得所有页面的正确行数据,挂钩到 RowDataBound 事件并在那里设置 LinkBut​​ton 的 CommandArgument。

于 2012-05-31T12:07:01.567 回答