我有一个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="#">×</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">×</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>
Modal JS:
$(document).ready(function () {
$("#btnOpen").click(function () {
$("#myModal").modal();
});
});
答案 0 :(得分: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();
}