私は ASP.NET に比較的慣れていません。私の問題は、GridView を作成し、DataTable を使用してデータをバインドしようとしていることです。VS 2012 のデザイン モードでは GridView 要素が表示されますが、ブラウザー (IE) で実行すると何も表示されません。データをバインドしました。明らかにデータが入力されており、EmptyDataText が値に設定されているため、GridView 要素からページに何も表示されない理由について混乱しています。GridView の外部に他のラベルを設定すると、正常に表示されるため、ホスティングの問題ではないと思います。AutoGenerateColumns の値を true にしても、何も起こりません。どんな助けでも大歓迎です。
<%@ 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>
これが私のコードビハインドです
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>";
}
}
}