locked
DropDownList and pagination conflict in gridview in .NET C# RRS feed

  • Question

  • User1036972001 posted

    hi guys,

    this is my gridview in .NET c# with pagination

        <asp:GridView ID="gvProducts" AutoGenerateColumns="False" EmptyDataText="ko" EnableViewState="true"
            runat="server" DataKeyNames="ID" CssClass="mGrid" HorizontalAlign="Center"
            OnRowDataBound="gvProducts_RowDataBound"
            AllowPaging="True" PageSize="10"
            OnPageIndexChanging="OnPageIndexChanging">
            <AlternatingRowStyle CssClass="altrows" />
            <Columns>
              <asp:BoundField DataField="Nr" HeaderText="Nr." ReadOnly="true" HtmlEncode="false"
                            ItemStyle-CssClass="ddl_Class_new" ItemStyle-HorizontalAlign="Center" />
    <asp:TemplateField HeaderText="Status" ItemStyle-HorizontalAlign="Center"> <ItemTemplate> <asp:DropDownList ID="ddlstatus" runat="server" CssClass="pure-u-23-24" AutoPostBack="true" OnSelectedIndexChanged="ddlstatus_SelectedIndexChanged"> </asp:DropDownList> </ItemTemplate> </asp:TemplateField> </Columns> <PagerTemplate> <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="/aspnet/img/bot_back_1.gif" CommandArgument="First" CommandName="Page" /> <asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="/aspnet/img/bot_back.gif" CommandArgument="Prev" CommandName="Page" /> Page <asp:DropDownList ID="ddlPages" runat="server" AutoPostBack="True" CssClass="ddl_Class" OnSelectedIndexChanged="ddlPages_SelectedIndexChanged"> </asp:DropDownList> of <asp:Label ID="lblPageCount" runat="server"></asp:Label> <asp:ImageButton ID="ImageButton3" runat="server" ImageUrl="/aspnet/img/bot_next.gif" CommandArgument="Next" CommandName="Page" /> <asp:ImageButton ID="ImageButton4" runat="server" ImageUrl="/aspnet/img/bot_next_1.gif" CommandArgument="Last" CommandName="Page" /> </PagerTemplate> </asp:GridView>

    on this gridview i have added this dropdownlist ddlstatus

       <asp:TemplateField HeaderText="Status" ItemStyle-HorizontalAlign="Center">
            <ItemTemplate>
                <asp:DropDownList ID="ddlstatus" runat="server" CssClass="pure-u-23-24"
                    AutoPostBack="true" OnSelectedIndexChanged="ddlstatus_SelectedIndexChanged">
                </asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>

    when the dropdownlist ddlstatus is changed i need open new webpage on the browser

    protected void ddlstatus_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ebb = (DropDownList)sender;
        GridViewRow gvr = (GridViewRow)ebb.NamingContainer;
        string dvalue = gvr.Cells[1].Text;
        ClientScript.RegisterStartupScript(this.GetType(), "OpenWindow", "window.open('newpage.aspx?ID=" + dvalue.ToString() + "');", true);
    }

    but the new webpage on the browser is opened also if try change page on gridview

    protected void ddlPages_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow gvrPager = gvProducts.BottomPagerRow;
        DropDownList ddlPages = (DropDownList)gvrPager.Cells[0].FindControl("ddlPages");
        gvProducts.PageIndex = ddlPages.SelectedIndex;
        BindData();
    }
    
    protected void Paginate(object sender, CommandEventArgs e)
    {
        int intCurIndex = gvProducts.PageIndex;
    
        switch (e.CommandArgument.ToString().ToLower())
        {
            case "First":
                gvProducts.PageIndex = 0;
                break;
            case "Prev":
                gvProducts.PageIndex = intCurIndex - 1;
                break;
            case "Next":
                gvProducts.PageIndex = intCurIndex + 1;
                break;
            case "Last":
                gvProducts.PageIndex = gvProducts.PageCount - 1;
                break;
        }
        gvProducts.DataBind();
    }
    
    protected void gvProducts_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvProducts.PageIndex = e.NewPageIndex;
        BindData();
    }
    
    protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvProducts.PageIndex = e.NewPageIndex;
        BindData();
    }

    this is the the RowDataBound event

    protected void gvProducts_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Pager)
        {
            DropDownList ddl = (DropDownList)(e.Row.FindControl("ddlpages"));
            Label lblPageCount = (Label)e.Row.FindControl("lblPageCount");
    
            if (lblPageCount != null)
                lblPageCount.Text = gvProducts.PageCount.ToString();
    
            for (int i = 1; i <= gvProducts.PageCount; i++)
            {
                ddl.Items.Add(i.ToString());
            }
    
            ddl.SelectedIndex = gvProducts.PageIndex;
    
            if (gvProducts.PageIndex == 0)
            {
                ((ImageButton)e.Row.FindControl("ImageButton1")).Visible = false;
                ((ImageButton)e.Row.FindControl("ImageButton2")).Visible = false;
            }
    
            if (gvProducts.PageIndex + 1 == gvProducts.PageCount)
            {
                ((ImageButton)e.Row.FindControl("ImageButton3")).Visible = false;
                ((ImageButton)e.Row.FindControl("ImageButton4")).Visible = false;
            }
        }
    }

    i don't understand why this happens?

    how to do resolve this?

    thanks in advance for any help

    Wednesday, September 16, 2020 11:56 AM

Answers

  • User-1330468790 posted

    Hi Koopers,

     

    I tested your codes and found that there is a missing method for binding the data for the grid view and drop down list.

    I made them by myself and ran the codes. No such error occurs. I suspected that the problem was located in those methods.

      

    Could you please provide those codes for reproduce the problem?

     

    My codes for your reference:

    Gridview aspx:

    <asp:GridView ID="gvProducts" AutoGenerateColumns="False" EmptyDataText="ko" EnableViewState="true"
                    runat="server" DataKeyNames="ID" CssClass="mGrid" HorizontalAlign="Center"
                    OnRowDataBound="gvProducts_RowDataBound"
                    AllowPaging="True" PageSize="10"
                    OnPageIndexChanging="OnPageIndexChanging">
                    <AlternatingRowStyle CssClass="altrows" />
                    <Columns>
                        <asp:BoundField DataField="Nr" HeaderText="Nr." ReadOnly="true" HtmlEncode="false"
                            ItemStyle-CssClass="ddl_Class_new" ItemStyle-HorizontalAlign="Center" />
    
                        <asp:TemplateField HeaderText="Status" ItemStyle-HorizontalAlign="Center">
                            <ItemTemplate>
                                <asp:DropDownList ID="ddlstatus" runat="server" CssClass="pure-u-23-24"
                                    AutoPostBack="true" OnSelectedIndexChanged="ddlstatus_SelectedIndexChanged">
                                </asp:DropDownList>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                    <PagerTemplate>
                        <asp:ImageButton ID="ImageButton1" runat="server" AlternateText="First" ImageUrl="/aspnet/img/bot_back_1.gif"
                            CommandArgument="First" CommandName="Page" />
                        <asp:ImageButton ID="ImageButton2" runat="server" AlternateText="Prev" ImageUrl="/aspnet/img/bot_back.gif"
                            CommandArgument="Prev" CommandName="Page" />
                        Page
                    <asp:DropDownList ID="ddlPages" runat="server" AutoPostBack="True" CssClass="ddl_Class"
                        OnSelectedIndexChanged="ddlPages_SelectedIndexChanged">
                    </asp:DropDownList>
                        of
                    <asp:Label ID="lblPageCount" runat="server"></asp:Label>
                        <asp:ImageButton ID="ImageButton3" runat="server" AlternateText="Next" ImageUrl="/aspnet/img/bot_next.gif"
                            CommandArgument="Next" CommandName="Page" />
                        <asp:ImageButton ID="ImageButton4" runat="server" AlternateText="Last" ImageUrl="/aspnet/img/bot_next_1.gif"
                            CommandArgument="Last" CommandName="Page" />
                    </PagerTemplate>
                </asp:GridView>

    Grid View Code behind:

     // Simulation of the data
            private static DataTable _gridviewDT;
            public static DataTable GridviewDT
            {
                get
                {
                    if (_gridviewDT is null)
                    {
                        _gridviewDT = new DataTable();
    
                        _gridviewDT.Columns.Add("ID", typeof(int));
                        _gridviewDT.Columns.Add("Nr", typeof(int));
    
    
                        for(int i = 1; i< 50; i++)
                        {
                            _gridviewDT.Rows.Add(i,i);
                        }
                        
    
                    }
    
                    return _gridviewDT;
                }
                set
                {
                    _gridviewDT = value;
                }
            }
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    BindData();
                }
            }
    
            protected void BindData()
            {
                gvProducts.DataSource = GridviewDT;
                gvProducts.DataBind();
            }
    
            protected void ddlPages_SelectedIndexChanged(object sender, EventArgs e)
            {
                GridViewRow gvrPager = gvProducts.BottomPagerRow;
                DropDownList ddlPages = (DropDownList)gvrPager.Cells[0].FindControl("ddlPages");
                gvProducts.PageIndex = ddlPages.SelectedIndex;
                BindData();
            }
    
            protected void ddlstatus_SelectedIndexChanged(object sender, EventArgs e)
            {
                DropDownList ebb = (DropDownList)sender;
                GridViewRow gvr = (GridViewRow)ebb.NamingContainer;
                string dvalue = gvr.Cells[0].Text;
                ClientScript.RegisterStartupScript(this.GetType(), "OpenWindow", "window.open('newpage.aspx?ID=" + dvalue.ToString() + "');", true);
            }
    
            protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
            {
                gvProducts.PageIndex = e.NewPageIndex;
                BindData();
            }
    
            protected void gvProducts_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.Pager)
                {
                    DropDownList ddl = (DropDownList)(e.Row.FindControl("ddlpages"));
                    Label lblPageCount = (Label)e.Row.FindControl("lblPageCount");
    
                    if (lblPageCount != null)
                        lblPageCount.Text = gvProducts.PageCount.ToString();
    
                    for (int i = 1; i <= gvProducts.PageCount; i++)
                    {
                        ddl.Items.Add(i.ToString());
                    }
    
                    ddl.SelectedIndex = gvProducts.PageIndex;
    
                    if (gvProducts.PageIndex == 0)
                    {
                        ((ImageButton)e.Row.FindControl("ImageButton1")).Visible = false;
                        ((ImageButton)e.Row.FindControl("ImageButton2")).Visible = false;
                    }
    
                    if (gvProducts.PageIndex + 1 == gvProducts.PageCount)
                    {
                        ((ImageButton)e.Row.FindControl("ImageButton3")).Visible = false;
                        ((ImageButton)e.Row.FindControl("ImageButton4")).Visible = false;
                    }
                }
    
                if(e.Row.RowType == DataControlRowType.DataRow)
                {
                    DropDownList ddlPages = (DropDownList)e.Row.FindControl("ddlstatus");
                    for(int i = 0; i < 4; i++)
                    {
                        ddlPages.Items.Add(i.ToString());
                    }
                    
    
                }
            }

    New Page aspx:

    <form id="form1" runat="server">
            <div>
                <h1>The ID is <asp:Label ID="DisplayID" runat="server"></asp:Label></h1>
            </div>
        </form>

    New Page Code behind:

    protected void Page_Load(object sender, EventArgs e)
            {
                if(Request.QueryString["ID"] != null)
                {
                    DisplayID.Text = Request.QueryString["ID"];
                }
            }

    Demo:

     

    Best regards,

    Sean

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 17, 2020 7:18 AM

All replies

  • User-1330468790 posted

    Hi Koopers,

     

    I tested your codes and found that there is a missing method for binding the data for the grid view and drop down list.

    I made them by myself and ran the codes. No such error occurs. I suspected that the problem was located in those methods.

      

    Could you please provide those codes for reproduce the problem?

     

    My codes for your reference:

    Gridview aspx:

    <asp:GridView ID="gvProducts" AutoGenerateColumns="False" EmptyDataText="ko" EnableViewState="true"
                    runat="server" DataKeyNames="ID" CssClass="mGrid" HorizontalAlign="Center"
                    OnRowDataBound="gvProducts_RowDataBound"
                    AllowPaging="True" PageSize="10"
                    OnPageIndexChanging="OnPageIndexChanging">
                    <AlternatingRowStyle CssClass="altrows" />
                    <Columns>
                        <asp:BoundField DataField="Nr" HeaderText="Nr." ReadOnly="true" HtmlEncode="false"
                            ItemStyle-CssClass="ddl_Class_new" ItemStyle-HorizontalAlign="Center" />
    
                        <asp:TemplateField HeaderText="Status" ItemStyle-HorizontalAlign="Center">
                            <ItemTemplate>
                                <asp:DropDownList ID="ddlstatus" runat="server" CssClass="pure-u-23-24"
                                    AutoPostBack="true" OnSelectedIndexChanged="ddlstatus_SelectedIndexChanged">
                                </asp:DropDownList>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                    <PagerTemplate>
                        <asp:ImageButton ID="ImageButton1" runat="server" AlternateText="First" ImageUrl="/aspnet/img/bot_back_1.gif"
                            CommandArgument="First" CommandName="Page" />
                        <asp:ImageButton ID="ImageButton2" runat="server" AlternateText="Prev" ImageUrl="/aspnet/img/bot_back.gif"
                            CommandArgument="Prev" CommandName="Page" />
                        Page
                    <asp:DropDownList ID="ddlPages" runat="server" AutoPostBack="True" CssClass="ddl_Class"
                        OnSelectedIndexChanged="ddlPages_SelectedIndexChanged">
                    </asp:DropDownList>
                        of
                    <asp:Label ID="lblPageCount" runat="server"></asp:Label>
                        <asp:ImageButton ID="ImageButton3" runat="server" AlternateText="Next" ImageUrl="/aspnet/img/bot_next.gif"
                            CommandArgument="Next" CommandName="Page" />
                        <asp:ImageButton ID="ImageButton4" runat="server" AlternateText="Last" ImageUrl="/aspnet/img/bot_next_1.gif"
                            CommandArgument="Last" CommandName="Page" />
                    </PagerTemplate>
                </asp:GridView>

    Grid View Code behind:

     // Simulation of the data
            private static DataTable _gridviewDT;
            public static DataTable GridviewDT
            {
                get
                {
                    if (_gridviewDT is null)
                    {
                        _gridviewDT = new DataTable();
    
                        _gridviewDT.Columns.Add("ID", typeof(int));
                        _gridviewDT.Columns.Add("Nr", typeof(int));
    
    
                        for(int i = 1; i< 50; i++)
                        {
                            _gridviewDT.Rows.Add(i,i);
                        }
                        
    
                    }
    
                    return _gridviewDT;
                }
                set
                {
                    _gridviewDT = value;
                }
            }
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    BindData();
                }
            }
    
            protected void BindData()
            {
                gvProducts.DataSource = GridviewDT;
                gvProducts.DataBind();
            }
    
            protected void ddlPages_SelectedIndexChanged(object sender, EventArgs e)
            {
                GridViewRow gvrPager = gvProducts.BottomPagerRow;
                DropDownList ddlPages = (DropDownList)gvrPager.Cells[0].FindControl("ddlPages");
                gvProducts.PageIndex = ddlPages.SelectedIndex;
                BindData();
            }
    
            protected void ddlstatus_SelectedIndexChanged(object sender, EventArgs e)
            {
                DropDownList ebb = (DropDownList)sender;
                GridViewRow gvr = (GridViewRow)ebb.NamingContainer;
                string dvalue = gvr.Cells[0].Text;
                ClientScript.RegisterStartupScript(this.GetType(), "OpenWindow", "window.open('newpage.aspx?ID=" + dvalue.ToString() + "');", true);
            }
    
            protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
            {
                gvProducts.PageIndex = e.NewPageIndex;
                BindData();
            }
    
            protected void gvProducts_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.Pager)
                {
                    DropDownList ddl = (DropDownList)(e.Row.FindControl("ddlpages"));
                    Label lblPageCount = (Label)e.Row.FindControl("lblPageCount");
    
                    if (lblPageCount != null)
                        lblPageCount.Text = gvProducts.PageCount.ToString();
    
                    for (int i = 1; i <= gvProducts.PageCount; i++)
                    {
                        ddl.Items.Add(i.ToString());
                    }
    
                    ddl.SelectedIndex = gvProducts.PageIndex;
    
                    if (gvProducts.PageIndex == 0)
                    {
                        ((ImageButton)e.Row.FindControl("ImageButton1")).Visible = false;
                        ((ImageButton)e.Row.FindControl("ImageButton2")).Visible = false;
                    }
    
                    if (gvProducts.PageIndex + 1 == gvProducts.PageCount)
                    {
                        ((ImageButton)e.Row.FindControl("ImageButton3")).Visible = false;
                        ((ImageButton)e.Row.FindControl("ImageButton4")).Visible = false;
                    }
                }
    
                if(e.Row.RowType == DataControlRowType.DataRow)
                {
                    DropDownList ddlPages = (DropDownList)e.Row.FindControl("ddlstatus");
                    for(int i = 0; i < 4; i++)
                    {
                        ddlPages.Items.Add(i.ToString());
                    }
                    
    
                }
            }

    New Page aspx:

    <form id="form1" runat="server">
            <div>
                <h1>The ID is <asp:Label ID="DisplayID" runat="server"></asp:Label></h1>
            </div>
        </form>

    New Page Code behind:

    protected void Page_Load(object sender, EventArgs e)
            {
                if(Request.QueryString["ID"] != null)
                {
                    DisplayID.Text = Request.QueryString["ID"];
                }
            }

    Demo:

     

    Best regards,

    Sean

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 17, 2020 7:18 AM
  • User1036972001 posted

    Hi Sean, thank you so much for help

    the problem could be how the ddlstatus dropdownlist is populated on RowDataBound ?

    please, see below

        private void mtddlstatus(GridViewRowEventArgs e)
        {
            int key = Convert.ToInt32(gvProducts.DataKeys[e.Row.RowIndex].Value.ToString());
            DropDownList ddlstatus = (DropDownList)(e.Row.FindControl("ddlstatus"));
    
            sql = @String.Format(" SELECT ");
            sql += String.Format(" CASE WHEN status = 0 THEN 'Open' ");
            sql += String.Format(" ELSE 'Close' END AS status ");
            sql += String.Format(" FROM `tbl_status` ");
            sql += String.Format(" WHERE ID = @paramID; ");
    
            using (MySqlConnection myConnectionString =
              new MySqlConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString))
            {
                using (MySqlCommand cmd =
                    new MySqlCommand(sql, myConnectionString))
                {
                    cmd.Parameters.AddWithValue("@paramID", key.ToString());
    
                    MySqlDataAdapter adapter =
                        new MySqlDataAdapter(cmd);
    
                    DataTable dt = new DataTable();
                    adapter.Fill(dt);
                    ddlstatus.DataSource = dt;
    
                    ddlstatus.DataTextField = "status";
                    ddlstatus.DataValueField = "status";
                    ddlstatus.DataBind();
                    string selectedStatus = DataBinder.Eval(e.Row.DataItem, "status").ToString();
                    ddlstatus.Items.FindByValue(selectedStatus).Selected = true;
                    ddlstatus.Items.Insert(0, new ListItem("=== === ===", "0"));
                    ddlstatus.Items.Insert(0, new ListItem("Open/Forward", "0"));
                }
            }
        }
    
        protected void gvProducts_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                mtddlstatus(e);
            }
        }

    Thursday, September 17, 2020 9:08 AM