locked
Grid View in Pop Up Paging not working RRS feed

  • Question

  • User-1859712754 posted

    Hey guys,

    so i have a problem that my pop up disappears when i click the next page in my gridview. I tried EnableSortingAndPagingCallbacks="true" but then i got an error with the asp:TemplateField. Thank you for your help in advance. The Code looks like this:

    <div class="modal fade" runat="server" id="Prüfungsinhalt_Modal" data-backdrop="static" data-keyboard="false" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
                        <div class="modal-dialog modal-lg" role="document">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <h2 class="modal-title">Prüfungsinhalte hinzufügen</h2>
                                    <button type="button" runat="server" onserverclick="ModalReset2" onclick="ModalReset2" class="close" data-dismiss="modal">&times;</button>
                                </div>
                                <div class="modal-body">
                                    <!-- Übersicht Tabelle -->
                                    <div class="col-md-10">
                                        <div class="row">
                                            <div class="col-md-10">
                                                <br />
                                                <div class="row">
                                                    <asp:GridView runat="server" ID="PruefungsinhaltsGrid"
                                                        ItemType="BlueThinking.Models.Pruefungsinhalt"
                                                        DataKeyNames="Id"
                                                        EmptyDataText="Keine Prüfungsinhalte vorhanden"
                                                        SelectMethod="PruefungsinhaltsGrid_GetData"
                                                        OnRowDeleting="PruefungsinhaltsGrid_RowDeleting"
                                                        OnPageIndexChanging="PruefungsinhaltsGrid_PageIndexChanging"
                                                        AutoGenerateColumns="false"
                                                        AutoGenerateEditButton="false"
                                                        AutoGenerateDeleteButton="false"
                                                        EnableEventValidation="false"
                                                        CssClass="tdesign"
                                                        PagerStyle-CssClass="pager"
                                                        HeaderStyle-CssClass="header"
                                                        RowStyle-CssClass="rows">
    
                                                        <Columns>
                                                            <asp:BoundField DataField="Id" HeaderText="Id"
                                                                InsertVisible="False" ReadOnly="True" SortExpression="Id"
                                                                ItemStyle-CssClass="hiddencol" HeaderStyle-CssClass="hiddencol" />
                                                            <asp:BoundField DataField="Bezeichnung" HeaderText="Name"
                                                                SortExpression="Name"
                                                                ItemStyle-CssClass="itemStyle" />
                                                            <asp:BoundField DataField="Gueltigkeitsdauer" HeaderText="Gueltigkeitsdauer"
                                                                SortExpression="Gueltigkeitsdauer"
                                                                ItemStyle-CssClass="itemStyle" />
                                                            <asp:TemplateField>
                                                                <ItemTemplate>
                                                                    <asp:CheckBox ID="PrüfungsinhaltAuswahl" runat="server" GroupName="Name" AutoPostBack="false" BackColor="Transparent" />
                                                                </ItemTemplate>
                                                            </asp:TemplateField>
                                                        </Columns>
                                                    </asp:GridView>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="modal-footer">
                                    <asp:Button runat="server" ID="Button3" Text="Hinzufügen" OnClick="Button3_Click" CausesValidation="false" UseSubmitBehavior="false" data-dismiss="modal" />
                                    <asp:Button runat="server" ID="Button4" Text="Auswahl löschen" CausesValidation="false" OnClick="ModalReset2" UseSubmitBehavior="false" data-dismiss="modal" />
                                </div>
                            </div>
                        </div>
                    </div>

    Wednesday, January 16, 2019 11:45 AM

Answers

  • User-1174608757 posted

    Hi JonesNr3,

    According to your description, I have made  a sample here.

    When you click the next page,the Gridview will always rebind the datasource. So , in PageIndexChanging event,  you have to record the state of checkbox before the Gridview databind else the checkbox will be unmarked. I suggest you to record the checked checkbox in session.After Gridview bind, you could set the state of  checkboxs which you have checked before.

    Here is the demo . I hope it could help you.

    aspx:

    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
                <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                    <ContentTemplate>
                        <asp:GridView ID="GridView1" DataKeyNames="id" runat="server" AllowPaging="true" PageSize="3"  OnPageIndexChanging="GridView1_PageIndexChanging">
                            <Columns>
                                <asp:TemplateField>
                                    <ItemTemplate>
                                    <asp:CheckBox ID="CheckBox1" runat="server" />
                                    </ItemTemplate>
                                </asp:TemplateField>
                            </Columns>
                        </asp:GridView>                
                     
                    </ContentTemplate>
                </asp:UpdatePanel>
            </div>
        </form>
    </body>

    aspx.cs:

      public partial class popup : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    string sql = "select * from db";
                    GridView1.DataSource = SqlHelper.ExecuteDataTable(sql);
                    GridView1.DataBind();
    
                }
            }
    
            protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
            {
               
                var selectedIDs = (Session["CheckedIDs"] != null) ?
                Session["CheckedIDs"] as List<int> : new List<int>();
    
                //we are now at current page. set the checked ids to a list
                foreach (GridViewRow row in GridView1.Rows)
                {
    
                    //get the checkbox in the row
                    var emailCheckBox = row.FindControl("PrüfungsinhaltAuswahl") as CheckBox;
                    //gets the primary key of the corresponding row
                    var rowOrgID = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);
                    //is row org_id in the selectedIDs list
                    var isRowIDPresentInList = selectedIDs.Contains(rowOrgID);
                    // add to list
                    if (emailCheckBox.Checked && !isRowIDPresentInList)
                    {
                        selectedIDs.Add(rowOrgID);
                    }
                    //remove from list
                    if (!emailCheckBox.Checked && isRowIDPresentInList)
                    {
                        selectedIDs.Remove(rowOrgID);
                    }
                }
    //bind data GridView1.PageIndex = e.NewPageIndex; string sql = "select * from db"; GridView1.DataSource = SqlHelper.ExecuteDataTable(sql); GridView1.DataBind();
    //foreach all the checkbox and change the state foreach (GridViewRow row in GridView1.Rows) { var emailCheckBox = row.FindControl("PrüfungsinhaltAuswahl") as CheckBox; var rowOrgID = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value); if (selectedIDs.Contains(rowOrgID)) { emailCheckBox.Checked = true; } } Session["CheckedIDs"] = (selectedIDs.Count > 0) ? selectedIDs : null; } } }

    It shows as below,I hope it could help you.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 17, 2019 6:06 AM

All replies

  • User-1859712754 posted

    so I found a way with UpdatePanel and ScriptManager, but the checkboxes will be unmarked after the page change. Anyone an idea for this one ?

    Wednesday, January 16, 2019 4:12 PM
  • User-1174608757 posted

    Hi JonesNr3,

    According to your description, I have made  a sample here.

    When you click the next page,the Gridview will always rebind the datasource. So , in PageIndexChanging event,  you have to record the state of checkbox before the Gridview databind else the checkbox will be unmarked. I suggest you to record the checked checkbox in session.After Gridview bind, you could set the state of  checkboxs which you have checked before.

    Here is the demo . I hope it could help you.

    aspx:

    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
                <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                    <ContentTemplate>
                        <asp:GridView ID="GridView1" DataKeyNames="id" runat="server" AllowPaging="true" PageSize="3"  OnPageIndexChanging="GridView1_PageIndexChanging">
                            <Columns>
                                <asp:TemplateField>
                                    <ItemTemplate>
                                    <asp:CheckBox ID="CheckBox1" runat="server" />
                                    </ItemTemplate>
                                </asp:TemplateField>
                            </Columns>
                        </asp:GridView>                
                     
                    </ContentTemplate>
                </asp:UpdatePanel>
            </div>
        </form>
    </body>

    aspx.cs:

      public partial class popup : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    string sql = "select * from db";
                    GridView1.DataSource = SqlHelper.ExecuteDataTable(sql);
                    GridView1.DataBind();
    
                }
            }
    
            protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
            {
               
                var selectedIDs = (Session["CheckedIDs"] != null) ?
                Session["CheckedIDs"] as List<int> : new List<int>();
    
                //we are now at current page. set the checked ids to a list
                foreach (GridViewRow row in GridView1.Rows)
                {
    
                    //get the checkbox in the row
                    var emailCheckBox = row.FindControl("PrüfungsinhaltAuswahl") as CheckBox;
                    //gets the primary key of the corresponding row
                    var rowOrgID = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);
                    //is row org_id in the selectedIDs list
                    var isRowIDPresentInList = selectedIDs.Contains(rowOrgID);
                    // add to list
                    if (emailCheckBox.Checked && !isRowIDPresentInList)
                    {
                        selectedIDs.Add(rowOrgID);
                    }
                    //remove from list
                    if (!emailCheckBox.Checked && isRowIDPresentInList)
                    {
                        selectedIDs.Remove(rowOrgID);
                    }
                }
    //bind data GridView1.PageIndex = e.NewPageIndex; string sql = "select * from db"; GridView1.DataSource = SqlHelper.ExecuteDataTable(sql); GridView1.DataBind();
    //foreach all the checkbox and change the state foreach (GridViewRow row in GridView1.Rows) { var emailCheckBox = row.FindControl("PrüfungsinhaltAuswahl") as CheckBox; var rowOrgID = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value); if (selectedIDs.Contains(rowOrgID)) { emailCheckBox.Checked = true; } } Session["CheckedIDs"] = (selectedIDs.Count > 0) ? selectedIDs : null; } } }

    It shows as below,I hope it could help you.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 17, 2019 6:06 AM
  • User-1859712754 posted

    Thank you very much ! You saved me !

    Saturday, January 19, 2019 3:01 PM