使用另一个GridView中的行参数打开GridView

时间:2016-05-03 14:37:24

标签: c# gridview selectcommand

我正在从下面的按钮(btnOpen)打开一个模态窗口。这个bhutton位于GridView内部。它需要在模态窗口中打开另一个Gridview,但我的代码不起作用:

<asp:TemplateField>
    <ItemTemplate>
        <asp:Button ID="btnOpen" runat="server" Text="Show Gridview" CommandName="cmdDetail" CommandArgument="<%# ((GridViewRow) Container).DataItemIndex %>"/>
    </ItemTemplate>
</asp:TemplateField>

我的模态窗口:

<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>

Sql DataSource:

<asp:SqlDataSource ID="SqlgvDetail" runat="server" ConnectionString="<%$ ConnectionStrings:InventoryConnectionString %>">
</asp:SqlDataSource>

代码背后:

protected void gvSummary_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "cmdDetail")
    {
        // Retrieve the row index stored in the CommandArgument property.
        int index = Convert.ToInt32(e.CommandArgument);
        // Retrieve the row that contains the button from the Rows collection.
        GridViewRow row = gvSummary.Rows[index];

        Button btnOpen = row.FindControl("btnOpen") as Button;
        btnOpen.CssClass = "openModal";

        string clientRef = row.Cells[0].Text;

        SqlgvDetail.SelectCommand = " SELECT td.metalid , td.enddate , td.startdate , td.clientref , td.quantity FROM trxdetail td " +
                                    " WHERE td.clientref = '" + clientRef + "'";
        gvDetail.DataBind();
    }
}

当我单击一个按钮时,它会获得正确的SQL命令,但不会加载模态。如果我再次单击该按钮,它会在第一次单击时加载带有SQL命令的模态。

我已经坚持了好几天,所以感谢任何帮助。

0 个答案:

没有答案