GridView displaying in display mode but not in browser ASP.NET C#
Solution 1
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)
Solution 2
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;
Solution 3
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>";
}
imnotverygoodatthis
Updated on June 14, 2022Comments
-
imnotverygoodatthis almost 2 years
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>"; } } }
-
imnotverygoodatthis almost 11 yearsThat makes a lot of sense. Thank you for your reply. I removed the localized instance of gv and placed the protected instance in the partial class like you suggested. This, however, still did not fix the problem. The GridView is still not rendering in the browser.
-
imnotverygoodatthis almost 11 yearsThank you for your response. My intention was to add the gridview dynamically to the page. I tried your suggestion and added the MyForm.Controls.Add(gv); to the Page_Load, however the gridview is still not rendering, even after I removed the HTML markup.
-
imnotverygoodatthis almost 11 yearsI could cry I am so happy. Thank you so much for your response and help! This was driving me crazy.
-
AGuyCalledGerald almost 11 yearsnice, is always a pleasure ;)