imnotverygoodatthis
imnotverygoodatthis

Reputation: 55

GridView displaying in display mode but not in browser ASP.NET C#

I'm relatively new to ASP.NET. My problem is I am trying to create a GridView and bind data to it by using a DataTable. My GridView element shows up in the design mode of VS 2012, but when I run it in the browser(IE), nothing displays. I have bound the data, there is clearly data entered in and I even have the EmptyDataText set to a value, so I am confused as to why NOTHING is displaying on the page from the GridView element. If I set other labels outside of GridView it displays fine, so I do not believe it is a hosting issue. Even when I turn the AutoGenerateColumns value to true, nothing happens. Any help at all would be extremely appreciated.

 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"  Inherits="Tester.Default" %> 

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>This is my page.</title>


<style type="text/css">
    table {
        border: 2px dashed #00FF00;
        padding: inherit;
        margin: inherit;
        width: auto;
        height: auto;
        top: auto;
        right: auto;
        bottom: auto;
        left: auto;
        background-color: #0000FF;
        color: #FFFFFF;
        font-weight: bold;
    }
</style>


</head>
<body>
<form runat="server" id="MyForm">
    <asp:GridView AutoGenerateColumns="false" ID="gv" runat="server" Width="1000px" Visible="true" BorderColor="Red" EmptyDataText="WHERE IS MY DATA???">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Label runat="server" Text="testing123">Label from GridView</asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="VenLogo" HeaderText="ID" />
            <asp:BoundField DataField="VenName" HeaderText="Website" />
            <asp:BoundField DataField="VenWeb" HeaderText="URL" HtmlEncode="false" />
        </Columns>
    </asp:GridView>

</form>
</body>
</html>

Here is my CodeBehind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using System.Text;


namespace Tester
{
public partial class Default : System.Web.UI.Page
{
    GridView gv = new GridView();

    protected void Page_Load(object sender, ObjectDataSourceStatusEventArgs e)
    {


        if (!Page.IsPostBack)
        {
            gv.DataSource = Datatable();
            gv.DataBind();
            gv.Visible = true;
        }


    }

    private DataTable Datatable()
    {
        DataTable datatable = new DataTable();

        datatable.Columns.Add("VenLogo", typeof(string));
        datatable.Columns.Add("VenName", typeof(string));
        datatable.Columns.Add("VenWeb", typeof(string));

        AddNewRow("Logo URL", "google", "http://google.com", datatable);
        AddNewRow("Logo URL", "facebook", "http://facebook.com", datatable);

        return datatable; 
    }

    private void AddNewRow(string id, string website, string url, DataTable table)
    {
        DataRow row = table.NewRow();
        row["VenLogo"] = id;
        row["VenName"] = website;
        //get url from GetURL method  
        string link = GetURL(website, url);
        row["VenWeb"] = HttpUtility.HtmlDecode(link);
        table.Rows.Add(row);
    }


    private string GetURL(string website, string url)
    {
        return "<a href=\"" + url + "\">" + website + "</a>";
    }  
}
}

Image of Split View in VS.

Upvotes: 2

Views: 10942

Answers (3)

Hisham
Hisham

Reputation: 455

Your CS file code would be like this.

protected void Page_Load(object sender, EventArgs e)
        {
            GridView gv = new GridView();

                gv.DataSource = Datatable();
                gv.DataBind();
                gv.Visible = true;
                MyForm.Controls.Add(gv);

        }
        private DataTable Datatable()
    {
        DataTable datatable = new DataTable();

        datatable.Columns.Add("VenLogo", typeof(string));
        datatable.Columns.Add("VenName", typeof(string));
        datatable.Columns.Add("VenWeb", typeof(string));

        AddNewRow("Logo URL", "google", "http://google.com", datatable);
        AddNewRow("Logo URL", "facebook", "http://facebook.com", datatable);

        return datatable; 
    }

    private void AddNewRow(string id, string website, string url, DataTable table)
    {
        table.Rows.Add(id, website, url);
    }


    private string GetURL(string website, string url)
    {
        return "<a href=\"" + url + "\">" + website + "</a>";
    }  

Upvotes: 1

AGuyCalledGerald
AGuyCalledGerald

Reputation: 8150

I am not sure, do you want to add the gridview dynamically to the page or use the one from your markup? If the first, you need to add the statement

MyForm.Controls.Add(gv);

to Page_Load. If the latter, you don´t need

GridView gv = new GridView();

but can just reference gv from markup by it´s ID.

By the way, you also have to change the parameter type of the Page_Load:

protected void Page_Load(object sender, EventArgs e)

Upvotes: 1

Josh
Josh

Reputation: 10624

Your local instance of gv is screwing things up. Check other parts of your partial class for a definition of gv. I'm guessing that you Page_Load code is binding to a local, private instance, instead of the protected instance that the page is using for the control. You'll want something like the following:

protected global::System.Web.UI.WebControls.GridView gv;

Upvotes: 1

Related Questions