GridView中的LinkBut​​ton没有选择正确的值

时间:2013-02-13 09:43:35

标签: asp.net

我正在使用链接按钮显示网格视图中所选报告ID的pdf报告。在页面加载时,我将数据源与Gridview绑定。通过单击链接按钮,它从第0列获取正确的报告ID并显示报告。排序网格后,网格视图显示已排序的数据。现在,如果我单击链接按钮,它不会从第0列中选择更改的值,而是在排序之前选择该值。

说:在排序之前,第0列的值为1,2,3,4,5。如果我在排序之前单击第3行的链接按钮,它将从第0列中选择值3.排序后它是3,4,5,2,1。现在,如果我点击第3行的链接按钮,它仍然是选择值3而不是5.你能帮我吗。

以下是我的代码: ASPX:

CS:

protected void Page_Load(object sender, EventArgs e)
{
     bindGrid();
}
protected void GvStockTakingReport_Sort(object sender, GridViewSortEventArgs e)
    {

            GvStockTakingReport.DataSource = null;
            GvStockTakingReport.DataBind();
            GvStockTakingReport.Dispose();
            DataSet ds = StockTakingList.BindStocktakingReportGrid();
            DataTable dtSortTable = ds.Tables[0];
            if (dtSortTable != null)
            {
                DataView dv = new DataView(ds.Tables[0]);
                dv.Sort = e.SortExpression + " " + getSortDirectionString();
                //ViewState["sortExpression"] = e.SortExpression;
                //Session["sortExpression"] = e.SortExpression;
                GvStockTakingReport.DataSource = dv;
                GvStockTakingReport.DataBind();
            }                      
       }

   protected void PrintReport(object sender, CommandEventArgs e)
    {

            LinkButton lkButton = (LinkButton)sender;
            GridViewRow item = (GridViewRow)lkButton.NamingContainer;

            string Id = (item.FindControl("lblstk_id") as Label).Text;

            //Getting value for Id    
            string Id1 = e.CommandArgument.ToString();


            int rowIndex = Convert.ToInt32((string)e.CommandArgument);


            //string Id1 = Convert.ToString(GvStockTakingReport.Rows[rowIndex].Cells[0].Text);

            string Id2 = ((Label)(GvStockTakingReport.Rows[rowIndex].FindControl("lblstk_id"))).Text;

            //Getting value for Id

            string virtualPath = string.Format("~/{0}/{1}{2}", Portal.Business, Portal.Core.Profile.ReportsDirectory, "General/RPT_03002_StockTakinReport.rpt");
            string physicalPath = Server.MapPath(virtualPath);

            using (ReportDocument report = new ReportDocument())
            {
                report.Load(physicalPath);
                Core.Security.CrystalReportLogOn(report, (SqlConnectionStringBuilder)Portal.Core.Profile.ConnectionStrings["MTServer"]);
                report.SetParameterValue("@STK_Id", Id);
                report.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Page.Response, true, "Stock Taking report");
            }          
    }

1 个答案:

答案 0 :(得分:0)

问题是每当我点击页面加载调用的链接按钮 bindgrid方法,在printreport方法调用之前将gridview重置为原始排序。 我已经在bindgrid方法中包含了排序逻辑,并在下面解决了这个问题。

   public void bindGrid()
    {
        try
        {
            DataSet ds = StockTakingList.BindStocktakingReportGrid();
            GvStockTakingReport.DataSource = ds;
            GvStockTakingReport.DataBind();

            //To make sure the grid remains with sorting order while pageload method invokes otherwise it will reset to original order
            DataView dv = new DataView(ds.Tables[0]);
            if (dv != null)
            {
                if (ViewState["sortExpression"] == null)
                {
                    ViewState["sortExpression"] = "STK_Id"; //First time it will sort by STK_ID i.e on first time page loading
                }

                if (ViewState["sortDirection"] == null)
                {
                    ViewState["sortDirection"] = "ASC";//First time it will sort ascending i.e on first time page loading
                }

                dv.Sort = ViewState["sortExpression"].ToString() + " " + ViewState["sortDirection"].ToString();
                GvStockTakingReport.DataSource = dv;
                GvStockTakingReport.DataBind();
            }
        }
        catch (Exception ex)
        {
            error.Visible = true;
            error.Text = ex.Message.ToString();
        }
    }