Question GridView disappears while MessageBox is showing (if used on tab page)

Ptolemy

New member
Joined
Jul 5, 2014
Messages
2
Programming Experience
Beginner
I have 3 GridViews, each inside a separate tab. Every row in the GridView is associated with a LinkButton, and when it's clicked a MessageBox are popping up, showing the content on that particular row.

Grid_View_Tabs1.png



The problem is that when the MessageBox pops up the GridView disappears, and when MessageBox is closed GridView then comes back.

Grid_View_Tabs2.png



This problem doesn't occur if GridView is used without any tabs and is placed outside the TabControl.

Here is my code:

C#:
protected void Page_Load(object sender, EventArgs e)
        {
            TabControl TheTabCtrl = new TabControl("InfoTabCtrl");


            for (var i = 0; i < 3; i++)
            {
                GridView newGridView = new GridView();
                //generate dynamic id        
                newGridView.ID = String.Concat("GridView", i);
                newGridView.AutoGenerateColumns = false;
                newGridView.RowDataBound += new GridViewRowEventHandler(OnRowDataBound);


                //if (!this.IsPostBack)
                //{
                    BoundField bfield = new BoundField();
                    bfield.HeaderText = "Id";
                    bfield.DataField = "Id";
                    newGridView.Columns.Add(bfield);


                    bfield = new BoundField();
                    bfield.HeaderText = "Name";
                    bfield.DataField = "Name";
                    newGridView.Columns.Add(bfield);


                    TemplateField tfield = new TemplateField();
                    tfield.HeaderText = "Country";
                    newGridView.Columns.Add(tfield);


                    tfield = new TemplateField();
                    tfield.HeaderText = "View";
                    newGridView.Columns.Add(tfield);
                //}
                    this.BindGrid(newGridView, i);


                    string myString = i.ToString();
                    TabPage BasicPage1 = new TabPage(myString, myString);
                    BasicPage1.Controls.Add(newGridView);
                    TheTabCtrl.Tabs.Add(BasicPage1);
            }


            if (!this.IsPostBack)
            {
                string value = Request.Form[TheTabCtrl.Id + "_SelectedTab"];
                if (!string.IsNullOrEmpty(value))
                {
                    try
                    {
                        TheTabCtrl.SelectedTab = TheTabCtrl.Tabs.IndexOf(TheTabCtrl.Tabs.Where(x => x.Id == value).First());
                    }
                    catch
                    {
                    }
                }
            }
            form1.Controls.Add(TheTabCtrl.GetControl);
         
        }


        private void BindGrid(GridView newGridView, int id)
        {
            string[][,] jaggedArray = new string[3][,] 
            {
                new string[,] { {"John Hammond", "United States"}, {"Mudassar Khan", "India"}, {"Suzanne Mathews", "France"}, {"Robert Schidner", "Russia"} },
                new string[,] { {"Zoey Melwick", "New Zeeland"}, {"Bryan Robertson", "England"}, {"Beth Stewart", "Australia"}, {"Amanda Rodrigues", "Portugal"} },
                new string[,] { {"Glenda Becker", "Germany"}, {"Despoina Athanasiadis", "Greece"}, {"Alexandra L?pez", "Spain"}, {"David Bouchard", "Canada"} }
            };
            
            for (int row = 0; row < jaggedArray.Length; row++)
            {
                if (id != row) continue;
                DataTable dt = new DataTable();
                // Share the same headlines
                dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
                        new DataColumn("Name", typeof(string)),
                        new DataColumn("Country",typeof(string)) });


                for (int pair = 0; pair < jaggedArray[row].Length / 2; pair++)
                {
   
                    dt.Rows.Add(pair + 1, jaggedArray[row][pair, 0], jaggedArray[row][pair, 1]);
                }


                string myPage = string.Concat(row, "page");
                string myString = row.ToString();


                newGridView.DataSource = dt;
                newGridView.DataBind();


            }//End for Row                 


        }//BindGrid


        protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                TextBox txtCountry = new TextBox();
                txtCountry.ID = "txtCountry";
                txtCountry.Text = (e.Row.DataItem as DataRowView).Row["Country"].ToString();


                e.Row.Cells[1].Width = 200;
                e.Row.Cells[2].Controls.Add(txtCountry);


                LinkButton lnkView = new LinkButton();
                lnkView.ID = "lnkView";
                lnkView.Text = "View";
                
                lnkView.Click += ViewDetails;
                
                lnkView.CommandArgument = (e.Row.DataItem as DataRowView).Row["Id"].ToString();
                e.Row.Cells[3].Controls.Add(lnkView);
                
            }
        }


        protected void ViewDetails(object sender, EventArgs e)
        {
            LinkButton lnkView = (sender as LinkButton);
            GridViewRow row = (lnkView.NamingContainer as GridViewRow);
            string id = lnkView.CommandArgument;
            string name = row.Cells[1].Text;
            string country = (row.FindControl("txtCountry") as TextBox).Text;
            ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Id: " + id + " Name: " + name + " Country: " + country + "')", true);
        }
 

Attachments

  • Grid_View_Tabs1.png
    Grid_View_Tabs1.png
    15.3 KB · Views: 54
Top Bottom