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

时间:2014-02-28 09:35:10

标签: c# asp.net gridview

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

我的Page_load方法

 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>

3 个答案:

答案 0 :(得分:1)

试试这个:

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";
        }
    }
}

答案 1 :(得分:0)

    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块

答案 2 :(得分:0)

试试这个,你必须在绑定网格之前在页面加载时检查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();
    }
}