locked
How to add multiple linkbutton in gridview Dynamically RRS feed

  • Question

  • User-462704258 posted

    I have to add linkbutton to each row in gridview dynamically. When i'm running my code i'm getting error of object reference.

    each link button is fired separate query so i have to check which button is click in which row and column.
    Here is my code:

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (division == "SOUTH")
            {
                LinkButton lbl = (LinkButton)e.Row.Cells[1].FindControl("LinkButton1");
                foreach (GridViewRow r in GridView1.Rows)
                {
                    //LinkButton lbl = (LinkButton)e.Row.Cells[1].FindControl("LinkButton1");
                    string str = r.Cells[0].Text;
                    string str1 = str.Substring(0, str.IndexOf("(")).Trim();
    
                    if (str1 == "Abnormal")
                    {
                        con.Open();
                        cmd = new OracleCommand(" ", con);
                        dr = cmd.ExecuteReader();
    
                        while (dr.Read())
                        {
                            lbl.Text = dr[0].ToString();
                        }
                        r.Cells[1].Controls.Add(lbl);
                        con.Close();
                    }
                    else if (str1 == "Sub-Normal")
                    {
                        con.Open();
                        cmd = new OracleCommand(" ", con);
                        dr = cmd.ExecuteReader();
                        while (dr.Read())
                        {
                            r.Cells[1].Text = dr[0].ToString();
                        }
                        con.Close();
                    }
                    else if (str1 == "Normal")
                    {
                        con.Open();
                        cmd = new OracleCommand(" ", con);
                        dr = cmd.ExecuteReader();
                        while (dr.Read())
                        {
                            r.Cells[1].Text = dr[0].ToString();
                        }
                        con.Close();
                    }
                    else if (str1 == "Critical")
                    {
                        con.Open();
                        cmd = new OracleCommand(" ", con);
                        dr = cmd.ExecuteReader();
                        while (dr.Read())
                        {
                            r.Cells[1].Text = dr[0].ToString();
                        }
                        con.Close();
                    }
                    else if (str1 == "Super Critical")
                    {
                        con.Open();
                        cmd = new OracleCommand(" ", con);
                        dr = cmd.ExecuteReader();
                        while (dr.Read())
                        {
                            r.Cells[1].Text = dr[0].ToString();
                        }
                        con.Close();
                    }
                }
                for (int i = 2; i < GridView1.Columns.Count; i++)
                {
                    GridView1.Columns[i].Visible = false;
                }
                GridView1.Columns[6].Visible = true;
            }
        }
    }

    Thanks in advanced.

    Wednesday, January 25, 2017 6:10 AM

All replies

  • User-707554951 posted


    HI ryan_xcoder,

    Please try to use OnRowDataBound to add the linkbutton dynamically.
    And LinkButton lbl = (LinkButton)e.Row.Cells[1].FindControl("LinkButton1") is to find an exist control in the gridview not to create one.

    You could refer to below code:

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    if (division == "SOUTH")
                    {
                        LinkButton lbl = new LinkButton();
                        foreach (GridViewRow r in GridView1.Rows)
                        {
                            string str = r.Cells[0].Text;
                            string str1 = str.Substring(0, str.IndexOf("(")).Trim();
    
                            if (str1 == "Abnormal")
                            {
                                con.Open();
                                cmd = new OracleCommand(" ", con);
                                dr = cmd.ExecuteReader();
    
                                while (dr.Read())
                                {
                                    lbl.Text = dr[0].ToString();
                                }
                                r.Cells[1].Controls.Add(lbl);
                                con.Close();
                            }
                            else if (str1 == "Sub-Normal")
                            {
                                con.Open();
                                cmd = new OracleCommand(" ", con);
                                dr = cmd.ExecuteReader();
                                while (dr.Read())
                                {
                                    lbl.Text = dr[0].ToString();
                                }
                                r.Cells[1].Controls.Add(lbl);
                                con.Close();
                            }
                            else if (str1 == "Normal")
                            {
                                con.Open();
                                cmd = new OracleCommand(" ", con);
                                dr = cmd.ExecuteReader();
                                while (dr.Read())
                                {
                                    lbl.Text.Text = dr[0].ToString();
                                }
                                r.Cells[1].Controls.Add(lbl);
                                con.Close();
                            }
                            else if (str1 == "Critical")
                            {
                                con.Open();
                                cmd = new OracleCommand(" ", con);
                                dr = cmd.ExecuteReader();
                                while (dr.Read())
                                {
                                    lbl.Text = dr[0].ToString();
                                }
                                r.Cells[1].Controls.Add(lbl);
                                con.Close();
                            }
                            else if (str1 == "Super Critical")
                            {
                                con.Open();
                                cmd = new OracleCommand(" ", con);
                                dr = cmd.ExecuteReader();
                                while (dr.Read())
                                {
                                    lbl.Text = dr[0].ToString();
                                }
                                r.Cells[1].Controls.Add(lbl);
                                con.Close();
                            }
                        }
                        for (int i = 2; i < GridView1.Columns.Count; i++)
                        {
                            GridView1.Columns[i].Visible = false;
                        }
                        GridView1.Columns[6].Visible = true;
                    }
                }
            }
    
    
    

    Best Regards

    Cathy

    Thursday, January 26, 2017 6:23 AM