Click here to Skip to main content
15,886,963 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I work on razor asp.net page model . i need to implement tree view with children's checkboxes then submit changes on every parent from selected checkboxes so are you have any sample on razor page asp.net

can you help me by provide me sample or attachement by csharp asp.net razor for tree view with children checkbox

What I have tried:

How to implement tree view with children checkbox with button submit
i need sample or link if possible
Posted
Updated 18-Jun-23 22:44pm
v2

If you are doing this for your own use and not for a large company, then you can use Modern cross-platform ASP.NET Core UI controls | Syncfusion[^] under their community license program: 1,800+ Free controls and frameworks for desktop, web, and mobile apps.[^]

Here is their TreeView control that will do what you want and more: ASP.NET Core TreeView | Advanced Tree List | Syncfusion[^]
 
Share this answer
 
v2
Here's a nice example of creating a tree view in pure HTML and CSS - you don't even need any JavaScript:
Tree views in CSS[^]

Adapting that to add checkboxes to each item is fairly simple:
HTML
<ul class="tree">
  <li>
    <details open>
      <summary><input type="checkbox" /> Giant planets</summary>
      <ul>
        <li>
          <details>
            <summary><input type="checkbox" /> Gas giants</summary>
            <ul>
              <li><input type="checkbox" name="planet" value="Jupiter" /> Jupiter</li>
              <li><input type="checkbox" name="planet" value="Saturn" /> Saturn</li>
            </ul>
          </details>
        </li>
        <li>
          <details>
            <summary><input type="checkbox" /> Ice giants</summary>
            <ul>
              <li><input type="checkbox" name="planet" value="Uranus" /> Uranus</li>
              <li><input type="checkbox" name="planet" value="Neptune" /> Neptune</li>
            </ul>
          </details>
        </li>
      </ul>
    </details>
  </li>
</ul>
JavaScript
document.addEventListener("click", e => {
	const { target } = e;
	if (!target.matches(".tree input[type='checkbox']")) { return; }
	
	if (target.matches("summary input")) {
		// Clicked on a header:
		const group = target.closest("details");
		group.querySelectorAll("input[type='checkbox']").forEach(el => el.checked = target.checked);
	}
	
	// Update the state of the headers:
	const tree = target.closest(".tree");
	tree.querySelectorAll("details").forEach(group => {
		const header = group.querySelector("summary input[type='checkbox']");
		header.checked = group.querySelector("li > input[type='checkbox']:not(:checked)") === null;
	});
});
Demo[^]

Only the checkboxes on the "leaf" nodes have names and values, so the submitted data will include the checked leaf nodes, not the header nodes. If that's not what you want, then you can add appropriate names and values to the header nodes.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900