By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,506 Members | 1,881 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,506 IT Pros & Developers. It's quick & easy.

Strange problem of Dynamic Checkbox

P: n/a
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;
}

Nov 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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();
............
...........
............
}

Nov 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.