开发者

LinkButton Commandargument is giving values of the first page in all the other pages (Gridview pagination)

I am using GridView along with custom pagination to show data. In one of the columns where I am showing a link (linkButton) to delete the record, I am passing ID of the record to the backend function. I am getting the commandargument value correctly for all the records in page 1 but from page 2 onwards Im getting the same values as page 1 instead of values of page 2 records.

Below is the ASP code

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

The records from the database is fetched based on the PageIndex and PageSize from an SP and data is bound to gridview as below.

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);
        myComman开发者_JAVA技巧d.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) + "...";
        }

    }
}

Can somebody able to make out why I am getting the values of page 1 in all the pages?


I know this is a bit old but I just had a similar problem.

In order to get the correct row data for all pages, hook into the RowDataBound event and set the LinkButton's CommandArgument there.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜