【问题标题】:GridView - Client Side "WHERE" clause in the SqlDataSource?GridView - SqlDataSource 中的客户端“WHERE”子句?
【发布时间】:2016-08-31 04:28:30
【问题描述】:

我有一个GridView 和一个包含一个按钮的TemplateField。此按钮打开一个模式窗口,其中包含另一个 GridView,如下所示:

Gridview1 中的模板字段:

<asp:TemplateField>
<ItemTemplate>
    <asp:Button ID="btnOpen" runat="server" Text="Show Gridview" OnClick="btnOpen_Click" data-toggle="modal" data-target="#myModal"/>
</ItemTemplate>

模态窗口:

<div class="modal" id="idModal">
        <div class="container">
            <div class="modal-header">
                <h1>Transaction Details<a class="close-modal" href="#">&times;</a></h1>
            </div>
            <div class="modal-body">
                <asp:GridView ID="gvDetail" runat="server" AutoGenerateColumns="false" DataSourceID="SqlgvDetail"
                OnRowDataBound="gvDetail_RowDataBound" CssClass="table table-hover table-bordered" EmptyDataText="No data to display.">
                    <Columns>
                        <asp:BoundField DataField="metalid" HeaderText="Metal ID"/>
                        <asp:BoundField DataField="enddate" HeaderText="End Date" DataFormatString="{0:dd-MM-yyyy}" />
                        <asp:BoundField DataField="startdate" HeaderText="Start Date" DataFormatString="{0:dd-MM-yyyy}" />
                        <asp:BoundField DataField="clientref" HeaderText="Client Ref" />
                        <asp:BoundField DataField="quantity" HeaderText="Quantity" DataFormatString="{0:N2}" />
                    </Columns>
                </asp:GridView>
            </div>
            <div class="modal-footer">
                <asp:Button ID="btn_close" runat="server" Text="OK" CssClass="close-modal btn-sm btn-primary"/>
            </div>
        </div>
    </div>
    <div class="modal-backdrop"></div>

GridView2SqlDataSource:

<asp:SqlDataSource ID="SqlgvDetail" runat="server" ConnectionString="<%$ ConnectionStrings:InventoryConnectionString %>"
    SelectCommand="SELECT td.metalid , td.enddate , td.startdate , td.clientref , td.quantity FROM trxdetail td">
</asp:SqlDataSource>

现在这段代码可以正常工作,并按预期打开带有SelectCommand 的模态窗口。但是,我需要根据 GridView1 中的行值添加 where 子句。例如。 ...WHERE td.clientref = GridView1.SelectedRow.Cells[0].Text

请帮忙!

编辑:模态窗口:

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">Modal title</h4>
                </div>
                <div class="modal-body">
                    <asp:GridView ID="gvDetail" runat="server" AutoGenerateColumns="false" DataSourceID="SqlgvDetail"
                    OnRowDataBound="gvDetail_RowDataBound" CssClass="table table-hover table-bordered" EmptyDataText="No data to display.">
                        <Columns>
                            <asp:BoundField DataField="metalid" HeaderText="Metal ID"/>
                            <asp:BoundField DataField="enddate" HeaderText="End Date" DataFormatString="{0:dd-MM-yyyy}" />
                            <asp:BoundField DataField="startdate" HeaderText="Start Date" DataFormatString="{0:dd-MM-yyyy}" />
                            <asp:BoundField DataField="clientref" HeaderText="Client Ref" />
                            <asp:BoundField DataField="quantity" HeaderText="Quantity" DataFormatString="{0:N2}" />
                        </Columns>
                    </asp:GridView>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    <button type="button" class="btn btn-primary">Save changes</button>
                </div>
            </div>
        </div>
    </div>

模态JS:

$(document).ready(function () {
        $("#btnOpen").click(function () {
            $("#myModal").modal();
        });
    });

【问题讨论】:

    标签: c# asp.net gridview selectcommand


    【解决方案1】:

    您实际上可以将&lt;asp:ControlParameter&gt; 设置为GridView 的SelectedValue。我想这就是你要找的。正如文档所说:

    作为更进一步的快捷方式,您可以直接使用 SelectedValue 属性确定所选行的第一个键字段的数据键值。

    因此,您可以将 GridView1 上的 DataKeyNames 值设置为您想在 WHERE 子句中使用的任何值。

    <asp:GridView ID="GridView1" runat="server" DataKeyNames="clientref"
        ...
    </asp:GridView>
    

    然后将其设置为 SqlDataSource 中的控制参数。

    <asp:SqlDataSource ID="SqlgvDetail" runat="server"
        ConnectionString="<%$ ConnectionStrings:InventoryConnectionString %>"
        SelectCommand="SELECT td.metalid, td.enddate, td.startdate, td.clientref , td.quantity 
                       FROM trxdetail td
                       WHERE clientref=@clientref">
        <SelectParameters>
            <asp:ControlParameter ControlID="GridView1"
                PropertyName="SelectedValue"
                Name="clientref"
                Type="Whatever type clientref is" />
        </SelectParameters>
    </asp:SqlDataSource>
    

    请记住,您需要确保 GridView1 中的行实际上被标记为 SelectedRow。您可以在按钮单击事件中执行此操作。

    protected void btnOpen_Click(object sender, EventArgs e)
    {
        // Find the index to select
        Button btnOpen = (Button)sender;
        GridViewRow row = (GridViewRow)btnOpen.NamingContainer;
        int selectedIndex = row.DataItemIndex;
    
        // Set the selected index of the GridView
        GridView1.SelectedIndex = selectedIndex;
    
        // Bind the detail GridView now that the row is selected so 
        // that its SqlDataSource can get a SelectedValue for the
        // parent GridView
        gvDetail.DataBind();
    }
    

    【讨论】:

    • 感谢您的回答。我已经尝试过这段代码,但 sql 命令读取为 ... WHERE clientref = @clientref 即它没有选择 @clientref 变量作为 Gridview1 选定的行。我该怎么做?
    • 我很确定它在幕后做到了这一点。我不知道我是否曾经看到过“@”值被替换为查看 select 命令时的值。
    • 确保您在 GridView1 中选择一行 - 就像以某种方式将其标记为 SelectedRow。
    • 我一直在尝试这样做,但我真的不知道如何做到这一点。你知道我缺少什么代码吗?
    • 不,因为除了 WHERE 子句外,我看不到比您提供的代码更多的代码,您说这些代码已经有效。 GridView1 中有SelectedRow 吗?
    猜你喜欢
    • 2012-03-01
    • 2019-06-10
    • 2017-07-13
    • 1970-01-01
    • 1970-01-01
    • 2012-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多