ViewState for ASP.Net GridView paging on LinkButton click using C#

Last Reply 9 months ago By kalpesh

Posted 9 months ago

I have a gridview which shows how many issues are open,pending and completed and when i click open only the issues which are opened are displayed in second gridview similarly when i click pending only the issues which are pending are displayed in same second gridview and when i click completed only the issues which are completed are displayed in second gridview based on the selection of link button the issyes are binded to second gridview. my question is how can i call the link button in page index changing event of second gridview

 protected void lnkOpen_Click(object sender, EventArgs e)
    {
        DataTable dt = adm.GetBugsToView("Open", ddlProject0.SelectedValue);
        gvShowBugs.Visible = true;
        lnkDownload1.Visible = true;
        if (dt.Rows.Count > 0)
        {
            gvShowBugs.DataSource = dt;
            gvShowBugs.DataBind();

        }
        else
        {
            gvShowBugs.DataSource = null;
            gvShowBugs.DataBind();
        }
        ViewState["Clicked"] = "Open";
    }

    protected void lnkInpen_Click(object sender, EventArgs e)
    {
        DataTable dt = adm.GetBugsToView("In-Progress", ddlProject0.SelectedValue);
        gvShowBugs.Visible = true;
        lnkDownload1.Visible = true;
        if (dt.Rows.Count > 0)
        {
            gvShowBugs.DataSource = dt;
            gvShowBugs.DataBind();

        }
        else
        {
            gvShowBugs.DataSource = null;
            gvShowBugs.DataBind();
        }
        ViewState["Clicked"] = "InProgress";
    }

    protected void lnkComp_Click(object sender, EventArgs e)
    {
        DataTable dt = adm.GetBugsToView("ReTest", ddlProject0.SelectedValue);
        gvShowBugs.Visible = true;
        lnkDownload1.Visible = true;
        if (dt.Rows.Count > 0)
        {
            gvShowBugs.DataSource = dt;
            gvShowBugs.DataBind();

        }
        else
        {
            gvShowBugs.DataSource = null;
            gvShowBugs.DataBind();
        }
        ViewState["Clicked"] = "ReTest";
    }

I need to call the bind the three view states data based on the click of link button how can i do this

 

 protected void gvShowBugs_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
 
    }

 

Results 1 - 5 of 9 12
Posted 9 months ago

Try by using below code. Make one common method to call on all three link button click event as all code is same just need to pass the option value to GetBugsToView method. Also call same common method on PageIndexChanging on Gridview by passing the Viewstate value. If any chanegs in code modify it as per your code logic.

C#

protected void lnkOpen_Click(object sender, EventArgs e)
{
    string clickedValue = "Open";
    BindShowBugs(clickedValue);
    ViewState["Clicked"] = clickedValue;
}

protected void lnkInpen_Click(object sender, EventArgs e)
{
    string clickedValue = "In-Progress";
    BindShowBugs(clickedValue);
    ViewState["Clicked"] = clickedValue;
}

protected void lnkComp_Click(object sender, EventArgs e)
{
    string clickedValue = "ReTest";
    BindShowBugs(clickedValue);
    ViewState["Clicked"] = clickedValue;
}

private void BindShowBugs(string optionValue)
{
    DataTable dt = adm.GetBugsToView(optionValue, ddlProject0.SelectedValue);
    gvShowBugs.Visible = true;
    lnkDownload1.Visible = true;
    if (dt.Rows.Count > 0)
    {
        gvShowBugs.DataSource = dt;
        gvShowBugs.DataBind();

    }
    else
    {
        gvShowBugs.DataSource = null;
        gvShowBugs.DataBind();
    }
}

protected void gvShowBugs_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    gvShowBugs.PageIndex = e.NewPageIndex;
    BindShowBugs(Convert.ToString(ViewState["Clicked"]));
}

 


Posted 9 months ago

but it is not working i am trying to display the data in second gridview based on project selection and link button selection

 

protected void lnkOpen_Click(object sender, EventArgs e)
   {
       DataTable dt = adm.GetBugsToView("Open", ddlProject0.SelectedValue);
       gvShowBugs.Visible = true;
       lnkDownload1.Visible = true;
       if (dt.Rows.Count > 0)
       {
           gvShowBugs.DataSource = dt;
           gvShowBugs.DataBind();
 
       }
       else
       {
           gvShowBugs.DataSource = null;
           gvShowBugs.DataBind();
       }
       ViewState["Clicked"] = "Open";
   }
 
   protected void lnkInpen_Click(object sender, EventArgs e)
   {
       DataTable dt = adm.GetBugsToView("In-Progress", ddlProject0.SelectedValue);
       gvShowBugs.Visible = true;
       lnkDownload1.Visible = true;
       if (dt.Rows.Count > 0)
       {
           gvShowBugs.DataSource = dt;
           gvShowBugs.DataBind();
 
       }
       else
       {
           gvShowBugs.DataSource = null;
           gvShowBugs.DataBind();
       }
       ViewState["Clicked"] = "InProgress";
   }
 
   protected void lnkComp_Click(object sender, EventArgs e)
   {
       DataTable dt = adm.GetBugsToView("ReTest", ddlProject0.SelectedValue);
       gvShowBugs.Visible = true;
       lnkDownload1.Visible = true;
       if (dt.Rows.Count > 0)
       {
           gvShowBugs.DataSource = dt;
           gvShowBugs.DataBind();
 
       }
       else
       {
           gvShowBugs.DataSource = null;
           gvShowBugs.DataBind();
       }
       ViewState["Clicked"] = "ReTest";
   }

 


Posted 9 months ago Modified on 9 months ago

Debug the code and check the values which are passing

also share your HTML And code


Posted 9 months ago

 

public void BindGrid()
    {
        if (string.IsNullOrEmpty(BugID_Edit))
        {
            DataTable dt = adm.GetBugs();
            if (dt.Rows.Count > 0)
            {
                gvShow.DataSource = dt;
                gvShow.DataBind();
            }
            else
            {
                gvShow.DataSource = null;
                gvShow.DataBind();
            }
        }
        else
        {
            EditBug(BugID_Edit);
            pnlNew.Visible = true;
            pnlView.Visible = true;

            DataTable dt = adm.GBToE(BID_Edit);
            if (dt.Rows.Count > 0)
            {
                gvShow.DataSource = dt;
                gvShow.DataBind();
            }
            else
            {
                gvShow.DataSource = null;
                gvShow.DataBind();
            }
            btnSubmit.Text = "Update";
        }

    }
  protected void lnkNew_Click(object sender, EventArgs e)
    {
        DataTable dt = adm.GBToV("New", ddlPro.SelectedValue);
        gvShow.Visible = true;
        lnkDownload1.Visible = true;
        if (dt.Rows.Count > 0)
        {
            gvShow.DataSource = dt;
            gvShow.DataBind();

        }
        else
        {
            gvShow.DataSource = null;
            gvShow.DataBind();
        }
        ViewState["Clicked"] = "New";

    }

    protected void lnkOp_Click(object sender, EventArgs e)
    {
        DataTable dt = adm.GBToV("Open", ddlPro.SelectedValue);
        gvShow.Visible = true;
        lnkDownload1.Visible = true;
        if (dt.Rows.Count > 0)
        {
            gvShow.DataSource = dt;
            gvShow.DataBind();

        }
        else
        {
            gvShow.DataSource = null;
            gvShow.DataBind();
        }
        ViewState["Clicked"] = "Open";
    }

    protected void lnkInPro_Click(object sender, EventArgs e)
    {
        DataTable dt = adm.GetBToVi("In-Progress", ddlPro.SelectedValue);
        gvShow.Visible = true;
        lnkDownload1.Visible = true;
        if (dt.Rows.Count > 0)
        {
            gvShow.DataSource = dt;
            gvShow.DataBind();

        }
        else
        {
            gvShow.DataSource = null;
            gvShow.DataBind();
        }
        ViewState["Clicked"] = "InProgress";
    }

    protected void lnkReTe_Click(object sender, EventArgs e)
    {
        DataTable dt = adm.GBToV("ReTest", ddlPro.SelectedValue);
        gvShow.Visible = true;
        lnkDownload1.Visible = true;
        if (dt.Rows.Count > 0)
        {
            gvShow.DataSource = dt;
            gvShow.DataBind();

        }
        else
        {
            gvShow.DataSource = null;
            gvShow.DataBind();
        }
        ViewState["Clicked"] = "ReTest";
    }

    protected void lnkRejec_Click(object sender, EventArgs e)
    {
        DataTable dt = adm.GBToV("Rejected", ddlPro.SelectedValue);
        gvShow.Visible = true;
        lnkDownload1.Visible = true;
        if (dt.Rows.Count > 0)
        {
            gvShow.DataSource = dt;
            gvShow.DataBind();

        }
        else
        {
            gvShowBugs.DataSource = null;
            gvShowBugs.DataBind();
        }
        ViewState["Clicked"] = "Rejected";
    }

    protected void lnkRe_Click(object sender, EventArgs e)
    {
        DataTable dt = adm.GBToV("Requirement", ddlPro.SelectedValue);
        gvShowBugs.Visible = true;
        lnkDownload1.Visible = true;
        if (dt.Rows.Count > 0)
        {
            gvShow.DataSource = dt;
            gvShow.DataBind();

        }
        else
        {
            gvShow.DataSource = null;
            gvShow.DataBind();
        }
        ViewState["Clicked"] = "Requirement";
    }

    protected void lnkClo_Click(object sender, EventArgs e)
    {
        DataTable dt = adm.GBToV("Closed", ddlPro.SelectedValue);
        gvShow.Visible = true;
        lnkDownload1.Visible = true;
        if (dt.Rows.Count > 0)
        {
            gvShow.DataSource = dt;
            gvShow.DataBind();

        }
        else
        {
            gvShow.DataSource = null;
            gvShow.DataBind();
        }
        ViewState["Clicked"] = "Closed";
    }
 <asp:GridView ID="gvShowBugs" runat="server" AutoGenerateColumns="false" AlternatingRowStyle-CssClass="alt" PagerStyle-CssClass="pgr"
                OnRowCommand="gvShowBugs_RowCommand" Width="100%" OnRowDataBound="gvShowBugs_RowDataBound" PageSize="05" AllowPaging="true" OnPageIndexChanging="gvShowBugs_PageIndexChanging">
                <Columns>
                    <asp:TemplateField HeaderText="Sir ID" ItemStyle-HorizontalAlign="Center">
                        <ItemTemplate>
                            <asp:Label ID="lbl1" runat="server"></asp:Label>
                            <asp:LinkButton runat="server" Text='<%# Eval("BugId") %>' CommandName="EditRow" CommandArgument='<%# Eval("BugId") %>'></asp:LinkButton>
                            <%--<asp:ImageButton ID="IbtnEdit" ImageUrl="Image/edit.png" runat="server" Width="12" Height="15" CommandName="EditRow" ToolTip="Edit" CommandArgument='<%# Eval("BugId") %>' />   --%>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="BugTitle" HeaderText="Sir Title" />
                    <asp:BoundField DataField="ProjectName" HeaderText="ProjectName" />
                    <asp:BoundField DataField="Status" HeaderText="Status" />
                    <asp:BoundField DataField="Priority" HeaderText="Priority" />
                    <%--  <asp:BoundField DataField="BugDesc" HeaderText="BugDesc" />
                <asp:BoundField DataField="DeveloperDesc" HeaderText="DeveloperDesc" />--%>
                    <asp:BoundField DataField="CreatedBy" HeaderText="Raised By" />
                    <asp:BoundField DataField="CreatedDate" HeaderText="CreatedDate" />
                    <asp:BoundField DataField="AccetedBy" HeaderText="Accepeted By" />
                    <asp:TemplateField HeaderText="Action">
                        <ItemTemplate>
                            <asp:LinkButton ID="lnkAccept" runat="server" Text="Accept" CommandName="Accept" CommandArgument='<%# Eval("BugId") %>'></asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <%-- <asp:TemplateField HeaderText="SIR_Report">
                    <ItemTemplate>
                        <asp:LinkButton ID="lnkDownload" runat="server" Text="Download" OnClick="lnkDownload_Click" CommandArgument='<%# Eval("BugReportURL") %>'></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>--%>
                </Columns>
            </asp:GridView>

 


Posted 9 months ago Modified on 9 months ago
kavithav says:
 DataTable dt = adm.GetBugs();

 

kavithav says:
DataTable dt = adm.GBToE(BID_Edit);

 

kavithav says:
DataTable dt = adm.GBToV("New", ddlPro.SelectedValue);

 

kavithav says:
DataTable dt = adm.GetBToVi("In-Progress", ddlPro.SelectedValue);

You are binding the Second gridview from the different Methods each time so how can you need to retain the bind grid functionality on page index after index get changed you also need to specify which method need to call as per the view state value .

Put the debugger in each method after your Page index event get changed you need to check what exactly happening which method get called. According to that you need to call the Method GetBug , GetToE,GetToV,GetToVi Or any other as per you last action.

Always be proper with your code and Html before posting the question so other user can reply you accordingly to your problem. as code in main question is diffrent and current last reply question code is diffrent.

Try to debug on page index changing and find the problem as you are calling many methods to populate the same grid.