LinkBut​​ton Commandargument给出了所有其他页面中第一页的值(Gridview分页)

时间:2011-07-20 14:04:02

标签: c# asp.net gridview commandargument

我正在使用GridView以及自定义分页来显示数据。在我显示删除记录的链接(linkBut​​ton)的其中一列中,我将记录的ID传递给后端函数。我正在为第1页中的所有记录正确获取commandargument值,但是从第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页的值吗?

1 个答案:

答案 0 :(得分:1)

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

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