如何从refcursor在updatepanel中填充网格

时间:2008-11-20 16:17:03

标签: asp.net oracle

我正在使用ASP.NET 2.0和VS 2005.我需要在用户单击按钮后从Oracle refcursor在更新面板上填充网格。我有一个来自另一个项目的例子,但它非常复杂。有没有一种简单的方法可以在updatepanel中的网格中显示数据?我们不希望在用户单击按钮后首次打开选项卡时显示数据。

TIA, 颖

2 个答案:

答案 0 :(得分:0)

我在这里发布了一些代码,因为这些东西很难弄清楚和研究。这是使用Microsoft Oracle数据提供程序(我更喜欢ODP.NET)的一种简单方法:

<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OracleClient" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>GridView w/ Oracle Ref Cursor</title>
<style type="text/css">
    body {padding:25px;}
    .Button1 {margin:35px 0;}
</style>
<script runat="server" type="text/C#">
    protected void Page_Load(object sender, EventArgs e) {
    }

    protected void Button1_Click(object sender, EventArgs e) {
        var dataSet = new DataSet();
        // get connection string from web.config
        var connStr = ConfigurationManager.ConnectionStrings["ConnStr1"].ConnectionString;
        // create connection
        using (var conn = new OracleConnection(connStr)) {
            // create & define the parameter
            var refCursorParam = new OracleParameter();
            refCursorParam.ParameterName = "RET";
            refCursorParam.OracleType = OracleType.Cursor;
            refCursorParam.Direction = ParameterDirection.Output;
            // create & define the command
            var cmd = new OracleCommand();
            cmd.CommandText = "GET_ALL_EMPS";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(refCursorParam);
            cmd.Connection = conn;
            // use data adapter to fill dataset
            using (var adapter = new OracleDataAdapter(cmd))
                adapter.Fill(dataSet);
        }
        // set some gridview properties
        GridView1.AllowPaging = true;
        GridView1.PageSize = 5;

        // bind dataset to grid
        GridView1.DataSourceID = null;
        var dv = dataSet.Tables[0].AsDataView();
        // save dataview to session so gridview can be re-bound later
        Session["dataView"] = dv;
        GridView1.DataSource = dv;
        GridView1.DataBind();

        pCount.InnerText = "Total Row Count: " + dataSet.Tables[0].Rows.Count.ToString();

        // dispose of dataset
        dataSet.Dispose();
    }

    void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) {
        // get dataview from session
        var dv = (DataView)Session["dataView"];
        GridView1.DataSource = dv;
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataBind(); // re-bind data
    }
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<div>
    <asp:UpdatePanel id="updatepanel1" runat="server">
        <ContentTemplate>
            <asp:Button ID="Button1"
                runat="server"
                Text="Refresh GridView"
                OnClick="Button1_Click" />
            <p id="pCount" runat="server" />
            <asp:GridView ID="GridView1"
                          runat="server"
                          OnPageIndexChanging="GridView1_PageIndexChanging" />
        </ContentTemplate>
    </asp:UpdatePanel>
</div>
</form>
</body>
</html>

尝试一下,如果您有任何疑问,请告诉我。这里有一些很好的参考资料:

-gabe

答案 1 :(得分:0)