c# - 从存储过程填充 gridview 的问题

标签 c# asp.net

我有一个 Gridview,我从一个接受 3 个参数的存储过程中填充它。第一个参数从表单变量 Request.Form["uid"] 获取其值,第二个和第三个参数从 DropDownLists SelectedValue 属性获取其值。如果未从 DropDownLists 中选择任何内容,则第二个和第三个参数应接受 NULL。我还将参数的 ConvertEmptyStringToNull 属性设置为 true。 Gridview 仍然没有填充。但是当我手动传递参数并从 SQL Server Management Studio 以及 Visual Studio 的测试查询窗口执行存储过程时,它工作正常。

我的 Gridview SQLDataSource 看起来像这样:

<asp:SqlDataSource ID="SqlGVDownloadHistory" CancelSelectOnNullParameter="false" runat="server" ConnectionString="<%$ ConnectionStrings:xxx %>"
            SelectCommand="spDHR" SelectCommandType="StoredProcedure">
            <SelectParameters>
                <asp:FormParameter FormField="uid" Name="UIDUser" Type="String" />
                <asp:ControlParameter ControlID="ddlSelectDocument" Name="FileName"
                    PropertyName="SelectedValue" Type="String" ConvertEmptyStringToNull="true" />
                <asp:ControlParameter ControlID="ddlSelectCCO" Name="UIDSelect" PropertyName="SelectedValue"
                    Type="String" ConvertEmptyStringToNull="true" />
            </SelectParameters>
        </asp:SqlDataSource>

请帮忙!!

最佳答案

试试这个。

<asp:SqlDataSource ID="SqlGVDownloadHistory" runat="server" 
            ConnectionString="<%$ ConnectionStrings:xxx %>" 
            SelectCommand="spDHR"
            SelectCommandType="StoredProcedure" 
            OnSelecting="SqlGVDownloadHistory_Selecting">
    <SelectParameters>
        <asp:FormParameter FormField="uid" Name="UIDUser" Type="String" />
    </SelectParameters>
</asp:SqlDataSource>

关于代码隐藏

protected void SqlGVDownloadHistory_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
    SqlGVDownloadHistory.SelectParameters.Remove("FileName");
    SqlGVDownloadHistory.SelectParameters.Remove("UIDSelect");
    if (ddlSelectDocument.SelectedIndex > 0)
    {
        SqlGVDownloadHistory.SelectParameters.Add("FileName", ddlSelectDocument.SelectedValue);
    }
    if (ddlSelectCCO.SelectedIndex > 0)
    {
        SqlGVDownloadHistory.SelectParameters.Add("UIDSelect", ddlSelectCCO.SelectedValue);
    }
}

想法是在数据检索操作发生之前设置参数。

根据 MSDN documentation on COntrolParameters ,我没有看到 asp:DropDownList Selectedvalue 被设置为 ControlValuePropertyAttribute

关于c# - 从存储过程填充 gridview 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6741618/

相关文章:

asp.net - 选择日期时如何关闭 Bootstrap 日期时间选择器

asp.net - 依赖项未加载或部分加载

jquery - 单击按钮时仅显示三个 div 中的一个并隐藏其他两个

c# - ASP.Net 中的 CSS、主题和皮肤是什么?

c# - 首次操作调用未调用 JwtBearerEvents.OnMessageReceived

c# linq,按索引对不同值求和

c# - 在没有事件源注册的情况下写入 Windows 应用程序事件日志

c# - 使用c#将图像插入到excel单元格中

css - 以 % 表示的 divs 高度在 ie 中不起作用

javascript - TableRow 中的隐藏字段 aspx