【问题标题】:Cannot get values from Gridview when binding it without SQLDatasource?在没有 SQLDatasource 的情况下绑定 Gridview 时无法从 Gridview 获取值?
【发布时间】:2014-02-28 09:35:10
【问题描述】:

我在 GridView1 的 Page_load 方法中绑定了一些数据。我有一个复选框 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>

【问题讨论】:

  • 如果你进入Button1_Click和里面的if,你有没有检查调试模式?
  • 是的,我使用调试模式进行了检查。它不进入 if 部分。当我单击按钮时,好像 Gridview 里面没有数据。
  • Amit Tiwari,它不起作用。 :(
  • 能不能把gridview和button的.aspx代码放上去
  • 你可以为你的 GridView 发布 html 吗?

标签: c# asp.net gridview


【解决方案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";
        }
    }
}

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:
          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 块中

      【讨论】:

      • 现在检查您尚未实现回发条件的解决方案,因此在按钮上单击它重新加载页面并清除复选框...
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-08
      相关资源
      最近更新 更多