Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在代码隐藏中创建SelectCommand,其工作原理与在SQLDataSource中创建相同_C#_Asp.net_Webforms_Code Behind_Sqldatasource - Fatal编程技术网

C# 在代码隐藏中创建SelectCommand,其工作原理与在SQLDataSource中创建相同

C# 在代码隐藏中创建SelectCommand,其工作原理与在SQLDataSource中创建相同,c#,asp.net,webforms,code-behind,sqldatasource,C#,Asp.net,Webforms,Code Behind,Sqldatasource,我正在使用asp.NET webforms和gridview在我的网站上创建大型数据表。我还有一个非常简单的代码隐藏方法,可以将整个gridview下载到excel文件中。当我在sqlDataSource中创建selectCommand时,这一切都非常有效。我的问题是我想在代码隐藏中创建一个SelectCommand,这样我就可以添加很多参数,使它更具动态性。我知道您也可以在sqlDataSource SelectCommand中添加参数,但对于我想要的代码隐藏,这要简单得多 在code beh

我正在使用asp.NET webforms和gridview在我的网站上创建大型数据表。我还有一个非常简单的代码隐藏方法,可以将整个gridview下载到excel文件中。当我在sqlDataSource中创建selectCommand时,这一切都非常有效。我的问题是我想在代码隐藏中创建一个SelectCommand,这样我就可以添加很多参数,使它更具动态性。我知道您也可以在sqlDataSource SelectCommand中添加参数,但对于我想要的代码隐藏,这要简单得多

在code behind中创建的selectCommand可以完美地工作并显示gridview。问题是当我尝试下载到excel时,excel文件是空的。换言之,gridview中的数据不会被转移。我想这一定与我创建select命令的方式有关。。。我就是这样做的

Aspx文件:

<asp:SqlDataSource ID="RegionCompliance" Runat="server"
    ConnectionString="<%$ ConnectionStrings:ApplicationServices %>">        
</asp:SqlDataSource>   

<%  
    SetSelectCommand(); // this is where the select command is created
%>

<h2>
    Download To:    
    <asp:LinkButton  ID="Button1" runat="server" OnClick="DownloadToExcel" Text="Excel" />
</h2>


    <asp:GridView 

        ID="GridView1" 
        DataSourceID="RegionCompliance" 
        DataKeyNames="Region">

        <Columns>
            <asp:BoundField ReadOnly="true" HeaderText="Region" DataField="Region"></asp:BoundField>
        </Columns>
   </asp:GridView>

如果我在sqlDataSource中构建了select命令,它本身就会工作得很好。。。有人知道为什么不允许excel文件使用SELECT命令和gridview数据吗?您需要使用页面方法

protected void Page_Load(object sender, EventArgs e)
{
    RegionCompliance.SelectCommand = "SELECT region FROM tablename"
}

您不应该尝试像那样在线调用方法-这是典型的ASP功能。

代码块是,这对于excel来说太晚了(您可以通过调试器检查是否调用了它)。您可以在创建excel文件时显式调用此方法,也可以调用
DataBind()

I reccalled RegionCompliance.SelectCommand=sqlCommand;在excel下载方法中,这对我很有用。。。谢谢
protected void Page_Load(object sender, EventArgs e)
{
    RegionCompliance.SelectCommand = "SELECT region FROM tablename"
}