Strange problem of Dynamic Checkbox

S

savvy

I'm comparing two strings and trying to check a dynamic checkbox when
its true on pageload , but i dont know why i'm not able to do that on
page load, when i click some other links and come back to this page
then the required checkbox will be checked. I'm not able to figure out
where is the problem. Sorry about the long code
Any help will be greatly appreciated

private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
get_servc();
}
}


override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
if (Page.IsPostBack)
{
Session.Timeout=120;
_checkboxes = (ArrayList) Session["CheckBoxArray"];
}
else
{
_checkboxes = new ArrayList();
get_servc();
}

foreach(CheckBox c1 in _checkboxes)
Panel1.Controls.Add(c1);
}


public void get_servc()
{
strcatgy = Convert.ToString(Session["Category"]);
string temp = strcatgy;
int xPos = 20;
int yPos= 335;
int count=0;
objcmd = new SqlCommand("SelectDynControls", objConn);
objcmd.CommandType = CommandType.StoredProcedure;
objConn.Open();
objRdr=objcmd.ExecuteReader();
while(objRdr.Read())
{
CheckBox c1 = new CheckBox();
c1.ID="chk" + _checkboxes.Count.ToString();
c1.AutoPostBack=true;
c1.Visible=true;
c1.Text = Convert.ToString(objRdr["CbName"]);


if(c1.Text == temp)
{
c1.Checked=true; // this is the part which is not working
}

c1.Style.Add("DISPLAY", "BLOCK");
if((count % 6)==0)
{
yPos +=30;
xPos =20;
}
c1.Style.Add("POSITION", "absolute");
c1.Style.Add("TOP", Convert.ToString(yPos));
c1.Style.Add("LEFT", Convert.ToString(xPos));
_checkboxes.Add(c1);
Panel1.Controls.Add(c1);
xPos += 120;
count += 1;
}
objRdr.Close();
objConn.Close();
yPos = yPos + 50;
xPos = 275;
}
 
S

savvy

Actually the code had no problem
As I was creating the dynamic controls prior to page load. we have to
call method get_servc() again as shown in my code below
and now its working
hope this will be of some help to the people who stuck with the same
problem


private void dl_Edit(object source,
System.Web.UI.WebControls.DataListCommandEventArgs e)
{
strcatgy =((Label)e.Item.FindControl("lblcategory")).Text;
Session["Category"]= strcatgy;
get_servc();
............
...........
............
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top