You must Sign In to post a response.
  • Category: ASP.NET

    How to get checkbox inside a gridview in webmethod in asp.net

    I am calling a code behind method from javascript button click function.In code Behind i have made the method as static web method.So now i want to retrieve the checkbox control which is inside grid view.But i am not able to do so.
  • #768579
    Try this code snippet to get checkbox inside a gridview
    public partial class _Default : System.Web.UI.Page  
    {
    SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
    protected void Page_Load(object sender, EventArgs e)
    {
    if (!Page.IsPostBack)
    {
    refreshdata();
    }
    }
    public void refreshdata()
    {
    SqlCommand cmd = new SqlCommand("select * from tbl_data", con);
    SqlDataAdapter sda = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    sda.Fill(dt);
    GridView1.DataSource = dt;
    GridView1.DataBind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
    foreach (GridViewRow gvrow in GridView1.Rows)
    {
    var checkbox = gvrow.FindControl("CheckBox1") as CheckBox;
    if (checkbox.Checked)
    {
    var lblID = gvrow.FindControl("Label1") as Label;
    var lblName = gvrow.FindControl("Label2") as Label;
    var lblCity = gvrow.FindControl("Label3") as Label;
    SqlCommand cmd = new SqlCommand("insert into tbl_save (id,name,city) values (@id,@name,@city)", con);
    cmd.Parameters.AddWithValue("id", lblID.Text);
    cmd.Parameters.AddWithValue("name", lblName.Text);
    cmd.Parameters.AddWithValue("city", lblCity.Text);
    con.Open();
    int i = cmd.ExecuteNonQuery();
    con.Close();
    refreshdata();
    }
    }
    }
    }


  • Sign In to post your comments