The TreeView control included in ASP.Net 2.0 is lacking an option to cause a postback due to a user checking/unchecking a checkbox on a tree node.
I hacked together a little derived class to use in place of the treeview that postsback when the user clicks the checkbox, and also fires an event.
It would of course be useful to add a bit more code so that the resulting event could specify which node was changed, etc. But as of now you can just look at the “checkednodes” property of the tree.
1 |
<span class="kwrd">using</span> System;<br /><span class="kwrd">using</span> System.Text;<br /><span class="kwrd">using</span> System.Data;<br /><span class="kwrd">using</span> ASP = System.Web.UI.WebControls;<br /><span class="kwrd">using</span> System.Web.UI;<br /><span class="kwrd">using</span> System.IO;<br /><br /><span class="kwrd">namespace</span> myNameSpace<br />{<br /> <span class="kwrd">class</span> VerboseTreeView : ASP.TreeView, IPostBackEventHandler <br /> {<br /> <span class="kwrd">public</span> <span class="kwrd">event</span> EventHandler CheckClick;<br /><br /> <span class="kwrd">protected</span> <span class="kwrd">override</span> <span class="kwrd">void</span> Render(HtmlTextWriter writer)<br /> {<br /> StringBuilder builder = <span class="kwrd">new</span> StringBuilder();<br /><br /> <span class="kwrd">using</span>(StringWriter stringWriter = <span class="kwrd">new</span> StringWriter(builder))<br /> {<br /> HtmlTextWriter tempWriter = <span class="kwrd">new</span> HtmlTextWriter(stringWriter);<br /> <span class="kwrd">base</span>.Render(tempWriter);<br /> }<br /><br /> <span class="kwrd">string</span> find = <span class="str">"<input type="checkbox" "</span>;<br /> <span class="kwrd">string</span> replace = <span class="str">"<input type="checkbox" onClick=""</span> + getPostBack() + <span class="str">"" "</span>;<br /><br /> writer.Write(builder.ToString().Replace(find, replace));<br /> }<br /><br /> <span class="kwrd">protected</span> <span class="kwrd">string</span> getPostBack()<br /> {<br /> <span class="kwrd">return</span> <span class="kwrd">this</span>.Page.GetPostBackEventReference(<span class="kwrd">this</span>, <span class="str">"@CheckPostBack"</span>);<br /> }<br /><br /> <span class="kwrd">protected</span> <span class="kwrd">virtual</span> <span class="kwrd">void</span> OnCheckClick(EventArgs e)<br /> {<br /> <span class="kwrd">if</span> (CheckClick != <span class="kwrd">null</span>) CheckClick(<span class="kwrd">this</span>, e);<br /> }<br /><br /> <span class="kwrd">void</span> IPostBackEventHandler.RaisePostBackEvent(<span class="kwrd">string</span> eventArgument)<br /> {<br /> OnCheckClick(<span class="kwrd">new</span> EventArgs());<br /> }<br /><br /> }<br />}<br /> |