在没有SQLDatasource的情况下绑定Gridview时无法从中获取值



我正在GridView1中的Page_load方法中绑定一些Data。我有一个复选框CheckBox1。选中复选框后,我想在按下按钮时在Label1中显示一些内容(作为测试)。但它并没有表现出来。我不是用SQLDatasource绑定数据,而是手动绑定它。

我的页面加载方法

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();
}

我的按钮点击功能

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 != null && cb.Checked)
{
Label1.Text += "1";
}
}
}

我已在调试模式下进行了检查。当我点击按钮时,if部分没有被执行。就好像我一点击按钮,GridView中的数据就丢失了。换句话说,GridView在离开Page_Load方法时丢失了数据

我的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>

试试这个:

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();
}
}
if (!Page.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();
}

将您的代码放在page.ispostbak块中

最新更新