jQuery,Find Control和Button Click

时间:2013-11-25 17:00:44

标签: jquery asp.net

我有父子GridView。当我单击父GridVew按钮时,子gridview正在扩展。我的问题是我想使用jQuery来避免在扩展/折叠子GridView时重新加载页面。

代码:

    $(document).ready(function () {
        $('#<%= ((Button)FindControlRecursive(Page, "btnStaffCount")).ClientID %>').click(function () {
            alert("clicked");
        });
    });

public Control FindControlRecursive(Control rootControl, string controlID)
{
    if (rootControl.ID == controlID) return rootControl;

    foreach (Control controlToSearch in rootControl.Controls)
    {
        Control controlToReturn =
            FindControlRecursive(controlToSearch, controlID);
        if (controlToReturn != null) return controlToReturn;
    }
    return null;
}

jQuery仅适用于第一条记录?当所有行都可扩展时,你能否为我提供一个解决方案。

编辑1:

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeFile="StaffLocations_test.aspx.cs" Inherits="StaffLocations_test" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="Server">
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function () {
                $('#<%= ((Button)FindControlRecursive(Page, "lblStaffCount")).ClientID %>').click(function () {
                    $('#<%= ((GridView)FindControlRecursive(Page, "gvoStaffInfo")).ClientID %>').toggle();
                });
            });
    </script>
    <center>
        <asp:Button ID="button" runat="server" Text="button" OnClientClick="return false" />
        <asp:Label runat="server" ID="lblStaff"></asp:Label><br />
        <br />
        <asp:GridView ID="GVOStaff" runat="server" AutoGenerateColumns="false" OnRowDataBound="GVOStaff_RowDataBound"
            OnRowCommand="GVOStaff_RowCommand" DataKeyNames="GVOTeamID,CityID" ShowFooter="True"
            HorizontalAlign="Center" CellPadding="4" ForeColor="#333333" CssClass="gridview"
            AllowPaging="True">
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            <Columns>
                <asp:TemplateField HeaderText="No">
                    <ItemTemplate>
                        <%# Container.DataItemIndex + 1 %>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="GVO Function">
                    <ItemTemplate>
                        <asp:Label ID="lblGVOFunction" runat="server" Text='<%# Bind("gvoFunction") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="GVOD Team ID" Visible="false">
                    <ItemTemplate>
                        <asp:Label ID="lblGVOTeamID" runat="server" Text='<%# Bind("GVOTeamID") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="GVOD Team">
                    <ItemTemplate>
                        <asp:Label ID="lblGVOTeam" runat="server" Text='<%# Bind("GVOTeam") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Staff Count">
                    <ItemTemplate>
                        <asp:Button ID="lblStaffCount" runat="server" Text='<%# Bind("StaffCount") %>' CommandName="ShowDetails"
                            CommandArgument="<%# ((GridViewRow)Container).RowIndex %>" CausesValidation="True"
                            OnClientClick="return false" UseSubmitBehavior="False" Width="80" />
                        <tr>
                            <td runat="server" id="tdStaffdetails" colspan="4">
                                <div id="nestedGridView" class="NotDisplayed">
                                <asp:GridView ID="gvoStaffInfo" AutoGenerateColumns="false" runat="server"
                                    CellPadding="4" CssClass="NotDisplayed" DataKeyNames="SOEID" OnRowCommand="gvoStaffInfo_RowCommand">
                                    <Columns>
                                        <asp:TemplateField HeaderText="No">
                                            <ItemTemplate>
                                                <%# Container.DataItemIndex + 1 %>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText="First Name">
                                            <ItemTemplate>
                                                <asp:Label ID="lblFirstName" runat="server" Text='<%# Bind("FirstName") %>' />
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText="Last Name">
                                            <ItemTemplate>
                                                <asp:Label ID="lblLastName" runat="server" Text='<%# Bind("Lastname") %>' />
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText="SOEID">
                                            <ItemTemplate>
                                                <asp:Label ID="lblSOEID" runat="server" Text='<%# Bind("SOEID") %>' />
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText="GD">
                                            <ItemTemplate>
                                                <asp:LinkButton ID="lblGD" runat="server" Text="GD" CommandName="cmdRedirectGlobalDirectory"
                                                    OnClientClick="aspnetForm.target ='_blank';" CommandArgument="<%# ((GridViewRow)Container).RowIndex %>" />
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText="Edit" Visible="true">
                                            <ItemTemplate>
                                                <asp:LinkButton ID="btnEditStaff" runat="server" Text="Edit" CommandName="cmdEditStaff"
                                                    CommandArgument="<%# ((GridViewRow)Container).RowIndex %>" />
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText="Expertise">
                                            <ItemTemplate>
                                                <asp:ImageButton runat="server" ID="btnExpertises" CommandName="cmdGetExpertises"
                                                    CommandArgument="<%# ((GridViewRow)Container).RowIndex %>" ImageUrl="~/Images/expertise.jpg">
                                                </asp:ImageButton>
                                                <tr>
                                                    <td runat="server" id="tdExpertise" colspan="7">
                                                        <asp:GridView ID="gvUserExpertises" runat="server" CellPadding="4" ForeColor="#333333"
                                                            CssClass="gridview" Visible="false" AutoGenerateColumns="false" BorderStyle="None"
                                                            EmptyDataText="No Expertises found.">
                                                            <Columns>
                                                                <asp:TemplateField HeaderText="No">
                                                                    <ItemTemplate>
                                                                        <%# Container.DataItemIndex + 1 %>
                                                                    </ItemTemplate>
                                                                </asp:TemplateField>
                                                                <asp:TemplateField HeaderText="Expertise">
                                                                    <ItemTemplate>
                                                                        <asp:Label ID="lblExpertise" runat="server" Text='<%# Bind("modelname") %>' />
                                                                    </ItemTemplate>
                                                                </asp:TemplateField>
                                                                <asp:TemplateField HeaderText="Level">
                                                                    <ItemTemplate>
                                                                        <asp:Label ID="lblLevel" runat="server" Text='<%# Bind("expertise") %>' />
                                                                    </ItemTemplate>
                                                                </asp:TemplateField>
                                                            </Columns>
                                                            <AlternatingRowStyle BackColor="White" ForeColor="#284775" CssClass="gridview" />
                                                            <EditRowStyle BackColor="#999999" />
                                                            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                                                            <HeaderStyle BackColor="#367c2b" Font-Bold="True" ForeColor="White" />
                                                            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                                                            <RowStyle BackColor="#f2f7f5" ForeColor="#333333" />
                                                        </asp:GridView>
                                                    </td>
                                                </tr>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                    </Columns>
                                    <AlternatingRowStyle BackColor="White" ForeColor="#284775" CssClass="gridview" />
                                    <EditRowStyle BackColor="#999999" />
                                    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                                    <HeaderStyle BackColor="#5d9b9d" Font-Bold="True" ForeColor="White" />
                                    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                                    <RowStyle BackColor="#f2f7f5" ForeColor="#333333" />
                                </asp:GridView>
                                </div>
                            </td>
                        </tr>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:Button ID="btnTotalStaffCount" runat="server" Text="Show All Staff from City"
                            CommandName="ShowAll" CausesValidation="True" UseSubmitBehavior="False" OnClientClick="aspnetForm.target ='_self';" />
                        <tr>
                            <td runat="server" id="tdStaffdetailsAll" colspan="4">
                                <asp:GridView ID="gvoStaffInfoAll" AutoGenerateColumns="false" runat="server" CellPadding="4"
                                    CssClass="gridview" Visible="false" DataKeyNames="SOEID" OnRowCommand="gvoStaffInfoAll_RowCommand">
                                    <Columns>
                                        <asp:TemplateField HeaderText="No">
                                            <ItemTemplate>
                                                <%# Container.DataItemIndex + 1 %>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText="First Name">
                                            <ItemTemplate>
                                                <asp:Label ID="lblFirstName" runat="server" Text='<%# Bind("FirstName") %>' />
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText="Last Name">
                                            <ItemTemplate>
                                                <asp:Label ID="lblLastName" runat="server" Text='<%# Bind("Lastname") %>' />
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText="SOEID">
                                            <ItemTemplate>
                                                <asp:Label ID="lblSOEID" runat="server" Text='<%# Bind("SOEID") %>' />
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText="GD">
                                            <ItemTemplate>
                                                <asp:LinkButton ID="lblGD" runat="server" Text="GD" CommandName="cmdRedirectGlobalDirectoryAll"
                                                    OnClientClick="aspnetForm.target ='_blank';" CommandArgument="<%# ((GridViewRow)Container).RowIndex %>" />
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText="Edit" Visible="true">
                                            <ItemTemplate>
                                                <asp:LinkButton ID="btnEditStaffAll" runat="server" Text="Edit" CommandName="cmdEditStaffAll"
                                                    CommandArgument="<%# ((GridViewRow)Container).RowIndex %>" />
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText="Expertise">
                                            <ItemTemplate>
                                                <asp:ImageButton runat="server" ID="btnExpertisesAll" CommandName="cmdGetExpertisesAll"
                                                    CommandArgument="<%# ((GridViewRow)Container).RowIndex %>" ImageUrl="~/Images/expertise.jpg">
                                                </asp:ImageButton>
                                                <tr>
                                                    <td runat="server" id="tdExpertiseAll" colspan="7">
                                                        <asp:GridView ID="gvUserExpertisesAll" runat="server" CellPadding="4" ForeColor="#333333"
                                                            CssClass="gridview" Visible="false" AutoGenerateColumns="false" BorderStyle="None"
                                                            EmptyDataText="No Expertises found.">
                                                            <Columns>
                                                                <asp:TemplateField HeaderText="No">
                                                                    <ItemTemplate>
                                                                        <%# Container.DataItemIndex + 1 %>
                                                                    </ItemTemplate>
                                                                </asp:TemplateField>
                                                                <asp:TemplateField HeaderText="Expertise">
                                                                    <ItemTemplate>
                                                                        <asp:Label ID="lblExpertise" runat="server" Text='<%# Bind("modelname") %>' />
                                                                    </ItemTemplate>
                                                                </asp:TemplateField>
                                                                <asp:TemplateField HeaderText="Level">
                                                                    <ItemTemplate>
                                                                        <asp:Label ID="lblLevel" runat="server" Text='<%# Bind("expertise") %>' />
                                                                    </ItemTemplate>
                                                                </asp:TemplateField>
                                                            </Columns>
                                                            <AlternatingRowStyle BackColor="White" ForeColor="#284775" CssClass="gridview" />
                                                            <EditRowStyle BackColor="#999999" />
                                                            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                                                            <HeaderStyle BackColor="#367c2b" Font-Bold="True" ForeColor="White" />
                                                            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                                                            <RowStyle BackColor="#f2f7f5" ForeColor="#333333" />
                                                        </asp:GridView>
                                                    </td>
                                                </tr>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                    </Columns>
                                    <AlternatingRowStyle BackColor="White" ForeColor="#284775" CssClass="gridview" />
                                    <EditRowStyle BackColor="#999999" />
                                    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                                    <HeaderStyle BackColor="#5d9b9d" Font-Bold="True" ForeColor="White" />
                                    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                                    <RowStyle BackColor="#f2f7f5" ForeColor="#333333" />
                                </asp:GridView>
                            </td>
                        </tr>
                    </FooterTemplate>
                </asp:TemplateField>
            </Columns>
            <EditRowStyle BackColor="#999999" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#E9E7E2" />
            <SortedAscendingHeaderStyle BackColor="#506C8C" />
            <SortedDescendingCellStyle BackColor="#FFFDF8" />
            <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
        </asp:GridView>
        <br />
        <br />
    </center>
</asp:Content>


public partial class StaffLocations : System.Web.UI.Page
{
    Methods methods = new Methods();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            int cityid = 15661;
            int? GVOFunctionid = string.IsNullOrEmpty((Request["GVOFunctionid"])) ? (int?)null : (int?)Convert.ToInt32((Request["GVOFunctionid"]));
            int? GVOTeamid = string.IsNullOrEmpty(Request["GVOTeamid"]) ? (int?)null : (int?)Convert.ToInt32(Convert.ToInt16(Request["GVOTeamid"]));
            methods.GetStaffLocations(cityid, GVOFunctionid, GVOTeamid, GVOStaff);
            lblStaff.Text = "Staff Details: GVO Function - " + methods.GetGVOFunctionFromID(GVOFunctionid) + ", GVO Team - " + methods.GetGVOTeamFromID(GVOTeamid) + ", city " + methods.GetCityFromID(cityid);
        }
    }

    public Control FindControlRecursive(Control rootControl, string controlID)
    {
        if (rootControl.ID == controlID) return rootControl;

        foreach (Control controlToSearch in rootControl.Controls)
        {
            Control controlToReturn =
                FindControlRecursive(controlToSearch, controlID);
            if (controlToReturn != null) return controlToReturn;
        }
        return null;
    }

    protected void GVOStaff_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int cityid = 15661;
                Label lblGVOTeamID = ((Label)e.Row.FindControl("lblGVOTeamID"));
                int gvoTeamID = Convert.ToInt32(lblGVOTeamID.Text);
                GridView gvGVOStaffInfo = (GridView)e.Row.FindControl("gvoStaffInfo"); // find child gridview
                gvGVOStaffInfo.DataSource = GetStaff(cityid, gvoTeamID).Tables[1];
                gvGVOStaffInfo.DataBind();

            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }


    protected void GVOStaff_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "ShowAll")
            {
                GridView gvoAllStaffInfo = GVOStaff.FooterRow.FindControl("gvoStaffInfoAll") as GridView;
                int gvoTeamID = 0;
                int cityID = Convert.ToInt16(GVOStaff.DataKeys[0].Values[1].ToString());
                gvoAllStaffInfo.DataSource = GetStaff(cityID, gvoTeamID).Tables[1];
                gvoAllStaffInfo.DataBind();
                if (gvoAllStaffInfo.Visible == false)
                    gvoAllStaffInfo.Visible = true;
                else
                    gvoAllStaffInfo.Visible = false;
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }

    private static DataSet GetStaff(int cityid, int GVOTeamid)
    {
        ConnectDatabase connectDB = new ConnectDatabase();
        IDBManager dbManager = connectDB.ConnectDB();

        dbManager.Open();
        dbManager.CreateParameters(2);
        dbManager.AddParameters(0, "@gvoTeamid", GVOTeamid);
        dbManager.AddParameters(1, "@cityid", cityid);
        var ds = dbManager.ExecuteDataSet(CommandType.StoredProcedure, "sp_select_staff");
        return ds;
    }

    protected void gvoStaffInfo_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            int index = Convert.ToInt32(e.CommandArgument);
            GridView gvoStaffInfo = sender as GridView;
            GridViewRow row = gvoStaffInfo.NamingContainer as GridViewRow;

            //Checking for command name which command/button is pressed
            if (e.CommandName == "cmdRedirectGlobalDirectory")
            {

                string soeid = gvoStaffInfo.DataKeys[index].Values[0].ToString();
                Response.Redirect("page" + soeid);
            }
            else if (e.CommandName == "cmdGetExpertises")
            {
                string soeid = gvoStaffInfo.DataKeys[index].Values[0].ToString();
                GridView gvUserExpertises = (GridView)gvoStaffInfo.Rows[index].FindControl("gvUserExpertises");
                DataTable dt = new DataTable();
                dt = GetUserExpertises(soeid).Tables[0];
                gvUserExpertises.DataSource = dt;
                gvUserExpertises.DataBind();

                if (gvUserExpertises.Visible == true)
                    gvUserExpertises.Visible = false;
                else
                    gvUserExpertises.Visible = true;
            }
            else if (e.CommandName == "cmdEditStaff")
            {
                string SOEID = gvoStaffInfo.DataKeys[index].Values[0].ToString();
                Response.Redirect("~/EditStaff.aspx?SOEID=" + SOEID);
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }

    protected void gvoStaffInfoAll_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            int index = Convert.ToInt32(e.CommandArgument);
            GridView gvoStaffInfo = sender as GridView;
            GridViewRow row = gvoStaffInfo.NamingContainer as GridViewRow;

            //Checking for command name which command/button is pressed
            if (e.CommandName == "cmdRedirectGlobalDirectoryAll")
            {
                string soeid = gvoStaffInfo.DataKeys[index].Values[0].ToString();
                Response.Redirect("page" + soeid);
            }
            else if (e.CommandName == "cmdGetExpertisesAll")
            {
                string soeid = gvoStaffInfo.DataKeys[index].Values[0].ToString();
                GridView gvUserExpertises = (GridView)gvoStaffInfo.Rows[index].FindControl("gvUserExpertisesAll");
                DataTable dt = new DataTable();
                dt = GetUserExpertises(soeid).Tables[0];
                gvUserExpertises.DataSource = dt;
                gvUserExpertises.DataBind();

                if (gvUserExpertises.Visible == true)
                    gvUserExpertises.Visible = false;
                else
                    gvUserExpertises.Visible = true;
            }
            else if (e.CommandName == "cmdEditStaffAll")
            {
                string SOEID = gvoStaffInfo.DataKeys[index].Values[0].ToString();
                Response.Redirect("~/EditStaff.aspx?SOEID=" + SOEID);
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }

    private static DataSet GetUserExpertises(string soeid)
    {
        // create ConnectDatabase object to get acces to its methods
        ConnectDatabase connectDB = new ConnectDatabase();
        IDBManager dbManager = connectDB.ConnectDB();
        DataSet ds = new DataSet();

        try
        {
            dbManager.Open();
            dbManager.CreateParameters(1);
            dbManager.AddParameters(0, "@soeid", soeid);
            ds = dbManager.ExecuteDataSet(CommandType.StoredProcedure, "sp_select_user_expertises");
        }
        catch (Exception error)
        {
            HttpContext.Current.Response.Write(error.ToString());
        }
        finally
        {
            dbManager.Close();
            dbManager.Dispose();
        }
        return ds;
    }

    protected void gvStaffDetails_RowCommand(object sender, GridViewCommandEventArgs e)
    {
    }

}

0 个答案:

没有答案