我正在尝试将数据绑定(bind)到名为“mygv”的网格,但出现错误“名称“mygv”在当前上下文中不存在”。这里有类似的问题,但提供的解决方案对我不起作用。这就是我所做的。
new.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;
using System.Text.RegularExpressions;
namespace DistrictEfts
{
public partial class _new : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void Generate_Efts(object sender, System.EventArgs e)
{
StreamReader sr=null;
DataTable dt = new DataTable();
DataRow dr;
string column1 = "";
string column2="";
string column3="";
string column4="";
string column5="";
string column6="";
string column7="";
string column8="";
string column9="";
try
{
sr = new StreamReader(Server.MapPath("~\\1623107554X.PAY"));
string eachline = "";
string[] colval = null;
int i = 0;
while (!sr.EndOfStream)
{
//Read line by line
eachline = sr.ReadLine();
//Formatting the file contents
string s = Regex.Replace(eachline , @"[^\w'""&:;-()-]+", " ,");
colval = eachline.Split(',');
if (i == 0)
{
column1 = colval[0];
column2 = colval[1];
column3 = colval[2];
column4 = colval[3];
column5 = colval[4];
column6 = colval[5];
column7 = colval[6];
column8 = colval[7];
column9 = colval[8];
dt.Columns.Add(column1);
dt.Columns.Add(column2);
dt.Columns.Add(column3);
dt.Columns.Add(column4);
dt.Columns.Add(column5);
dt.Columns.Add(column6);
dt.Columns.Add(column7);
dt.Columns.Add(column8);
dt.Columns.Add(column9);
i = i + 1;
}
//Add values in the datatable columns
else
{
dr = dt.NewRow();
dr[ column1] = colval[0];
dr[column2] = colval[1];
dr[column3] = colval[2];
dr[column4] = colval[3];
dr[column5] = colval[4];
dr[column6] = colval[5];
dr[column7] = colval[6];
dr[column8] = colval[7];
dr[column9] = colval[8];
dt.Rows.Add(dr);
}
}
sr.Close();
mygv.DataSource = dt;
mygv.DataBind();
}
catch (Exception ex)
{
}
finally
{
}
}
}
}
new.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="new.aspx.cs" Inherits=" _new" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table cellpadding="0" cellspacing="0" width="800" align="center">
<tr>
<td height="60">
<p> District efts</p>
</td>
</tr>
<tr>
<td height="30" align="center">
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td height="50" align="center">
<asp:Button ID="Button2" runat="server" Text="Generate Efts"
onclick="Generate_Efts" />
</td>
</tr>
<tr>
<td align="center">
<asp:GridView ID="mygv" runat="server" Width="400">
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
将不胜感激任何帮助。 谢谢。
最佳答案
几个问题。首先,在 asp.net 2.0 中不使用 CodeBehind 属性。您应该改用 CodeFile。
接下来,您不能将 Page 类放在命名空间中 - 如果可以,它必须与 Inherits 属性中引用的相同:TaskList 与 TaskGridList。
关于c# - Asp.net 和 C# 错误 : The name "mygv" does not exist in the current context,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28938191/