GridView - SqlDataSource 中的客户端“WHERE”子句?

3

我有一个包含按钮的TemplateFieldGridView。这个按钮会打开一个模态窗口,其中包含另一个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>

GridView2的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">
</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();
        });
    });
1个回答

1
你可以将<asp:ControlParameter>设置为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选定行来获取。我该怎么做呢? - Johnathan
我非常确定它在幕后执行这个操作。当查看选择命令时,我不知道是否曾经看到“@”值被替换为应该是的值。 - j.f.
请确保在GridView1中选择了一行 - 即以某种方式将其标记为SelectedRow。 - j.f.
所以在 btn_Click() 的代码后台中,SelectedRow 总是返回空/ null。而 SelectedIndex 总是返回 -1。 - Johnathan
干得好。看看我的编辑。那应该让你知道如何选择行了。 - j.f.
显示剩余9条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接