GridVIew在page_load上有一个Databind,在点击一个按钮时有另一个。在第二个数据绑定时,如果页面编号单击转到第一个数据绑定

时间:2014-11-03 19:08:33

标签: asp.net vb.net gridview data-binding

我有一个带有分页的GridView,并在Page_Load上以编程方式对其进行数据绑定。此外,我有一个搜索按钮,单击它时数据绑定到不同的sql命令。如果单击搜索按钮并检索多个页面,如果单击第2页,则会检索第一个Databind()的数据。我如何使用GridView1.PageIndexChanging修复它,这是我在第一个Databind上使用的分页? Anyhelp将非常感激。它甚至可以吗?

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
    AutoGenerateColumns="False" PageSize="100">
 <Columns>
    <asp:BoundField DataField="fldEmployeeID" HeaderText="EmployeeID" />
    <asp:BoundField DataField="fldAbsentDate" HeaderText="AbsentDate" />
    <asp:BoundField DataField="fldAbsentCode" HeaderText="AbsentCode" />
    <asp:BoundField DataField="fldRuleViolationWarningType" HeaderText="Rule Violation Warning" />
    <asp:BoundField DataField="fldRuleViolationIssueDate" HeaderText="Rule Violation Issue Date" />
    <asp:BoundField DataField="fldLOAEndDate" HeaderText="LOA End Date" />
</Columns>

</asp:GridView>

vb.net

   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim SqlDataSource1 As SqlDataSource = New SqlDataSource()
    SqlDataSource1.ID = "SqlDataSource1"
    Page.Controls.Add(SqlDataSource1)
    SqlDataSource1.ConnectionString = "your connection string"
    'SqlDataSource1.SelectCommand = "SELECT * FROM [tblAbsences] WHERE [fldEmployeeID]=38"
    SqlDataSource1.SelectCommand = "SELECT * FROM [tblAbsences] ORDER BY [fldEmployeeID], [fldAbsentDate], [fldAbsentCode]"
    GridView1.DataSource = SqlDataSource1
    GridView1.DataBind()
End Sub

Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs) Handles GridView1.PageIndexChanging
    GridView1.PageIndex = e.NewPageIndex
    GridView1.DataBind()
End Sub

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim SqlDataSource2 As SqlDataSource = New SqlDataSource()
    SqlDataSource2.ID = "SqlDataSource2"
    Page.Controls.Add(SqlDataSource2)
    SqlDataSource2.ConnectionString = "your connection string"
    SqlDataSource2.SelectCommand = "SELECT * FROM [tblAbsences] WHERE [fldAbsentDate] BETWEEN '7-03-2014' AND '8-21-2014' ORDER BY [fldAbsentDate]"
    'SqlDataSource2.SelectCommand = "SELECT * FROM [tblAbsences] ORDER BY [fldEmployeeID], [fldAbsentDate], [fldAbsentCode]";
    GridView1.DataSource = SqlDataSource2
    GridView1.DataBind()

End Sub

1 个答案:

答案 0 :(得分:1)

在主窗体中添加一个不可见的html标签/文本框...

<asp:label runat="Server" ID="SDS2" visible="false" text="0" />

页面加载看起来如下所示,并且可能会因为在其他地方加入SDS2而受益。同一IF语句中的条款。

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If SDS2.Text = "0"
        Dim SqlDataSource1 As SqlDataSource = New SqlDataSource()
        SqlDataSource1.ID = "SqlDataSource1"
        Page.Controls.Add(SqlDataSource1)
        SqlDataSource1.ConnectionString = "your connection string"
        'SqlDataSource1.SelectCommand = "SELECT * FROM [tblAbsences] WHERE [fldEmployeeID]=38"
        SqlDataSource1.SelectCommand = "SELECT * FROM [tblAbsences] ORDER BY [fldEmployeeID],       [fldAbsentDate], [fldAbsentCode]"
        GridView1.DataSource = SqlDataSource1

        GridView1.DataBind()
    Else
        'Call the SDS2 stuff here
    End If
End Sub

在按钮单击处理程序中添加此行以将其设置为使用SDS2。

SDS2.Text = "1"