how to read the dynamically created check box checked values in gridview

Posted by Raajkumar under ASP.NET on 10/15/2013 | Points: 10 | Views : 3241 | Status : [Member] | Replies : 2
Hi,
here is my code to create the checkboxes in gridview dynamially. Now i want to read the checked items from every row by clicking on link button created dynamically in the last column of every row........


code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class timetable : System.Web.UI.Page
{
DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{

dt.Columns.Add("Classes");
foreach (ListItem li in chklst_subjs.Items)
{
if (li.Selected)
{
dt.Columns.Add(li.Text);
}
}
dt.Columns.Add("Select Teachers");
int n = Convert.ToInt32(tb_clrms.Text);
for (int i = 1; i <= n; i++)
{
dt.Rows.Add(i);
}

GridView1.DataSource = dt;
GridView1.DataBind();

}
int rows = 0;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

int rowscount = Convert.ToInt32(GridView1.Rows.Count);
int colscount = e.Row.Cells.Count;
int col = Convert.ToInt32(dt.Columns.Count)-1;

if (rowscount != 0)
{
foreach (DataColumn dc in dt.Columns)
{
col = col - 1;
if (col > 0)
{
CheckBox chk = new CheckBox();
chk.ID = "chk" + rows;
GridView1.Rows[rows].Cells[col].Controls.Add(chk);
LinkButton lb = new LinkButton();
lb.ID = "lb" + rows;
int n=Convert.ToInt32(dt.Columns.Count) - 1;
GridView1.Rows[rows].Cells[n].Controls.Add(lb);
}
}
rows++;
}

}

}




Responses

Posted by: Allemahesh on: 10/16/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
You can see the below link:-

http://stackoverflow.com/questions/18662320/adding-check-box-dynamically-to-gridview
http://stackoverflow.com/questions/18886978/gridview-checkbox-select-is-taking-last-value-of-column-in-asp-net
http://stackoverflow.com/questions/18785599/finding-dynamically-added-checkbox-inside-gridview
http://www.c-sharpcorner.com/Forums/Thread/217846/how-to-get-checked-value-of-dynamic-checkbox-inside-gridview.aspx

Happy Coding
If it helps you or directs U towards the solution, MARK IT AS ANSWER



Raajkumar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nismeh on: 10/16/2013 [Member] Starter | Points: 25

Up
0
Down
You have to simiply use findcontrol

Label lbl = (Label)grdRow.FindControl("lalName");
You can use any control in place of label

IT KNOWLEDGE IS APPLIED KNOWLEDGE
So Just Do It

Raajkumar, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response