当绑定在后面的代码中时,GridView丢失数据 [英] gridview losing data when bound in code behind

查看:53
本文介绍了当绑定在后面的代码中时,GridView丢失数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个gridview,它是从后面的代码的数据表中加载的.当我尝试更改页面时,gridview消失了.在调试中逐步完成之后,数据源为空白.如何维护数据并允许分页? (请注意,gridview是从后面的代码填充的原因,根据用户的不同,有几种可能的sql语句适用于同一gridview,我只发布了其中之一).

这是来自ASPX的gridview:

I have a gridview that is loaded from a datatable in code behind. When I try to change pages the gridview disappears. After stepping through it in debug the datasouce is blank. How do I maintain the data and allow paging? (Note the reason the gridview is populated from code behind, there are several possible sql''s statements that apply to the same gridview depending on the user, I only posted one of them)

Here is the gridview from the ASPX:

<asp:GridView ID="GridViewCertificationsDue" runat="server"  ForeColor="Black"

    AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="ID" AllowPaging="true" pagesize="10"

    >
    <Columns>
        <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" ItemStyle-Width="150px" />
        <asp:BoundField DataField="WorkArea" HeaderText="Work Area" SortExpression="WorkArea" ItemStyle-Width="150px" />
        <asp:BoundField DataField="Site" HeaderText="Site" SortExpression="Site" ItemStyle-Width="200px" />
        <asp:BoundField DataField="Item" HeaderText="Item" SortExpression="Item" ItemStyle-Width="250px" />
        <asp:BoundField DataField="Remarks" HeaderText="Remarks" SortExpression="Remarks"  ItemStyle-Width="250px"/>
        <asp:BoundField DataField="Due" HeaderText="Due" ReadOnly="True" dataformatstring="{0:dd MMM yyyy}" SortExpression="Due" ItemStyle-Width="100px"/>
    </Columns>
</asp:GridView>




要填充的代码:




Code to populate:

If Not IsPostBack Then


Me.GridViewCertificationsDue.Visible = True
sqlstr = "SELECT zEdits.ID, zEdits.LastName + ', ' + zEdits.FirstName AS Name, zEdits.Site,  tblCertifications.Item,  tblCertifications.Reoccurrence,  tblCertifications.DateQualified, " & _
    " tblCertifications.PID, zEdits.WorkArea,  tblCertifications.Remarks, tblSites.ProgramGroup,  tblCertifications.due FROM zEdits INNER JOIN  tblCertifications ON zEdits.ID =  tblCertifications.PID INNER JOIN tblSites " & _
    "ON zEdits.Site = tblSites.SiteName WHERE ((zEdits.site = '" + Session("SessionSite") + "') and ( tblCertifications.due < DATEADD(d, 30, GETDATE()))) ORDER BY  tblCertifications.due"
dt = GetData(sqlstr)
If (dt.Rows.Count > 0) Then
    Me.GridViewCertificationsDue.DataSource = dt
    Me.GridViewCertificationsDue.DataBind()
End If


End If
End Sub





Function GetData(ByVal querystring As String) As DataTable
    Dim connectionString As String = ConfigurationManager.ConnectionStrings("PKLConnectionString").ConnectionString
    Dim dt As New DataTable

    
    Try
        Dim connection As New SqlConnection(connectionString)
        Dim adapter As New SqlDataAdapter(querystring, connection)
        adapter.Fill(dt)
    Catch ex As Exception

    End Try

    Return dt


End Function



页面索引:(这是不再存在数据的地方)



Page indexing: (this is where the data is no longer present)

Private Sub GridViewCertificationsDue_PageIndexChanging(sender As Object, e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridViewCertificationsDue.PageIndexChanging
    Me.GridViewCertificationsDue.PageIndex = e.NewPageIndex
    Me.GridViewCertificationsDue.DataBind()
End Sub

推荐答案



Hi,

During the
GridViewCertificationsDue_PageIndexChanging

事件中,您需要
重新填充并绑定数据.因为您在
内部获得的数据(如您在页面加载中所做的操作) pageload事件不在页面内.如果要将填充的数据插入到ViewState中.然后在

event you need
to repopulate and bind the data. Because the data(like what you have done in page load) which you have get inside the
pageload event is not inside the page. if you want insert populated data in to ViewState. then in

GridViewCertificationsDue_PageIndexChanging

事件中,
从viewstate获取数据数据,并通过设置datasource和databind方法重新绑定.

希望这可以帮助您解决问题.

event,
get data data from the viewstate and rebind by setting datasource and databind methods.

Hope this will help you to solve the problem.


在回发数据丢失期间,

因此,将其存储在Session中,
Session ["dt"] = dtdata
这里dtdata是具有要与gridview绑定的数据的数据表.

并修改PageIndexChanging事件,例如,
During Postback data is lost,

So store that in Session like,
Session["dt"]=dtdata
Here dtdata is the datatable having data which is to be binded with gridview.

And modify PageIndexChanging event like,
Private Sub GridViewCertificationsDue_PageIndexChanging(sender As Object, e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles    GridViewCertificationsDue.PageIndexChanging                                    
    Me.GridViewCertificationsDue.PageIndex = e.NewPageIndex
    Me.GridViewCertificationsDue.DataSource= (DataTable)Session["dt"]
    Me.GridViewCertificationsDue.DataBind()
End Sub


这篇关于当绑定在后面的代码中时,GridView丢失数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆