我有一个动态gridview与分页。我想根据页面索引加载数据。 [英] I have a dynamic gridview with paging. I want to load the data based on page index.

查看:59
本文介绍了我有一个动态gridview与分页。我想根据页面索引加载数据。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个gridview,文本框和下拉列表将动态生成。



如果我有10000条记录,则载入10000条记录。但我不想要那个。

我需要根据页面索引获取记录。如果我在第1页,它应该得到并加载1到50个记录数据,如果我是第3页,它应该得到并加载100到150这样。



如何我可以做到这一点吗?请建议。



我尝试过:



这个网格我定义的视图和我设置属性allo paging true。



< asp:GridView ID =gvDUSrqstrunat =serverAutoGenerateColumns =FalseOnRowDataBound =OnRowDataBound
GridLines =VerticalBorderColor =WhiteAllowPaging =truePageSize =50>
< HeaderStyle BackColor =#009530CssClass =lblClsGridHdrHorizo​​ntalAlign =Center/>
< RowStyle Font-Names =ArialFont-Size =8ptForeColor =#009530/>
< / asp:GridView>





这是我的页面索引更改代码。



受保护的子OnPageIndexChanging(ByVal sender As Object,ByVal e As GridViewPageEventArgs)处理gvDUSrqst.PageIndexChanging 
gvDUSrqst.PageIndex = e.NewPageIndex
btnSaveDUS_Click(btnSaveDUS ,e)
Me.BindGrid()
End Sub





这是bindgrid方法。



 Private Sub BindGrid()
Dim dt As New DataTable()
Dim i As Integer = 0
Dim fld As String =
Sql =选择字段,情况是字段='MATNR'然后'X'当TASK_CODE ='& lblTsk.Text& '那''''''''''''''''''''''''''''''''''''''''''''''''' '& lbletcode.Text& '& AND TECODE =& '& lbltecode.Text& '& ORDER BY 2,TASK_CODE,SEQ
使用OracleConnect作为新的OracleConnection(OraConString)
OracleConnect.Open()
dt =新DataTable()
cmdOra =新的OracleCommand(Sql ,OracleConnect)
rdrOra = cmdOra.ExecuteReader
dt.Load(rdrOra)
For i = 0 To dt.Rows.Count - 1
if i = 0 Then
fld = dt.Rows(0).Item(0)
否则
fld = fld& ,& dt.Rows(i).Item(0)
End if
Next
dt.Dispose()
Sql =SELECT& fld& FROM TABLE2 WHERE WFCODE ='& lblWFCode.Text& '
'Sql =SELECT * FROM(SELECT& fld&,ROWNUM R FROM& ViewState(TABLE_NAME)&WHERE WFCODE ='& lblWFCode.Text& ;')其中r> &安培; ViewState(pageindex)& 和r< &安培; ViewState(pageindex)+ 51&
dt =新DataTable
cmdOra =新OracleCommand(Sql,OracleConnect)
rdrOra = cmdOra.ExecuteReader
dt.Load(rdrOra)
如果dt.Rows .Count> 0然后
gvDUSrqst.DataSource = dt
gvDUSrqst.DataBind()
结束如果
OracleConnect.Close()
结束使用
End Sub

解决方案

 Sql =  选择字段,当字段='MATNR'然后'X'当TASK_CODE ='& lblTsk.Text&  'THEN'Y'ELSE'Z'END FROM TABLE1,其中ETCODE =&  '& lbletcode.Text&  '&   AND TECODE =&  '& lbltecode.Text&  '&   ORDER BY 2,TASK_CODE,SEQ 



不是你问题的解决方案,而是你遇到的另一个问题。

永远不要通过连接字符串来构建SQL查询。迟早,您将使用用户输入来执行此操作,这会打开一个名为SQL注入的漏洞,这对您的数据库很容易并且容易出错。

名称中的单引号你的程序崩溃。如果用户输入像Brian O'Conner这样的名称可能会使您的应用程序崩溃,那么这是一个SQL注入漏洞,崩溃是最少的问题,恶意用户输入,并且它被提升为具有所有凭据的SQL命令。

SQL注入 - 维基百科 [ ^ ]

SQL注入 [ ^ ]

按示例进行SQL注入攻击 [ ^ ]

PHP:SQL注入 - 手册 [ ^ ]

SQL注入预防备忘单 - OWASP [ ^ ]


I have a gridview in that textboxes and dropdown lists will generate dynamically.

If i have 10000 records, it is loading 10000 records. But i dont want that.
I need to get the records based on page index. If i was in page 1 it should get and load 1 to 50 records data and if i was page 3 it should get and load 100 to 150 like that.

How can i achieve this. Please suggest.

What I have tried:

This the grid view i defined and i set the property allo paging true.

<asp:GridView ID="gvDUSrqst" runat="server" AutoGenerateColumns="False" OnRowDataBound="OnRowDataBound"
                GridLines="Vertical" BorderColor="White" AllowPaging = "true" PageSize="50">
                <HeaderStyle BackColor="#009530" CssClass="lblClsGridHdr" HorizontalAlign="Center" />
                <RowStyle Font-Names="Arial" Font-Size="8pt" ForeColor="#009530" />
            </asp:GridView>



This is my page index changed code.

Protected Sub OnPageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs) Handles gvDUSrqst.PageIndexChanging
        gvDUSrqst.PageIndex = e.NewPageIndex
        btnSaveDUS_Click(btnSaveDUS, e)
        Me.BindGrid()
    End Sub



This is bindgrid method.

Private Sub BindGrid()
        Dim dt As New DataTable()
        Dim i As Integer = 0
        Dim fld As String = ""
        Sql = "SELECT FIELD,CASE WHEN FIELD='MATNR' THEN 'X' WHEN TASK_CODE='" & lblTsk.Text & "' THEN 'Y' ELSE 'Z' END FROM TABLE1 where ETCODE=" & "'" & lbletcode.Text & "'" & " AND TECODE=" & "'" & lbltecode.Text & "'" & "  ORDER BY 2,TASK_CODE,SEQ"
        Using OracleConnect As New OracleConnection(OraConString)
            OracleConnect.Open()
            dt = New DataTable()
            cmdOra = New OracleCommand(Sql, OracleConnect)
            rdrOra = cmdOra.ExecuteReader
            dt.Load(rdrOra)
            For i = 0 To dt.Rows.Count - 1
                If i = 0 Then
                    fld = dt.Rows(0).Item(0)
                Else
                    fld = fld & "," & dt.Rows(i).Item(0)
                End If
            Next
            dt.Dispose()
            Sql = "SELECT " & fld & " FROM TABLE2 WHERE WFCODE='" & lblWFCode.Text & "'"
            'Sql = "SELECT * FROM (SELECT " & fld & ", ROWNUM R FROM " & ViewState("TABLE_NAME") & " WHERE WFCODE='" & lblWFCode.Text & "') where r>" & ViewState("pageindex") & " and r<" & ViewState("pageindex") + 51 & ""
            dt = New DataTable
            cmdOra = New OracleCommand(Sql, OracleConnect)
            rdrOra = cmdOra.ExecuteReader
            dt.Load(rdrOra)
            If dt.Rows.Count > 0 Then
                gvDUSrqst.DataSource = dt
                gvDUSrqst.DataBind()
            End If
            OracleConnect.Close()
        End Using
    End Sub

解决方案

Sql = "SELECT FIELD,CASE WHEN FIELD='MATNR' THEN 'X' WHEN TASK_CODE='" & lblTsk.Text & "' THEN 'Y' ELSE 'Z' END FROM TABLE1 where ETCODE=" & "'" & lbletcode.Text & "'" & " AND TECODE=" & "'" & lbltecode.Text & "'" & "  ORDER BY 2,TASK_CODE,SEQ"


Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]


这篇关于我有一个动态gridview与分页。我想根据页面索引加载数据。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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