I am creating a tree programmatically inside a row in a table.
Works fine, but can not get the assigned event is called:
TreeView arbolCapas = new TreeView();
arbolCapas.ID = "capas";
foreach (String capa in servicio.Capas)
{
TreeNode childNodes = new TreeNode();
childNodes.Text = capa;
childNodes.ShowCheckBox = true;
childNodes.SelectAction = TreeNodeSelectAction.None;
arbolCapas.Nodes.Add(childNodes);
}
arbolCapas.SelectedNodeChanged +=new EventHandler(arbolCapas_TreeNodeCheckChanged);
tbC.Controls.Add(arbolCapas);
tbR.Cells.Add(tbC);
protected void arbolCapas_TreeNodeCheckChanged(Object sender, EventArgs e)
{
TreeView elemento = (TreeView)(((CheckBox)sender).Parent);
foreach (TreeNode node in elemento.CheckedNodes)
{ //if (node.Checked)
}
}
How I can call an event when the checkbox of a child node is checked?
Thanks a lot.
Kindly change
childNodes.SelectAction = TreeNodeSelectAction.None
to
childNodes.SelectAction = TreeNodeSelectAction.Select;
I found the solution by adding the event as follows:
arbolCapas.Attributes.Add("onclick", "OnCheckBoxCheckChanged(event)");
And then, in javascript:
function OnCheckBoxCheckChanged(evt) {
alert("check change");
}
Here the solution:
http://geekswithblogs.net/ranganh/archive/2009/01/21/updated-asp.net-treeview-checkboxes-ndash-check-all-ndash-javascript.aspx
Related
A basic task but unable to achieve.
The data template is like this:
Comment --(template ID is {AB86861A-6030-46C5-B394-E8F99E8B87DB})
-Comment --section
-Author --field
-Comment Text --field
I am creating a new item of that template as follows
protected void btnSubmit_Click(object sender, EventArgs e)
{
Sitecore.Data.Database masterDb = Sitecore.Configuration.Factory.GetDatabase("master");
Item parentItem = Sitecore.Context.Item;
var template = masterDb.GetTemplate("{AB86861A-6030-46C5-B394-E8F99E8B87DB}");
using (new Sitecore.SecurityModel.SecurityDisabler())
{
Item newItem = parentItem.Add(Sitecore.DateUtil.IsoNow, template);
try
{
if (newItem != null)
{
newItem.Editing.BeginEdit();
newItem["Author"] = txtAuthor.Text;
newItem["Comment Text"] = txtContent.Text;
newItem.Editing.EndEdit();
}
}
catch
{
//...log error
newItem.Editing.CancelEdit();
}
}
txtAuthor.Text = string.Empty;
txtContent.Text = string.Empty;
}
The item gets saved with the timestamp as name, as I see in the content tree. Now when I try to see the values (Author, comment text), they are empty. So, I doubt , if they were saved in the first place.
Code to see comments, using another Usercontrol.
private void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Database masterDb = Factory.GetDatabase("master");
Item parentItem = Sitecore.Context.Item;
var commentTemplate = masterDb.GetTemplate("{AB86861A-6030-46C5-B394-E8F99E8B87DB}");
var commentsList = parentItem.Children.Where(x => x.TemplateID == commentTemplate.ID);
Item item = commentsList.FirstOrDefault();
Response.Write(item["Author"] + "$$" + item["Comment Text"]);
}
}
The result is only
$$
What am i missing here.
UPDATE:
The mistake was with the ID being referenced. I was using the Template ID of the 'Comment' data template. It has to be its ID.
I have a project that using dropdownlist for choices.When check checkbox1 dropdown automatically bind data from database using table1 and when I check checkbox2 dropdown automatically binding data from database using table2.I do not want to use get data by using any button .How can I do that .Please help me.
here is code by using button:
public void LokasyonDoldur()
{
birimBUS = new BirimBUSV1();
List<BirimVO> birimVO = new List<BirimVO>();
DrpChcs.Items.Clear();
List<ListItem> items = new List<ListItem>();
birimVO = birimBUS.LokasyonlariGetir();
foreach (var item in birimVO)
{
items.Add(new ListItem(item.BirimAdi, item.ID.ToString()));
}
DrpChcs.Items.AddRange(items.ToArray());
}
public void BirimleriDoldur()
{
PoliklinikBUS poliklinikBUS = new PoliklinikBUS();
List<PoliklinikVO> poliklinikVO = new List<PoliklinikVO>();
DrpChcs.Items.Clear();
List<ListItem> items = new List<ListItem>();
poliklinikVO = poliklinikBUS.Poliklinikler();
foreach (var item in poliklinikVO)
{
items.Add(new ListItem(item.PoliklinikAdi, item.ID.ToString()));
}
DrpChcs.Items.AddRange(items.ToArray());
}
protected void BtnLokasyon_Click(object sender, EventArgs e)
{
if (ChckLctn.Checked == true && ChckBrm.Checked==false)
{
LokasyonDoldur();
}
else if (ChckLctn.Checked == false && ChckBrm.Checked == true)
{
BirimleriDoldur();
}
else
{
}
Button1.Visible = true;
BtnLokasyon.Visible = false;
}
protected void DrpChcs_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
KirilimId = Int32.Parse(DrpChcs.SelectedValue);
BPolikilinikID= KirilimId;
}
but I do not want to use this one.
ohh its another language. its hard to read. but what you basicly have to do is check which checkbox is checked in the page load and then load the dropdown based on what is loaded.
something like this. (I have typed it from my head so its not like copy-paste but you get the idea)
page_load
{
if(checkbox1.checked)
{
dropdown.dataitems = items1;
dropdown.databind();
return;
}
if(checkbox2.checked)
{
dropdown.dataitems = items2;
dropdown.databind();
return;
}
}
YOu can call the Button1_click event from the Dropdown list selected index changed event like this
Button1_Click(Button1,new EventArgs());
and in this you can hide that button from the page and in code behind you are calling the same function
OR
You can refactor the code in a seperate function from the button click event and call that function in the selected index changed event.
Please let me knwo if I misunderstood your question
Thanks
i have created ListBox's Dynamically in a panel and i want to read the selected item from the Listbox created dynamically . below is the code that i used to create the Dynamic Listbox. can anyone please help me how to get the dynamically created listbox and then read the item selected. 'protected void GotoReport_Click(object sender, ImageClickEventArgs e)
{
foreach (TreeNode tndim in tvCubedef.CheckedNodes)
{
lbFilter.Items.Add(tndim.Text);
}
foreach (ListItem item in lbFilter.Items)
{
item.Selected = true;
}
panFilter.Controls.Clear();
connstr2 = System.Configuration.ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
conn2.ConnectionString = connstr2;
conn2.Open();
CubeCollection CubeList = conn2.Cubes;
string cb = ddlCubeList.SelectedItem.Text;
foreach (ListItem li in lbFilter.Items)
{
ListBox listb = new ListBox();
ListItem Memlist = new ListItem();
listb.SelectionMode = System.Web.UI.WebControls.ListSelectionMode.Multiple;
listb.Height = 150;
listb.Width = 250;
string Repl1 = li.Value.Replace("[", "");
string Repl2 = Repl1.Replace("]", "");
string[] DimMember = Repl2.Split('.');
foreach (Member dimem in CubeList[cb].Dimensions[DimMember[0]].Hierarchies[DimMember[1]].Levels[DimMember[2]].GetMembers())
{
Memlist.Text = dimem.Name;
listb.Items.Add(Memlist);
panFilter.Controls.Add(listb);
}
}
} '
You need to add the event-handler dynamically:
listb.SelectedIndexChanged += new EventHandler(listb_SelectedIndexChanged);
Of course you also need to provide this method:
protected void listb_SelectedIndexChanged(Object sender, EventArgs e)
{
ListBox listb = (ListBox) sender;
}
Are you recreating this ListBox on every postback (as you should) in page_load at the latest and with same ID as before?
I'm using ASP.NET and C#. I'm programmtically creating a checkboxlist. When I check an item, the SelectedIndexChanged event is firing. But, when I uncheck the item, the event is not fired. I'm binding the items on every postback and autopostback is set to true. Where am I going wrong? Here's the code -
page_load
{
var cblUser = new CheckBoxList();
cblUser.AutoPostBack = true;
cblUser.SelectedIndexChanged += cblUser_SelectedIndexChanged;
var list = DAL.GetUsers();
foreach (var user in list)
{
cblUser.Items.Add(new ListItem(user.Name, user.Id));
}
}
Thank you.
Update #1: Actual code -
public partial class CategoriesAccordion : UserControl
{
public List<Community> AllCommunities
{
get
{
if (Session["AllCommunities"] == null)
{
var db = new CommunityGuideDB();
Session["AllCommunities"] = db.Communities.OrderBy(x => x.Name).ToList();
}
return (List<Community>) Session["AllCommunities"];
}
}
public List<Category> Categories
{
get
{
if (Session["Categories"] == null)
{
var db = new CommunityGuideDB();
Session["Categories"] = db.Categories.OrderBy(x => x.Name).ToList();
}
return (List<Category>) Session["Categories"];
}
}
public event EventHandler Categories_Selected = delegate { };
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) Session.Remove("Categories");
LoadCategories();
}
private void LoadCategories()
{
foreach (var parent in Categories.Where(item => item.ParentId == null && item.ShowAsPivot == true).OrderBy(x => x.DisplayOrder))
{
var pane = new AccordionPane {ID = parent.Name};
pane.HeaderContainer.Controls.Add(new LiteralControl(parent.Name));
var cblValues = new CheckBoxList();
cblValues.AutoPostBack = true;
cblValues.SelectedIndexChanged += cblValues_SelectedIndexChanged;
foreach (var child in Categories.Where(child => child.ParentId == parent.Id))
{
var communityCount = child.CommunityCategory.Where(x => x.Categories_Id == child.Id).Count();
cblValues.Items.Add(new ListItem(string.Format("{0} ({1})", child.Name, communityCount), child.Id.ToString()));
}
pane.ContentContainer.Controls.Add(cblValues);
acdFilters.Panes.Add(pane);
}
}
protected void cblValues_SelectedIndexChanged(object sender, EventArgs e)
{
var cblValues = ((CheckBoxList) sender);
var selectedCategories = (from ListItem item in cblValues.Items where item.Selected select Categories.Find(c => c.Id == new Guid(item.Value))).ToList();
Categories_Selected(this, new CommandEventArgs("SelectedCategories", selectedCategories));
}
}
I don't get how do you add the control to a container?
I've just checked and I've got the event fired both on checking & unchecking.
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
CheckBoxList cbList = new CheckBoxList();
cbList.AutoPostBack = true;
for (int i = 0; i < 10; i++)
cbList.Items.Add(i.ToString());
cbList.SelectedIndexChanged += new EventHandler(cbList_SelectedIndexChanged);
form1.Controls.Add(cbList);
}
void cbList_SelectedIndexChanged(object sender, EventArgs e)
{
//fires both on check & uncheck of an item
}
}
The SelectedIndexChanged event you are bounding is fired upon selecting different item on your list, not when you check an item. CheckBoxList does not have an event for changing the status of its items.
Try a to use list control like Repeater ...
I generate my check boxes dynamically:
for (int i = 0; i < dtCommon.Count; i++)
{
CheckBox newBox = new CheckBox();
newBox.Text = dtCommon[i].userName;
newBox.CssClass = "cbox";
if (dtCommon[i].isAlreadyRequired > 0 )
{
newBox.CssClass = "cbox highlighted";
newBox.Checked = true;
}
ApprovalSelectPanel.Controls.Add(newBox);
}
And when the save button is pressed I call this function:
protected void SaveUsers(object sender, EventArgs e)
{
}
How do I know which check boxes the user has checked?!
You could loop through the ApprovalSelectPanel.Controls and cast them back to the corresponding CheckBox type and verify the Checked property.
I think it is better to use CheckBoxList Within the ApprovalSelectPanel instead of add it in the runtime and in the runtime do the following
CheckBoxList1.DataSource = dtCommon;
CheckBoxList1.DataMember = "userName";
CheckBoxList1.DataBind();
To know which one is selected do the following
foreach(ListItem item in CheckBoxList1.Items)
if (item.Selected)
{
//Do any action
}