3

I have a GridView with a TemplateField containing a button. This button opens up a modal window which contains another GridView as seen below:

Template Field in Gridview1:

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

Modal Window:

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

Now this code works fine and opens up the modal window with the SelectCommand as expected. However, I need to add a where clause based on a row value from GridView1. E.g. ...WHERE td.clientref = GridView1.SelectedRow.Cells[0].Text

Help please!

Edit: Modal Window:

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

Modal JS:

$(document).ready(function () {
        $("#btnOpen").click(function () {
            $("#myModal").modal();
        });
    });
Johnathan
  • 879
  • 3
  • 12
  • 22

1 Answers1

1

You can actually set an <asp:ControlParameter> to the SelectedValue of a GridView. I think this is what you are looking for. As the documentation says:

As a further shortcut, you can directly determine the data key value of the first key field of the selected row by using the SelectedValue property.

So what you can do is set the DataKeyNames value on GridView1 to whatever value it is that you want to use in the WHERE clause.

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

Then set that as the control parameter in your 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>

Just remember you need to make sure the row in GridView1 is actually marked as the SelectedRow. You can do this in your button click event.

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();
}
j.f.
  • 3,908
  • 2
  • 29
  • 42
  • Thanks for the answer. I have tried this code but the sql command is reading as `... WHERE clientref = @clientref` I.e. it is not picking up the `@clientref` variable as the Gridview1 selected row. How would I do this? – Johnathan May 05 '16 at 13:38
  • I'm pretty sure it does that behind the scenes. I don't know if I have ever seen the "@" value replaced with what it should be when viewing the select command. – j.f. May 05 '16 at 13:40
  • Make sure you are selecting a row in GridView1 - as in marking it as the SelectedRow somehow. – j.f. May 05 '16 at 13:41
  • I have been trying to do this but I am really stuck as to how to get this right. Do you know what code I am missing? – Johnathan May 05 '16 at 14:12
  • No, as I cannot see any more code than you provided, which you said already works, except for the WHERE clause. Do you have a [SelectedRow](https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.selectedrow(v=vs.110).aspx) in GridView1? – j.f. May 05 '16 at 14:14
  • So in the `btn_Click()` code behind, the `SelectedRow` always returns blank/null. The SelectedIndex always returns as -1. – Johnathan May 05 '16 at 14:21
  • Good work. See my edit. That should give you an idea on how to select the row. – j.f. May 05 '16 at 14:31
  • Thanks. So your solution works if gvDetail is not inside a modal window. I think modal windows are opened on ClientSide so the code behind doesn't take effect. Frustrating. – Johnathan May 05 '16 at 15:06
  • It's hard for me to tell with just a limited view of your code, but if it works without the modal, I would think it would work with the modal as well. The modal is on the same page, is it not? The client side stuff would just be the javascript hiding and showing the modal, right? – j.f. May 05 '16 at 15:13
  • It's on the same page, yes. I have edited my question to show the Modal code. At the moment, when I click a button, the modal window doesn't open. – Johnathan May 05 '16 at 15:38
  • So what we added actually broke the modal? It was working before, wasn't it? – j.f. May 05 '16 at 16:01
  • Well this looks like the same problem I was having before. It looks as though the modal pops up for a brief moment and then disappears again immediately. – Johnathan May 05 '16 at 16:30
  • Interestingly, if I add a `CommmandField>` to my GridView1 ( ` – Johnathan May 05 '16 at 16:38
  • Yep. sounds like the modal is showing (via the jQuery call), but the post back (via the click event) is wiping out the modal right afterwards. So you have an order of operations problem. Like you discovered, one option would be to do the steps separately. Or, make sure the javascript to show the modal happens after the click event - possibly by calling the javascript from the codebehind or by showing the modal from the codebehind without the use of javascript. – j.f. May 05 '16 at 16:44