Showing posts with label checkbox. Show all posts
Showing posts with label checkbox. Show all posts

Saturday, March 24, 2012

Strange event behavior with check boxes

I've designed a gridview that contains a template field with an
unbound checkbox, allowing the user to select certain rows for further
action.
I am capturing the CheckedChanged event in order to set the grid's
SelectedIndex equal to the row whose check box was checked or
unchecked. However, for some strange reason when I click a checkbox
the CheckedChanged event fires for every checked checkbox in the grid,
from top to bottom. Consequently, the SelectedIndex always ends up
being the last checked row.
Can anyone shed some light on this behavior?
-- HansOn Jul 26, 9:23 pm, Froefel <hansdeschry...@.gmail.com> wrote:
> I've designed a gridview that contains a template field with an
> unbound checkbox, allowing the user to select certain rows for further
> action.
> I am capturing the CheckedChanged event in order to set the grid's
> SelectedIndex equal to the row whose check box was checked or
> unchecked. However, for some strange reason when I click a checkbox
> the CheckedChanged event fires for every checked checkbox in the grid,
> from top to bottom. Consequently, the SelectedIndex always ends up
> being the last checked row.
> Can anyone shed some light on this behavior?
> -- Hans
Hi...
can you please provide some code sample...
Masudur

Tuesday, March 13, 2012

Strange problem of Dynamic Checkbox

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;
}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();
.........
.........
.........
}

Strange problem of Dynamic Checkbox

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;
}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();
.........
........
.........
}