Note that there are some explanatory texts on larger screens.

plurals
  1. POLinkButton Commandargument is giving values of the first page in all the other pages (Gridview pagination)
    primarykey
    data
    text
    <p>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.</p> <p>Below is the ASP code</p> <pre><code> &lt;form id="form1" runat="server"&gt; &lt;div class="FilterDiv"&gt;Page Size: &lt;asp:DropDownList ID="ddlPageSize" runat="server" AutoPostBack="true" OnSelectedIndexChanged="PageSize_Changed"&gt; &lt;asp:ListItem Text="4" Value="4" /&gt; &lt;asp:ListItem Text="2" Value="2" /&gt; &lt;asp:ListItem Text="1" Value="1" /&gt; &lt;/asp:DropDownList&gt; &lt;/div&gt; &lt;asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" CssClass="standard-table" AllowSorting="true" OnSorting="SortResultBy" OnRowDataBound="GridViewRowEventHandler"&gt; &lt;HeaderStyle CssClass="standard-tableHead" /&gt; &lt;RowStyle CssClass="standard-tableBody" /&gt; &lt;Columns&gt; &lt;asp:BoundField DataField="newscode" HeaderText="News Code" SortExpression="newscode" /&gt; &lt;asp:BoundField DataField="newstitle" HeaderText="News Title" SortExpression="newstitle" /&gt; &lt;asp:BoundField DataField="newsdesc" HeaderText="News Description" ItemStyle-Width="40%" /&gt; &lt;asp:BoundField DataField="Created" HeaderText="Created Date" SortExpression="Created" /&gt; &lt;asp:BoundField DataField="status" HeaderText="Status" /&gt; &lt;asp:TemplateField HeaderText="Actions"&gt; &lt;ItemTemplate&gt; &lt;asp:HyperLink ID="Hyperlink2" runat="server" Text='Details' NavigateUrl='&lt;%# Eval("newsid","newsdetails.aspx?idnews={0}") %&gt;'&gt; &lt;/asp:HyperLink&gt; &amp;nbsp; &lt;asp:HyperLink ID="Hyperlink1" runat="server" Text='Edit' NavigateUrl='&lt;%# Eval("newsid","editnews.aspx?idnews={0}") %&gt;'&gt; &lt;/asp:HyperLink&gt; &amp;nbsp; &lt;asp:LinkButton ID="NewsDeletelink" OnCommand="DeleteNewsbyID" CommandArgument='&lt;%# Eval("newsid") %&gt;' runat="server" EnableViewState="true"&gt; Delete &lt;/asp:LinkButton&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; &lt;div class="pagination"&gt; &lt;asp:Repeater ID="rptPager" runat="server"&gt; &lt;ItemTemplate&gt; &lt;asp:LinkButton ID="lnkPage" runat="server" Text = '&lt;%#Eval("Text") %&gt;' CommandArgument = '&lt;%# Eval("Value") %&gt;' Enabled = '&lt;%# Eval("Enabled") %&gt;' OnClick = "Page_Changed"&gt;&lt;/asp:LinkButton&gt; &lt;/ItemTemplate&gt; &lt;/asp:Repeater&gt; &lt;/div&gt; &lt;/form&gt; </code></pre> <p>The records from the database is fetched based on the PageIndex and PageSize from an SP and data is bound to gridview as below.</p> <pre><code>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 = "&lt;a&gt;Published&lt;/a&gt; "; } else { e.Row.Cells[4].Text = "&lt;a&gt;Draft&lt;/a&gt; "; } if (e.Row.Cells[2].Text.Length &gt;= 50) { e.Row.Cells[2].Text = e.Row.Cells[2].Text.Substring(0, 50) + "..."; } } } </code></pre> <p>Can somebody able to make out why I am getting the values of page 1 in all the pages?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload