RE: Checkbox in datagrid....
- From: "scsharma" <sharmasu@xxxxxxxxxxxxxx>
- Date: Tue, 14 Feb 2006 06:52:30 -0800
Thanks for description but putting populateDataGrid code in Page_init won't
help me. Because i am acutally populating my datagrid when some property is
set on my user control that contains that datagrid. To elaborate on that, i
have a form that has supplier information. Now based on the supplier i am
showing all the prices(displayed in datagrid) applicable for that supplier
and all the products(again displayed in different datagrid) that supplier
provides. Prices tied to products are always disabled and are
checked/unchecked based on the products selected in products datagrid.
Please help.
--
Thanks
SCS
"Phillip Williams" wrote:
When you disable a control, you discard its state. I placed your code on my.
website and added to it a button that change the state of the checkboxes and
disable them. Upon postback the checkboxes will lose their state. Try it:
http://www.webswapp.com/codesamples/aspnet20/StateOfDisabledCheckBox.aspx
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"scsharma" wrote:
Thanks for the reply but i am wondering if thats true then why does same code
works when the check boxes are not disabled?I am confused.
--
Thanks
SCS
"Phillip Williams" wrote:
Because you populate your datagrid dynamically during the Page Load event,
you should not expect the dynamically created checkbox to behave as expected.
Try moving the populateDataGrid step to the page_init.
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"scsharma" wrote:
Hi,
I have a datagrid which has one of the columns as checkbox. In this column,
based on certain condition some of the rows will have the checkbox enabled or
disabled. Now based on user action on the webform i am checking or unchecking
the checkboxes. I am running into strange problem where check boxes which are
disabled return me incorrect values even though on UI they have correct
value,that means, that even though, thru user action, the checkbox is
checked, when i retrieve the value of this checkbox in my code i am getting
the value of this checkbox as unchecked. If i don't set the state of checkbox
as disabled then same code returns me correct value. In order to test if its
the disabled state of checkbox which is causing the problem, i placed a
simple checkbox control on webform and set its state as disabled. On the
webform i placed a command button and on the click event of that command
button, i checked the "Checked" state of this disabled checkbox from
unchecked to checked. When i retrieved the value of this check box in my
code, the value returned is correct. I am wondering if checkbox inside the
grid is causing the problem.Please let me know. I am sending the sample code
in this message.
private void Page_Load(object sender, System.EventArgs e)
{
populateDataGrid();
createDummyUsers();
// if (!IsPostBack)
//// createNavBar();
}
private void populateDataGrid()
{
dgExistingUsers.Columns.Clear();
BoundColumn boundColumn = new BoundColumn();
boundColumn.HeaderText ="User ID"; //0
boundColumn.DataField ="UserID";
boundColumn.SortExpression ="UserID";
dgExistingUsers.Columns.Add(boundColumn);
boundColumn = new BoundColumn();
boundColumn.HeaderText ="First Name"; //1
boundColumn.DataField ="FirstName";
boundColumn.SortExpression ="FirstName";
dgExistingUsers.Columns.Add(boundColumn);
TemplateColumn checkboxColumn1 = new TemplateColumn(); //7
checkboxColumn1.HeaderText = "Include";
checkboxColumn1.ItemTemplate = new checkboxColumn();
dgExistingUsers.Columns.Add(checkboxColumn1);
dgExistingUsers.AutoGenerateColumns =false;
}
private void createDummyUsers()
{
DataSet ds = new DataSet();
ds.Tables.Add();
ds.Tables[0].Columns.Add("UserID",Type.GetType( "System.String"));
ds.Tables[0].Columns.Add("FirstName",Type.GetType( "System.String"));
DataRow dr = ds.Tables[0].NewRow();
dr["UserID"]="sharmasu";
dr["UserID"]="Subhash";
ds.Tables[0].Rows.Add(dr);
dr = ds.Tables[0].NewRow();
dr["UserID"]="Rajsharma";
dr["UserID"]="Raj";
ds.Tables[0].Rows.Add(dr);
dgExistingUsers.DataSource = new DataView(ds.Tables[0]);
dgExistingUsers.DataBind();
}
private void Button1_Click(object sender, System.EventArgs e)
{
string UserName = string.Empty;
foreach (DataGridItem item in dgExistingUsers.Items)
{
CheckBox cBox = (CheckBox) item.FindControl("IncludeCheckBox");
if (cBox.Checked )
UserName += item.Cells[0].Text;
}
this.TextBox1.Text = UserName;
if (this.CheckBox1.Checked )
this.TextBox1.Text +="Yoiu have checked it";
}
private void dgExistingUsers_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
CheckBox cbox = e.Item.FindControl("IncludeCheckBox") as CheckBox;
// if (cbox!= null)
// cbox.Enabled = false;
}
private void Button2_Click(object sender, System.EventArgs e)
{
foreach (DataGridItem ditem in dgExistingUsers.Items)
{
if (ditem.Cells[0].Text == "Subhash")
{
CheckBox cbox = ditem.FindControl("IncludeCheckBox") as CheckBox;
if (cbox!=null)
cbox.Checked = true;
}
}
CheckBox1.Checked = true;
}
public class checkboxColumn : ITemplate
{
public checkboxColumn()
{
}
public void InstantiateIn(Control container)
{
CheckBox checkbox = new CheckBox();
checkbox.ID = "IncludeCheckBox";
container.Controls.Add(checkbox);
}
}
--
Thanks
SCS
- References:
- Checkbox in datagrid....
- From: scsharma
- RE: Checkbox in datagrid....
- From: Phillip Williams
- RE: Checkbox in datagrid....
- From: scsharma
- RE: Checkbox in datagrid....
- From: Phillip Williams
- Checkbox in datagrid....
- Prev by Date: Re: CustomValidator is not firing
- Next by Date: RE: CustomValidator is not firing
- Previous by thread: RE: Checkbox in datagrid....
- Next by thread: Checkbox state after postback
- Index(es):