以ä¸ãå¿ããªãå
ã«ã¡ã¢ã¡ã¢ã
ASP.NETã®TreeViewã«ã¯ãSelectedNodeChangedã¤ãã³ããããã¾ãã
ãã®ã¤ãã³ãã¯ãã®ååã®éããé¸æãã¼ããå¤æ´ãããéã«ã³ã¼ã«ããã¯ããã¾ãã
ãã§ããã®ã¤ãã³ããå½ç¶ãªã®ã§ããåããã¼ãã2å以ä¸é£ç¶ã§é¸æããå ´åã¯
çºçãã¾ãããããã«ããã¹ã¯ãããã¢ããªã®ããªã¼ã³ã³ããã¼ã«ã¨éã£ã¦
ASP.NETã®TreeViewã³ã³ããã¼ã«ã«ã¯Clickã¤ãã³ãã¿ãããªã®ãããã¾ããã
(ä¾ï¼ åæ表示âãã¼ã(A)é¸æâSelectedNodeChangedçºçâãã¼ã(A)é¸æâã¤ãã³ãçºçããã
äºæ
ã«ãããåããã¼ããé¸æãããå ´åã§ãã¤ãã³ããçºçãã¦ããããªãã¨å°ããã¨ã«ãªã£ã¦ããã®ã§
ãããã§æ
å ±ãæ¢ãããã以ä¸ã®ãã¼ã¸ã«è§£æ±ºæ¹æ³ãè¨è¿°ããã¦ãã¾ãããæè¬ã
- ASP.NET TreeView and Selecting the Selected Node
å
·ä½çã«ã¯ã以ä¸ã®ããã«Page_Loadã®é¨åã§
TreeView1.SelectedNode.Selected = false;
ã¨ä¸æ¦é¸æç¶æ ã解é¤ãããã¨ã§è§£æ±ºã§ãã¾ããã
ä¸è¨ã®URLã«ã¦å¥ã®æ¹ãåçãã¦ããããã«
SelectedNodeChangedã¤ãã³ããã³ãã©å ã«ã¦ãã¼ãã®Selected=falseã¨ãã
æ¹æ³ãå®è¡ããã¨ãæ¯åã¤ãã³ãã¯çºçããã®ã§ããé¸æè²ãæ¶ãã¦ãã¾ãã¾ãã
é¸æè²ãªã©ã®ç¶æ
ããã®ã¾ã¾ã§ããã¤ãæ¯ã¯ãªãã¯æã«SelectedNodeChangedã¤ãã³ãã
çºçãããã«ã¯ãPage_Loadã®ä¸ã¨ãªãã¾ãã
以ä¸ããµã³ãã«ã³ã¼ãã§ãã
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:TreeView ID="TreeView1" runat="server" ImageSet="Arrows" Width="222px" OnSelectedNodeChanged="TreeView1_SelectedNodeChanged"> <HoverNodeStyle Font-Underline="True" ForeColor="#5555DD" /> <Nodes> <asp:TreeNode Text="ã«ã¼ããã¼ã" Value="ã«ã¼ããã¼ã"> <asp:TreeNode Text="åãã¼ãï¼" Value="åãã¼ãï¼"> <asp:TreeNode Text="å«ãã¼ãï¼" Value="å«ãã¼ãï¼"></asp:TreeNode> </asp:TreeNode> <asp:TreeNode Text="åãã¼ãï¼" Value="åãã¼ãï¼"> <asp:TreeNode Text="å«ãã¼ãï¼" Value="å«ãã¼ãï¼"></asp:TreeNode> </asp:TreeNode> <asp:TreeNode Text="åãã¼ãï¼" Value="åãã¼ãï¼"> <asp:TreeNode Text="å«ãã¼ãï¼" Value="å«ãã¼ãï¼"></asp:TreeNode> </asp:TreeNode> </asp:TreeNode> </Nodes> <NodeStyle Font-Names="Tahoma" Font-Size="10pt" ForeColor="Black" HorizontalPadding="5px" NodeSpacing="0px" VerticalPadding="0px" /> <ParentNodeStyle Font-Bold="False" /> <SelectedNodeStyle Font-Underline="True" ForeColor="White" BackColor="Blue" HorizontalPadding="0px" VerticalPadding="0px" /> </asp:TreeView> </div> <asp:Panel ID="Panel1" runat="server"> <asp:Label ID="Label1" runat="server" Text="Count:"></asp:Label> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> </asp:Panel> </form> </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Web.UI; public partial class _Default : Page { TreeNode _selectedNode; boolean _isFiredNodeChangeEvent; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { TextBox1.Text = 0.ToString(); } else { // // ãã®ã¿ã¤ãã³ã°ã§ã以ä¸ã®ã³ã¼ããè¨å®ãã㨠// é¸æè²ãä¿æããã¾ã¾ããã¼ãã®ã¯ãªãã¯ã§ // åããã¼ãã®åé¸æã§ããã«ã¦ã³ããã¢ãããããã¨ãåºæ¥ãã // ï¼ã¤ã¾ããSelectedNodeChangedã¤ãã³ããçºçãããï¼ // if (TreeView1.SelectedNode != null) { TreeView1.SelectedNode.Selected = false; _selectedNode = TreeView1.SelectedNode; _isFiredNodeChangeEvent = true; } } } protected void Page_LoadComplete(object sender, EventArgs e) { if (!_isFiredNodeChangeEvent) { _selectedNode.Selected = true; } } protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e) { var countString = TextBox1.Text; if (string.IsNullOrWhiteSpace(countString)) { countString = 0.ToString(); } int count; if (!int.TryParse(countString, out count)) { count = -1; } TextBox1.Text = (++count).ToString(); // // ãã®ã¿ã¤ãã³ã°ã§ä»¥ä¸ã®ã³ã¼ããè¨å®ãã㨠// ãã¼ããã¯ãªãã¯ãããã³ã«ãã«ã¦ã³ããã¢ããããããã« // ãªããããã¼ãã®é¸æè²ãæ¶ãã¦ãã¾ãã // //TreeView1.SelectedNode.Selected = false; } }
ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼
éå»ã®è¨äºã«ã¤ãã¦ã¯ã以ä¸ã®ãã¼ã¸ãããåç
§ä¸ããã
- ããããåå¿é²æ¥è¨ã¾ã¨ã