GridView仅显示第一行的信息

时间:2016-01-20 12:23:30

标签: javascript c# jquery asp.net gridview

我有两个GridView。一个只显示日期和用户,第二个显示一个列表,每个项目旁边都有一个复选框。

我在gridview中将日期设置为查看第二个gridview的链接,以便用户可以查看已勾选的项目。问题是它只显示第一行的信息,即使我选择了第二行。 enter image description here

在图片中看到我点击了顶部GridView中的第二行。但是第二个GridView中显示的信息不是我单击的行。它应该显示ID 395545的信息

我发现问题在于回发:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
           List<lookupCVRT> work = lookupCVRT.GetCVRTItems(Company.Current.CompanyID, ParentID.ToString());


                ViewState["CVRT"] = work;
                gvCVRT.DataSource = work;
                gvCVRT.DataBind();

                if (work.Count > 0)
                {
                    string numberToView = gvCVRT.Rows[0].Cells[0].Text;

                    List<lookupCVRT> workDetails = lookupCVRT.GetChecklistItemsByChecklistID(Company.Current.CompanyID, ParentID.ToString(), numberToView.ToString());
                    gvCVRTDetails.DataSource = workDetails;
                    gvCVRTDetails.DataBind();
                } 
         }
    }

此代码加载第一个GridView的信息,然后根据此GridView中的ID,加载第二个GridView。问题是它只加载第一行的ID。因此,当它绑定第二个GridView时,它只查看第一个ID。

有没有办法强制回发,以便第二个Grid重新加载我选择的行的ID?

这是GridView代码:

<div id="divCVRT">
    <fieldset class="groupbox" >                               
        <div style="width:100%;">
            <asp:UpdatePanel ID="udpCVRT" UpdateMode="Conditional" runat="server">
                <ContentTemplate>
                    <asp:GridView  style="width:75%"  
                        ID="gvCVRT" 
                        ShowHeaderWhenEmpty="true"
                        CssClass="tblResults" 
                        runat="server" 
                        OnRowDataBound="gvCVRT_RowDataBound"                             
                        DataKeyField="ID" 
                        AutoGenerateColumns="false"
                        allowpaging="false"
                        AlternatingRowStyle-BackColor="#EEEEEE">
                        <HeaderStyle CssClass="tblResultsHeader" />
                        <Columns>
                            <asp:BoundField DataField="ChecklistID" HeaderText="ID" ></asp:BoundField> 
                            <asp:BoundField DataField="ChecklistDate" HeaderText="Checklist Date" dataformatstring="{0:dd/MM/yyyy}"></asp:BoundField>
                            <asp:BoundField DataField="User" HeaderText="User" ></asp:BoundField>
                        </Columns>
                    </asp:GridView>
                </ContentTemplate>
            </asp:UpdatePanel>  
            <asp:LinkButton title="Add CVRT" id="btnAddCVRT" OnClick="btnAddCVRT_Click" runat="server" style="cursor:pointer;">
                <img src="../images/icons/buttons/basic1-072.png" />
            </asp:LinkButton>
        </div>
    </fieldset>
</div>
<div id="divCVRTDetails" style="display:none">
    <fieldset class="groupbox" >                               
        <div style="width:100%;">
            <asp:UpdatePanel ID="udpCVRTDetails" UpdateMode="Conditional" runat="server">
                <ContentTemplate>
                    <asp:GridView  style="width:75%"  
                        ID="gvCVRTDetails" 
                        ShowHeaderWhenEmpty="true"
                        CssClass="tblResults" 
                        runat="server" 
                        OnRowDataBound="gvCVRTDetails_RowDataBound"                             
                        DataKeyField="ID" 
                        AutoGenerateColumns="false"
                        allowpaging="false"
                        AlternatingRowStyle-BackColor="#EEEEEE">
                        <HeaderStyle CssClass="tblResultsHeader" />
                        <Columns>
                            <asp:BoundField DataField="ChecklistID" HeaderText="ID" ></asp:BoundField> 
                            <asp:BoundField DataField="Description" HeaderText="Checklist Items"></asp:BoundField> 
                            <asp:TemplateField HeaderText ="Checked?" ItemStyle-HorizontalAlign="Center">
                                <ItemTemplate>
                                         <asp:CheckBox ID="chkChecked" runat="server" ></asp:CheckBox>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>
                </ContentTemplate>
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="btnUpdateCVRT" />
                    <asp:AsyncPostBackTrigger ControlID="btnAddCVRT" />
                </Triggers>
            </asp:UpdatePanel>
            <asp:LinkButton ID="btnUpdateCVRT" OnClientClick="CloseAddEditCVRT()"  OnClick="btnUpdateCVRT_Click" runat="server" style="float:left" CssClass="btnSaveSmall" ></asp:LinkButton>
            <a title="Close CVRT Details" id="btnCloseCVRTDetails" runat="server" class="btnCancelSmall" style="cursor:pointer; margin-left:40px">
            </a>  
        </div>
</fieldset>

儿童gridview背后的代码:

protected void gvCVRTDetails_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            lookupCVRT work = (lookupCVRT)e.Row.DataItem;
            GridView gv = sender as GridView;



            e.Row.Attributes.Add("ID", "gvCVRTDetails_" + work.ID);
            e.Row.Cells[0].Attributes.Add("onclick", "event.stopPropagation();");

            HtmlGenericControl lnkShowHide = (HtmlGenericControl)e.Row.FindControl("lnkShowHide");
            HyperLink ChecklistItem = (HyperLink)e.Row.FindControl("ID");

            CheckBox chkChecked = e.Row.FindControl("chkChecked") as CheckBox;

            chkChecked.Attributes.Add("onclick", "UpdateCheckedBox(" + work.ID.ToString() + ", this.value);");

            chkChecked.Checked = work.Checked;

        }
    }

1 个答案:

答案 0 :(得分:0)

您没有将子GridView绑定到父GridView上的任何选择操作,这就是为什么它不会显示任何内容。您在页面加载上编写的代码将始终与父项的第一项绑定。

要修复此问题,您需要处理父GridView的OnSelectedIndexChanged,然后根据所选ID绑定子GridView

查看更新的代码(我删除了Ajax代码,因此它再次发回页面):

<div id="divCVRT">
    <fieldset class="groupbox" >                               
        <div style="width:100%;">
                <asp:GridView  style="width:75%"  
                        ID="gvCVRT" 
                        ShowHeaderWhenEmpty="true"
                        CssClass="tblResults" 
                        runat="server" 
                        OnRowDataBound="gvCVRT_RowDataBound"                             
                        DataKeyField="ID"
                        DataKeyNames="ID"
                OnSelectedIndexChanged="gridviewParent_SelectedIndexChanged"
                        AutoGenerateColumns="false"
                        allowpaging="false"
                        AlternatingRowStyle-BackColor="#EEEEEE">
                        <HeaderStyle CssClass="tblResultsHeader" />
                        <Columns>
                            <asp:BoundField DataField="ChecklistID" HeaderText="ID" ></asp:BoundField> 
                            <asp:BoundField DataField="ChecklistDate" HeaderText="Checklist Date" dataformatstring="{0:dd/MM/yyyy}"></asp:BoundField>
                            <asp:BoundField DataField="User" HeaderText="User" ></asp:BoundField>
                <asp:CommandField ShowSelectButton="True" />
                        </Columns>
                    </asp:GridView>
            <asp:LinkButton title="Add CVRT" id="btnAddCVRT" OnClick="btnAddCVRT_Click" runat="server" style="cursor:pointer;">
                <img src="../images/icons/buttons/basic1-072.png" />
            </asp:LinkButton>
        </div>
    </fieldset>
</div>
<div id="divCVRTDetails" style="display:none">
    <fieldset class="groupbox" >                               
        <div style="width:100%;">
                <asp:GridView  style="width:75%"  
                        ID="gvCVRTDetails" 
                        ShowHeaderWhenEmpty="true"
                        CssClass="tblResults" 
                        runat="server" 
                        OnRowDataBound="gvCVRTDetails_RowDataBound"                             
                        DataKeyField="ID" 
                        AutoGenerateColumns="false"
                        allowpaging="false"
                        AlternatingRowStyle-BackColor="#EEEEEE">
                        <HeaderStyle CssClass="tblResultsHeader" />
                        <Columns>
                            <asp:BoundField DataField="ChecklistID" HeaderText="ID" ></asp:BoundField> 
                            <asp:BoundField DataField="Description" HeaderText="Checklist Items"></asp:BoundField> 
                            <asp:TemplateField HeaderText ="Checked?" ItemStyle-HorizontalAlign="Center">
                                <ItemTemplate>
                                         <asp:CheckBox ID="chkChecked" runat="server" ></asp:CheckBox>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>
            <asp:LinkButton ID="btnUpdateCVRT" OnClientClick="CloseAddEditCVRT()"  OnClick="btnUpdateCVRT_Click" runat="server" style="float:left" CssClass="btnSaveSmall" ></asp:LinkButton>
            <a title="Close CVRT Details" id="btnCloseCVRTDetails" runat="server" class="btnCancelSmall" style="cursor:pointer; margin-left:40px"></a>  
        </div>
</fieldset>

然后在代码背后:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        List<lookupCVRT> work = lookupCVRT.GetCVRTItems(Company.Current.CompanyID, ParentID.ToString());
        gvCVRT.DataSource = work;
        gvCVRT.DataBind();
     }
}

protected void gridviewParent_SelectedIndexChanged(object sender, EventArgs e)
{
    List<lookupCVRT> workDetails = lookupCVRT.GetChecklistItemsByChecklistID(Company.Current.CompanyID, ParentID.ToString(), gvCVRT.SelectedDataKey.Value.ToString());
    gvCVRTDetails.DataSource = workDetails;
    gvCVRTDetails.DataBind();
}

<强>更新 这是获得想法的最小例子: Department是父表,而categories是子表

<div>
            <asp:GridView runat="server" ID="ParentGridView"
                DataKeyNames="DepartmentId" AutoGenerateColumns="false"
                OnSelectedIndexChanged="ParentGridView_SelectedIndexChanged">
                <Columns>
                    <asp:BoundField DataField="DepartmentId" HeaderText="DepartmentId"></asp:BoundField>
                    <asp:BoundField DataField="DepartmentName" HeaderText="DepartmentName"></asp:BoundField>
                    <asp:BoundField DataField="DepartmentDescription" HeaderText="DepartmentDescription"></asp:BoundField>
                    <asp:CommandField ShowSelectButton="True" />
                </Columns>
            </asp:GridView>

            <br />
            <br />

            <asp:GridView runat="server" ID="ChildGridView" AutoGenerateColumns="false">
                <Columns>
                    <asp:BoundField DataField="CategoryName" HeaderText="CategoryName"></asp:BoundField>
                    <asp:BoundField DataField="CategoryDescription" HeaderText="CategoryDescription"></asp:BoundField>
                </Columns>
            </asp:GridView>
        </div>

背后的代码:

 protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataClasses1DataContext db = new DataClasses1DataContext();
                var data = db.Departments.ToList();

                ParentGridView.DataSource = data;
                ParentGridView.DataBind();
            }
        }

        protected void ParentGridView_SelectedIndexChanged(object sender, EventArgs e)
        {
            int key = int.Parse(ParentGridView.SelectedDataKey.Value.ToString());

            DataClasses1DataContext db = new DataClasses1DataContext();
            var data = db.Categories.Where(p => p.DepartmentId == key);

            ChildGridView.DataSource = data;
            ChildGridView.DataBind();
        }

它可以按你的意愿工作。