Good Day everyone,

I have a web application that is a basic input form. The input form has a dynamic gridview that allows users to enter any number of rows to enter trip information.

Most everything is working well except two aspects;

  1. I have continued errors that say gridviewName is not in the current context
  2. Finally the more important issue lies in the fact that my gridview does not appear in debug in the browser (regardless of the browser chosen).

I have researched this for a week now and I think I need a new set of eyes on this. What could I be missing in my codebehind and aspx pages to make my gridview show up and the CodeBehind page to recognize the designer file.

<%@ Page Language="C#" AutoEventWireup="True" MasterPageFile="~/Site.Master" CodeBehind="intakeTripDetails.aspx.cs" Inherits="WebApplication2.TravelerInformation" %>

<asp:Content ID="intakeTripDets" ContentPlaceHolderID="MainContent" runat="server">
        <fieldset>
            <legend>Trip Details</legend>
            <div class="input">
                <p>Please enter trip details on the below form.  You can add additional rows as needed for each leg of our trip.</p>
                <br />
                <asp:Label runat="server" ID="lblMessage" Text="" ></asp:Label><br />
                <label>Flight Number(s)</label><asp:TextBox runat="server"></asp:TextBox>
                <br /><br /><br /><br />
                <asp:GridView ID="tripDetails" runat="server" ShowFooter="True" ShowHeaderWhenEmpty="True"> 
                    <Columns>
                        <asp:BoundField DataField="RowNumber" HeaderText="" />
                        <asp:TemplateField HeaderText="Arrival Date & Time" >
                            <ItemTemplate>
                                <asp:TextBox ID="arrivalDateTime" runat="server"></asp:TextBox>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Departure Date & Time">
                            <ItemTemplate>
                                <asp:TextBox ID="departDateTime" runat="server"></asp:TextBox>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Country">
                            <ItemTemplate>
                            <asp:DropDownList ID="countryList" runat="server" AutoPostBack="true" AppendDataBoundItems="true">
                                 <asp:ListItem Value="-1">--SELECT--</asp:ListItem>
                            </asp:DropDownList>
                                </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="City">
                            <ItemTemplate>
                            <asp:TextBox ID="city" runat="server">
                            </asp:TextBox>
                                </ItemTemplate>
                        <FooterStyle HorizontalAlign="Right" />
                        <FooterTemplate>
                            <asp:Button ID="btnAdd" runat="server" Text="Add New Row" />
                        </FooterTemplate>
                            </asp:TemplateField>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:LinkButton ID="lnkRemove" runat="server">Remove</asp:LinkButton>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>   
                <div class="btnfmt"><%--<asp:Button ID="prev" runat="server" Text="Previous: Traveler Information" />--%>
                <asp:Button ID="submit" runat="server" text="Submit Trip"/> </div>
            </div>
       </fieldset> </asp:Content>

CodeBehind:

using System;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace travelDetails
{
    public partial class DynamicGrid : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                SetInitialRow();
            }
        }

        private ArrayList countryData()
        {
            ArrayList arr = new ArrayList();

            arr.Add(new ListItem("Afghanistan", "1"));
            arr.Add(new ListItem("Aland Islands", "2"));
            arr.Add(new ListItem("Albania", "3"));
            arr.Add(new ListItem("Algeria", "4"));
            arr.Add(new ListItem("American Samoa", "5"));
            arr.Add(new ListItem("Andorra", "6"));
            arr.Add(new ListItem("Angola", "7"));
            arr.Add(new ListItem("Anguilla", "8"));
            arr.Add(new ListItem("Antarctica", "9"));
            arr.Add(new ListItem("Antigua and Barbuda", "10"));
            arr.Add(new ListItem("Argentina", "11"));
            arr.Add(new ListItem("Armenia", "12"));
            arr.Add(new ListItem("Aruba", "13"));
            arr.Add(new ListItem("Australia", "14"));
            arr.Add(new ListItem("Austria", "15"));
            arr.Add(new ListItem("Azerbaijan", "16"));
            arr.Add(new ListItem("Bahamas", "17"));
            arr.Add(new ListItem("Bahrain", "18"));
            arr.Add(new ListItem("Bangladesh", "19"));
            arr.Add(new ListItem("Barbados", "20"));

            return arr;
        }

                private void FillDropDownList(DropDownList ddl)
        {
            ArrayList arr = countryData();

            foreach (ListItem item in arr)
            {
                ddl.Items.Add(item);
            }
        }

            private void SetInitialRow()
            {
                DataTable dt = new DataTable();
                DataRow dr = null;

                dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
                dt.Columns.Add(new DataColumn("arrivalDateTime", typeof(DateTime)));
                dt.Columns.Add(new DataColumn("departDateTime", typeof(DateTime)));
                dt.Columns.Add(new DataColumn("city", typeof(string)));
                dt.Columns.Add(new DataColumn("countryList", typeof(string)));

                dr = dt.NewRow();
                dr["RowNumber"] = 1;
                dr["arrivalDateTime"] = string.Empty;
                dr["departDateTime"] = string.Empty;
                dr["city"] = string.Empty;
                dt.Rows.Add(dr);

                //What if I want to see the data for future reference
                ViewState["CurrentTable"] = dt;

                //Data is stuck here
                tripDetails.DataSource = dt;
                tripDetails.DataBind();

                DropDownList ddl1 = (DropDownList)tripDetails.Rows[0].Cells[4].FindControl("countryList");

                FillDropDownList(ddl1);
            }

            private void AddNewRowToGrid()
            {
                int rowIndex = 0;
                if (ViewState["CurrentTable"] != null)
                {
                    DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
                    DataRow drCurrentRow = null;

                    if (dtCurrentTable.Rows.Count > 0)
                    {

                        drCurrentRow = dtCurrentTable.NewRow();
                        drCurrentRow["RowNumber"] = dtCurrentTable.Rows.Count + 1;

                        for (int i = 0; i < dtCurrentTable.Rows.Count - 1; i++)
                        {

                            //extract the textbox values
                            TextBox box1 = (TextBox)tripDetails.Rows[i].Cells[1].FindControl("arrivalDateTime");
                            TextBox box2 = (TextBox)tripDetails.Rows[i].Cells[2].FindControl("departDateTime");
                            TextBox box3 = (TextBox)tripDetails.Rows[i].Cells[3].FindControl("city");

                            dtCurrentTable.Rows[i]["arrivalDateTime"] = box1.Text;
                            dtCurrentTable.Rows[i]["departDateTime"] = box2.Text;
                            dtCurrentTable.Rows[i]["city"] = box3.Text;

                            //extract the ddl values
                            DropDownList ddl1 = (DropDownList)tripDetails.Rows[i].Cells[4].FindControl("countryList");

                            //Update the DataRow with the ddl items selected
                            dtCurrentTable.Rows[i]["countryList"] = ddl1.SelectedItem.Text;
                        }

                        //Adds new row to the data table
                        dtCurrentTable.Rows.Add(drCurrentRow);

                        //Again...we probably want to refer to this again
                        ViewState["CurrentTable"] = dtCurrentTable;

                        //Rebind the grid with the current data
                        tripDetails.DataSource = dtCurrentTable;
                        tripDetails.DataBind();
                    }

                    else
                    {
                        Response.Write("View State is Null");
                    }

                    //Set previous data on postbacks
                    SetPreviousData();
                }
            }

            private void SetPreviousData()
            {
                int rowIndex = 0;
                if (ViewState["CurrentTable"] != null)
                {
                    DataTable dt = (DataTable)ViewState["CurrentTable"];
                    if (dt.Rows.Count > 0)
                    {
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            TextBox box1 = (TextBox)tripDetails.Rows[i].Cells[2].FindControl("arrivalDateTime");
                            TextBox box2 = (TextBox)tripDetails.Rows[i].Cells[3].FindControl("departDateTime");
                            TextBox box3 = (TextBox)tripDetails.Rows[i].Cells[4].FindControl("city");

                            DropDownList ddl1 = (DropDownList)tripDetails.Rows[rowIndex].Cells[4].FindControl("countryList");

                            //Fill the ddl with data
                            FillDropDownList(ddl1);

                            if (i < dt.Rows.Count - 1)
                            {
                                //Assign the value from the datatable to the textbox
                                box1.Text = dt.Rows[i]["arrivalDateTime"].ToString();
                                box2.Text = dt.Rows[i]["departDateTime"].ToString();
                                box3.Text = dt.Rows[i]["city"].ToString();

                                //Set the previously selected items to each ddl
                                ddl1.ClearSelection();
                                ddl1.Items.FindByText(dt.Rows[i]["countryList"].ToString()).Selected = true;
                            }
                            rowIndex++;
                        }
                    }
                }
            }

            protected void btnAdd_Click(object sender, EventArgs e)
            {
                AddNewRowToGrid();
            }

            protected void tripDetails_RowCreated(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    DataTable dt = (DataTable)ViewState["CurrentTable"];
                    LinkButton lb = (LinkButton)e.Row.FindControl("lnkRemove");
                    if (lb != null)
                    {
                        if (dt.Rows.Count > 1)
                        {
                            if (e.Row.RowIndex == dt.Rows.Count - 1)
                            {
                                lb.Visible = true;
                            }
                        }
                        else
                        {
                            lb.Visible = true;
                        }
                    }
                }
            }

            protected void lnkRemove_Click(object sender, EventArgs e)
            {
                LinkButton lb = (LinkButton)sender;
                GridViewRow gvRow = (GridViewRow)lb.NamingContainer;
                int rowID = gvRow.RowIndex;
                if (ViewState["CurrentTable"] != null)
                {
                    DataTable dt = (DataTable)ViewState["CurrentTable"];
                    if (dt.Rows.Count > 1)
                    {
                        if (gvRow.RowIndex < dt.Rows.Count - 1)
                        {

                            //Remove selected row data and reset row number
                            dt.Rows.Remove(dt.Rows[rowID]);
                            ResetRowID(dt);
                        }
                    }

                    //And another future reference
                    ViewState["CurrentTable"] = dt;

                    //Bind the data to the gridview again with updated data
                    tripDetails.DataSource = dt;
                    tripDetails.DataBind();
                }

                //Set previous data on postback
                SetPreviousData();
            }

            private void ResetRowID(DataTable dt)
            {
                int rowNumber = 1;
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow row in dt.Rows)
                    {
                        row[0] = rowNumber;
                        rowNumber++;
                    }
                }
            }

I have tried everything that I can think of - 1. Deleting Designer file and 'Converting to Web Application' 2. Redoing the code in another file 3. Recreating the whole project 4. Making everything visible/invisible

Any help would be great!

Related posts

Recent Viewed