C# 在没有SQLDatasource的情况下绑定Gridview时,无法从Gridview获取值?
我正在GridView1中的Page_load方法中绑定一些数据。我有一个复选框1。选中复选框后,我想在按下按钮时在Label1中显示一些内容(作为测试)。但它没有表现出来。我不是用SQLDatasource绑定数据,而是手动绑定 我的页面加载方法C# 在没有SQLDatasource的情况下绑定Gridview时,无法从Gridview获取值?,c#,asp.net,gridview,C#,Asp.net,Gridview,我正在GridView1中的Page_load方法中绑定一些数据。我有一个复选框1。选中复选框后,我想在按下按钮时在Label1中显示一些内容(作为测试)。但它没有表现出来。我不是用SQLDatasource绑定数据,而是手动绑定 我的页面加载方法 protected void Page_Load(object sender, EventArgs e) { string[,] arrMultiD = { { "John", "21", "Berlin"
protected void Page_Load(object sender, EventArgs e)
{
string[,] arrMultiD = {
{ "John", "21", "Berlin", "Germany" },
{ "Smith", "33" ,"London", "UK"},
{ "Ryder", "15" ,"Sydney", "Australia"},
{ "Jake", "18", "Tokyo", "Japan"},
{ "Tom","34" , "Mumbai", "India"}
};
DataTable dt = new DataTable();
dt.Columns.Add("Name", Type.GetType("System.String"));
dt.Columns.Add("Age", Type.GetType("System.String"));
dt.Columns.Add("City", Type.GetType("System.String"));
dt.Columns.Add("Country", Type.GetType("System.String"));
for (int i = 0; i < 5; i++)
{
dt.Rows.Add();
dt.Rows[dt.Rows.Count - 1]["Name"] = arrMultiD[i, 0];
dt.Rows[dt.Rows.Count - 1]["Age"] = arrMultiD[i, 1];
dt.Rows[dt.Rows.Count - 1]["City"] = arrMultiD[i, 2];
dt.Rows[dt.Rows.Count - 1]["Country"] = arrMultiD[i, 3];
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
我已在调试模式下检查。当我点击按钮时,if部分没有被执行。就好像我一单击按钮GridView中的数据就丢失了一样。换句话说,GridView在超出页面加载方法时丢失了数据
protected void Page_Load(object sender, EventArgs e)
{
string[,] arrMultiD = {
{ "John", "21", "Berlin", "Germany" },
{ "Smith", "33" ,"London", "UK"},
{ "Ryder", "15" ,"Sydney", "Australia"},
{ "Jake", "18", "Tokyo", "Japan"},
{ "Tom","34" , "Mumbai", "India"}
};
DataTable dt = new DataTable();
dt.Columns.Add("Name", Type.GetType("System.String"));
dt.Columns.Add("Age", Type.GetType("System.String"));
dt.Columns.Add("City", Type.GetType("System.String"));
dt.Columns.Add("Country", Type.GetType("System.String"));
for (int i = 0; i < 5; i++)
{
dt.Rows.Add();
dt.Rows[dt.Rows.Count - 1]["Name"] = arrMultiD[i, 0];
dt.Rows[dt.Rows.Count - 1]["Age"] = arrMultiD[i, 1];
dt.Rows[dt.Rows.Count - 1]["City"] = arrMultiD[i, 2];
dt.Rows[dt.Rows.Count - 1]["Country"] = arrMultiD[i, 3];
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
我的aspx代码
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateDeleteButton="True">
<Columns>
<asp:TemplateField HeaderText="dd">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" />
<br />
<asp:Label ID="Label1" runat="server"></asp:Label>
</form>
如果(!Page.IsPostBack)
{
字符串[,]arrMultiD={
{“约翰”、“21”、“柏林”、“德国”},
{“史密斯”、“33”、“伦敦”、“英国”},
{“莱德”、“15”、“悉尼”、“澳大利亚”},
{“杰克”、“18”、“东京”、“日本”},
{“汤姆”、“34”、“孟买”、“印度”}
};
DataTable dt=新的DataTable();
Add(“Name”,Type.GetType(“System.String”);
Add(“Age”,Type.GetType(“System.String”);
Add(“City”,Type.GetType(“System.String”);
Add(“Country”,Type.GetType(“System.String”);
对于(int i=0;i<5;i++)
{
dt.Rows.Add();
dt.Rows[dt.Rows.Count-1][“Name”]=arrMultiD[i,0];
dt.Rows[dt.Rows.Count-1][“Age”]=arrMultiD[i,1];
dt.Rows[dt.Rows.Count-1][“城市”]=arrMultiD[i,2];
dt.Rows[dt.Rows.Count-1][“国家”]=arrMultiD[i,3];
}
GridView1.DataSource=dt;
GridView1.DataBind();
}
将代码放在page.ispostbak块中尝试以下方法:
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
// Access the CheckBox
CheckBox cb = (CheckBox)row.FindControl("CheckBox1");
if (cb.Checked)
{
Label1.Text += "1";
}
}
}
尝试此方法,在绑定网格之前,必须检查页面加载时是否有IsPostBack
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string[,] arrMultiD = {
{ "John", "21", "Berlin", "Germany" },
{ "Smith", "33" ,"London", "UK"},
{ "Ryder", "15" ,"Sydney", "Australia"},
{ "Jake", "18", "Tokyo", "Japan"},
{ "Tom","34" , "Mumbai", "India"}
};
DataTable dt = new DataTable();
dt.Columns.Add("Name", Type.GetType("System.String"));
dt.Columns.Add("Age", Type.GetType("System.String"));
dt.Columns.Add("City", Type.GetType("System.String"));
dt.Columns.Add("Country", Type.GetType("System.String"));
for (int i = 0; i < 5; i++)
{
dt.Rows.Add();
dt.Rows[dt.Rows.Count - 1]["Name"] = arrMultiD[i, 0];
dt.Rows[dt.Rows.Count - 1]["Age"] = arrMultiD[i, 1];
dt.Rows[dt.Rows.Count - 1]["City"] = arrMultiD[i, 2];
dt.Rows[dt.Rows.Count - 1]["Country"] = arrMultiD[i, 3];
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
受保护的无效页面加载(对象发送方,事件参数e)
{
如果(!IsPostBack)
{
字符串[,]arrMultiD={
{“约翰”、“21”、“柏林”、“德国”},
{“史密斯”、“33”、“伦敦”、“英国”},
{“Ryder”、“15”、“Sydney”、“Australia”},
{“杰克”、“18”、“东京”、“日本”},
{“汤姆”、“34”、“孟买”、“印度”}
};
DataTable dt=新的DataTable();
Add(“Name”,Type.GetType(“System.String”);
Add(“Age”,Type.GetType(“System.String”);
Add(“City”,Type.GetType(“System.String”);
Add(“Country”,Type.GetType(“System.String”);
对于(int i=0;i<5;i++)
{
dt.Rows.Add();
dt.Rows[dt.Rows.Count-1][“Name”]=arrMultiD[i,0];
dt.Rows[dt.Rows.Count-1][“Age”]=arrMultiD[i,1];
dt.Rows[dt.Rows.Count-1][“城市”]=arrMultiD[i,2];
dt.Rows[dt.Rows.Count-1][“国家”]=arrMultiD[i,3];
}
GridView1.DataSource=dt;
GridView1.DataBind();
}
}
如果进入按钮1,您是否在调试模式下进行了检查?单击按钮中的if键,是否在其中?是的,我在调试模式下进行了检查。它不进入if部分。当我单击按钮时,就好像Gridview中没有数据一样。Amit Tiwari,它不工作:(您可以输入gridview的.aspx代码和按钮吗?您可以为gridview发布html吗?现在检查解决方案您尚未实现回发条件,因此在按钮上单击它重新加载页面并清除复选框。。。
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string[,] arrMultiD = {
{ "John", "21", "Berlin", "Germany" },
{ "Smith", "33" ,"London", "UK"},
{ "Ryder", "15" ,"Sydney", "Australia"},
{ "Jake", "18", "Tokyo", "Japan"},
{ "Tom","34" , "Mumbai", "India"}
};
DataTable dt = new DataTable();
dt.Columns.Add("Name", Type.GetType("System.String"));
dt.Columns.Add("Age", Type.GetType("System.String"));
dt.Columns.Add("City", Type.GetType("System.String"));
dt.Columns.Add("Country", Type.GetType("System.String"));
for (int i = 0; i < 5; i++)
{
dt.Rows.Add();
dt.Rows[dt.Rows.Count - 1]["Name"] = arrMultiD[i, 0];
dt.Rows[dt.Rows.Count - 1]["Age"] = arrMultiD[i, 1];
dt.Rows[dt.Rows.Count - 1]["City"] = arrMultiD[i, 2];
dt.Rows[dt.Rows.Count - 1]["Country"] = arrMultiD[i, 3];
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
}