所以我有一个GridView(gvSummary
),其中包含一个带有按钮的模板字段(btnOpen
)。此按钮的目的是根据gvDetail
中的行信息在模态窗口中打开第二个GridView(gvSummary
)。
我遇到的问题是,我似乎无法通过点击按钮gvSummary_RowCommand
来执行btnOpen
中的代码。我可以执行<asp:ButtonField>
中的代码,但这不会打开第二个GridView。
这是我的代码:
<asp:GridView ID="gvSummary" runat="server" DataSourceID="SqlgvSummary" AutoGenerateColumns="false"
OnRowDataBound="gvSummary_RowDataBound" OnRowCommand="gvSummary_RowCommand" CssClass="table table-hover table-bordered" EmptyDataText="No data to display." >
<Columns>
<asp:BoundField DataField="ClientRef" HeaderText="Reference No."/>
<asp:BoundField DataField="InsertedWhen" HeaderText="Transaction Date" DataFormatString="{0:dd-MM-yyyy}" />
<asp:BoundField DataField="Commodity" HeaderText="Commodity" />
<asp:BoundField DataField="StartDate" HeaderText="Settlement Date" DataFormatString="{0:dd-MM-yyyy}" />
<asp:BoundField DataField="EndDate" HeaderText="Delivery Date" DataFormatString="{0:dd-MM-yyyy}" />
<asp:BoundField DataField="Value" HeaderText="Transaction Value" DataFormatString="{0:N2}" />
<asp:BoundField DataField="Fee" HeaderText="Fee" DataFormatString="{0:N2}" />
<asp:BoundField DataField="InsertedBy" HeaderText="Purchased By" />
<asp:ButtonField ButtonType="Button" CommandName="cmdDetail1" HeaderText="View Details" Text="View" ControlStyle-CssClass="openModal"/>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<div>
<asp:Button ID="btnOpen" runat="server" Text="Show Gridview" CssClass="openModal" CommandName="cmdDetail2"/>
<div class="modal" id="idModal">
<div class="container">
<div class="modal-header">
<h1>Transaction Details<a class="close-modal" href="#">×</a></h1>
</div>
<div class="modal-body">
<asp:GridView ID="gvDetail" runat="server" DataSourceID="SqlgvDetail" AutoGenerateColumns="false"
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>
</div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
代码背后:
protected void gvSummary_RowCommand(object sender, GridViewCommandEventArgs e)
{
int index = Convert.ToInt32(e.CommandArgument);
string name = e.CommandName.ToString();
string clientRef = gvSummary.Rows[index].Cells[0].Text;
Response.Write("<br> index = " + index);
Response.Write("<br> name = " + name);
Response.Write("<br> clientRef = " + clientRef);
SqlgvDetail.SelectCommand = " SELECT td.metalid , td.enddate , td.startdate , td.clientref , td.quantity " +
" FROM trxdetail td " +
" WHERE td.clientref = " + "'" + clientRef + "'";
}
当我单击<asp:ButtonField>
时,SQL是正确的,Response.Write()在gvSummary_RowCommand
中设置正确的变量。但是,模态窗口不会打开。
当我点击btnOpen
时,模态窗口会打开,但不会在gvSummary_RowCommand
中设置变量。
我似乎无法做到这一点,所以任何帮助都会受到赞赏。 感谢。