【问题标题】:Couldn't get the values from the cells in GridView using check box in ASP.net无法使用 ASP.net 中的复选框从 GridView 中的单元格中获取值
【发布时间】:2011-06-09 09:02:24
【问题描述】:
protected void Page_Load(object sender, EventArgs e)
{
   SqlConnection conn = new SqlConnection("Server=ILLUMINATI;" + "Database=DB;Integrated Security= true");
   SqlCommand comm = new SqlCommand("Select * from FileUpload where UploadedBy='"+NAME+"'",conn);

   try
   {
      conn.Open();
      SqlDataReader rdr = comm.ExecuteReader();
      if (s.Equals("admin"))
      {
         GridView1.DataSource = rdr;
         GridView1.DataBind();
       }
       rdr.Close();
    }
    catch
    {
      conn.Close();
    }
}

我有一个这样的按钮。
但我无法获取单元格的值(标签未附加)。

protected void Button1_Click(object sender, EventArgs e)
{
  string[] FID={};
  int j=0;

  foreach (GridViewRow di in GridView1.Rows)
  {
     HtmlInputCheckBox chkBx = (HtmlInputCheckBox)di.FindControl("CheckBox1");
     if ( chkBx != null && chkBx.Checked)
     {
        FID[j] += di.Cells[2].Text;
        j++;

        Label1.Text += di.Cells[2].Text;
        //Label lbl = (Label)di.FindControl("Id");
        //Response.Write(lbl.Text + "<br>");
      }
   }
}

【问题讨论】:

  • 请向我们展示您的完整代码,page_load 代码
  • ... 以及 GridView 的 aspx 标记。

标签: c# .net asp.net gridview


【解决方案1】:

将您的页面加载代码放在if(!IsPostBack){yourCode....} 下,这样当您单击按钮时,您的page load event 将在click handler 之前被调用,它会重新绑定gridview

protected void Page_Load(object sender, EventArgs e)
{

 if(!IsPostBack)
{
SqlConnection conn = new SqlConnection("Server=ILLUMINATI;" + "Database=DB;Integrated Security= true");
     SqlCommand comm = new SqlCommand("Select * from FileUpload where UploadedBy='"+NAME+"'",conn);

     try
     {
       conn.Open();
        SqlDataReader rdr = comm.ExecuteReader();
         if (s.Equals("admin"))
         {
             GridView1.DataSource = rdr;
             GridView1.DataBind();
             }
       rdr.Close();
      }
      catch
     {
         conn.Close();
     }
   }
}

【讨论】:

    【解决方案2】:

    我想如果你可以调试代码, 您可以轻松找到并解决您的问题。

    首先,检查您的循环是否真的在做。 其次,是不是 Cell[2] 真的是你想要获取的数据。(我怀疑)

    希望它有效!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多