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

WEB APP Datalist Checkbox in C#

P: 5
I create a datalist from a datatable that is created in code on the page. I have all of my values assigned to dropdowns and labels inside the DL and it runs through them.

Everything works great

BUT

I have added a checkbox to Datalist and now want to set visable= false to the items in the datalist of the checkbox is check (on post back of course).. But I cannot find where I should put my code for this. in what function? selectedindex, itemcreated etc... None will work.. I think I am putting it in the wrong place so when it posts back it make the changes to the datalist

Expand|Select|Wrap|Line Numbers
  1. protected void dlSpanDays_ItemCommand(object source, DataListCommandEventArgs e)
  2.     {
  3.         CheckBox worked = (CheckBox)e.Item.FindControl("chkWork");
  4.  
  5.         Label lblDayer = (Label)e.Item.FindControl("lblDateDayName");
  6.  
  7.         if (worked.Checked == false)
  8.         {
  9.             lblDayer.Visible = false;
  10.         }
  11.  
  12.     }
Any help would be greatly appreciated!!

Thanks
Ryan
Nov 29 '07 #1
Share this Question
Share on Google+
1 Reply


P: 5
I found the easiest answer I think. If you come up with a better one please let me know.

On a datalist it is a bit different than a GridView. Even on ItemCeated with checkboxes.

if you set a condition like in the first post above the checkbox will always be the default setting of the checkbox, it does not get a new setting when you check it and postback and reload. It is static and always remains the same from my experience. There where tones of info using a dataset and storing the true/false info and rebinding the datatable. Instead of doing that I found that if you do the following on the CheckedChanged property of the checkbox, you can modify any control within the same row of the datalist. I just wanted to enable disable controls insidea datalist row, noting major but this could be expanded to do other things I am sure.

Expand|Select|Wrap|Line Numbers
  1. protected void chkWork_CheckedChanged(object sender, EventArgs e)
  2.     {
  3.  
  4.         CheckBox worked = (CheckBox)sender;
  5.         Label lblWorkOn1 = (Label)worked.NamingContainer.FindControl("lblWorkOn");
  6.         DropDownList ddlspanhourstart1 = (DropDownList)
  7.  
  8.         if (worked.Checked == true)
  9.         {
  10.             ddlspanhourstart1.Enabled = true;
  11.         }
  12.         else
  13.         {
  14.             ddlspanhourstart1.Enabled = false;
  15.         }
  16.  
  17.     }
When doing it this way it seems to me that you save server and client resources depending on what you want to do. It is at least better than loading all of the datasets into vewstate or session and running through all of them again.

I got the base info here and modified it for datalist instead of Gridview The gridview info is here

http://geekswithblogs.net/azamsharp/.../10/65433.aspx

Just my 2 cents..

Ryan
Nov 30 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.