开发者

Couldn't get the values from the cells in GridView using check box in ASP.net

protected void Page_Load(object sender, EventArgs e)
{
   SqlConnection conn = new SqlConnection("Server=ILLUMINATI;" + "Database=DB;Integrated Security= true");
   SqlCommand comm = new SqlCommand("Selec开发者_如何学运维t * 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();
    }
}

I have a button like this.

But I'm not able to get the values of the cells(Label is not getting appended).

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


Put your page load code under if(!IsPostBack){yourCode....} so when you click the button your page load event will be called before the click handler and it will rebind the 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();
     }
   }
}


i think if you can debug the code, you can find and solve your problem easily.

First, check is your loop really do. Second, is Cell[2] is really what you want to get data.(I doubt that)

Hope it works!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜