Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have populated a dynamic gridview from datatable and added a check box dynamically.I just need to check if the box checked on a outside button click..but when repopulating from datatable on button click the check box state is losing after postback..I have written code for creating check box at Pre_Init() event.so i can get the checkbocx .But the state is missing.If any one can help on this it will be much helpful.


Thanks in advance,
Jeffin
Posted
Updated 19-Jan-14 19:57pm
v2
Comments
Karthik_Mahalingam 20-Jan-14 1:51am    
can u post your code...
jeffin_ 20-Jan-14 4:33am    
Here is my code:

SqlDataAdapter adp2 = new SqlDataAdapter("select * FROM SOMETHING_MST", obj.con);
adp2.Fill(dt);
TemplateField lname = new TemplateField();
lname.HeaderText = "BANK NAME";
lname.ItemTemplate = new AddTemplateToGridView(ListItemType.Item, "NAME", dt2);
GridView1.Columns.Add(lname);

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

//CODE FOR DYNAMIC CHECKBOX //

public class AddTemplateToGridView : ITemplate
{
ListItemType _type;
string _colName;
DataTable dt2;
public AddTemplateToGridView(ListItemType type, string colname,DataTable dt)
{
_type = type;

_colName = colname;
dt2 = dt;

}
void ITemplate.InstantiateIn(System.Web.UI.Control container)
{

switch (_type)
{
case ListItemType.Item:

CheckBox ddl = new CheckBox();




// chk.DataBinding += new EventHandler(ht_DataBinding);
container.Controls.Add(ddl);

break;
}

}

}
i have got code to assign id to each check box but now everything ruined...
Karthik_Mahalingam 20-Jan-14 4:40am    
I am sorry to that, you cannot get the value after post back.
what you can do is , store the values in the hidden field in the client side( javascript ) and get the values on the server...
jeffin_ 3-Mar-14 5:56am    
finally...i got the solution in c# itself..thanks for suggesting this idea, Karthik..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900