473,396 Members | 1,917 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

asp.net user control : how to manage click on internal object

Hi,

I want to build an expandable treeview. I saw some code on VB but I know
only C#
I plan for my class that each nodes has its own id (using span for example).

Into a single aspx page without any other control, it's very easy because I
play on URL and parameters and refresh my aspx.
But in a screen having 2 treeview, I have the param and other controls. How
can I organize my object in order to catch (meaning server side) wich node
had been clicked ?

Thk's in adance
Nov 18 '05 #1
4 2167
"Remi Hugnon" <r.******@voila.fr> wrote in message
news:40***********************@news.easynet.fr...
Hi,

I want to build an expandable treeview. I saw some code on VB but I know
only C#
I plan for my class that each nodes has its own id (using span for example).
Into a single aspx page without any other control, it's very easy because I play on URL and parameters and refresh my aspx.
But in a screen having 2 treeview, I have the param and other controls. How can I organize my object in order to catch (meaning server side) wich node
had been clicked ?


You should look into the postback mechanism. It allows you to specify the ID
of the control doing the postback as well as an optional parameter. In your
case, the parameter could be the id of the individual node. The control
could capture the postback event, then find the node given the id, and raise
a node-clicked event for the given node.

Yeah, ok, I admit I should give you an example, but I'll have to write it
from scratch, so it will be a little while...
--
John Saunders
johnwsaundersiii at hotmail
Nov 18 '05 #2
Thk's

It's helpfull.

Remi

"John Saunders" <jo**************@notcoldmail.com> a écrit dans le message
de news:OS**************@TK2MSFTNGP10.phx.gbl...
"Remi Hugnon" <r.******@voila.fr> wrote in message
news:40***********************@news.easynet.fr...
Hi,

I want to build an expandable treeview. I saw some code on VB but I know
only C#
I plan for my class that each nodes has its own id (using span for example).

Into a single aspx page without any other control, it's very easy because I
play on URL and parameters and refresh my aspx.
But in a screen having 2 treeview, I have the param and other controls. How
can I organize my object in order to catch (meaning server side) wich

node had been clicked ?


You should look into the postback mechanism. It allows you to specify the

ID of the control doing the postback as well as an optional parameter. In your case, the parameter could be the id of the individual node. The control
could capture the postback event, then find the node given the id, and raise a node-clicked event for the given node.

Yeah, ok, I admit I should give you an example, but I'll have to write it
from scratch, so it will be a little while...
--
John Saunders
johnwsaundersiii at hotmail

Nov 18 '05 #3
"Remi Hugnon" <r.******@voila.fr> wrote in message
news:40***********************@news.easynet.fr...
Hi,

I want to build an expandable treeview. I saw some code on VB but I know
only C#
I plan for my class that each nodes has its own id (using span for example).
Into a single aspx page without any other control, it's very easy because I play on URL and parameters and refresh my aspx.
But in a screen having 2 treeview, I have the param and other controls. How can I organize my object in order to catch (meaning server side) wich node
had been clicked ?


Remi,

I've begun working on an example for you. Sorry to take so long.

I need to know if your treeview needs to be a UserControl, or if it's ok for
it to be a custom control. Also, I need to know a bit about what you want
the "click" event to be like. For instance, if all you need in the "click"
event is the string id of the clicked node, that's one thing. It would be a
bit more work to do something like the ItemCommand event of the DataGrid,
where the "click" event would receive an EventArgs which would include a
reference to the actual tree node.
--
John Saunders
johnwsaundersiii at hotmail
Nov 18 '05 #4
"Remi Hugnon" <r.******@voila.fr> wrote in message
news:40***********************@news.easynet.fr...
Hi,

I want to build an expandable treeview. I saw some code on VB but I know
only C#
I plan for my class that each nodes has its own id (using span for example).
Into a single aspx page without any other control, it's very easy because I play on URL and parameters and refresh my aspx.
But in a screen having 2 treeview, I have the param and other controls. How can I organize my object in order to catch (meaning server side) wich node
had been clicked ?


Ok, I've got an example. It makes this post a bit long, but I've also
included a zip file with the sources.

The example consists of three classes, DemoTreeView, DemoTreeViewNode and
DemoTreeViewNodeCollection:

DemoTreeView Class declaration:

/// <summary>
/// A simple Tree View control as an example for Remi Hugnon
(r.******@voila.fr)
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:DemoTreeView
runat=server></{0}:DemoTreeView>")]
[ParseChildren(true, "DemoTreeViewNodes")]
public class DemoTreeView : WebControl, IPostBackEventHandler

Note the use of the ParseChildren attribute. This allows the page parser to
interpret the content inside of the DemoTreeView as items to be added to the
DemoTreeNodes property. Each element will be parsed and then added to the
DemoTreeNodes collection via IList.Add.
IPostBackEventHandler is implemented in order to provide a Click event when
a node is clicked.

DemoTreeViewNodes property:

#region Public Properties
/// <summary>
/// Gets the top-level nodes in the tree
/// </summary>
[Category("Behavior"),
Description("The top-level nodes in the tree"),

DesignerSerializationVisibility(DesignerSerializat ionVisibility.Content),
NotifyParentProperty(true),
PersistenceMode(PersistenceMode.InnerDefaultProper ty)]
public DemoTreeViewNodeCollection DemoTreeViewNodes
{
get
{
if (_nodes == null)
_nodes = new DemoTreeViewNodeCollection();

return _nodes;
}
}
#endregion

This is a simple implementation of a read-only property. The interesting
things are in the attributes. The PersistenceMode is set to match the
ParseChildren attribute on the class. DesignerSerializationVisibility is set
to cause the property to be persisted as content between the start and end
tags.

#region Rendering
/// <summary>
/// Renders the contents of the control - what's between the
beginning and ending tags
/// </summary>
/// <param name="writer">The HtmlTextWriter to render to</param>
protected override void RenderContents(HtmlTextWriter writer)
{
DemoTreeViewNodeCollection nodes = DemoTreeViewNodes;
RenderNodes(writer, nodes, string.Empty, 0);

base.RenderContents(writer);
}

private void RenderNodes(HtmlTextWriter writer,
DemoTreeViewNodeCollection nodes, string prefix, int depth)
{
for (int i = 0; i < nodes.Count; i++)
{
DemoTreeViewNode node = nodes[i];
string path;

if (prefix.Length == 0)
path = i.ToString();
else
path = string.Format("{0}|{1}", prefix, i);
writer.AddAttribute(HtmlTextWriterAttribute.Onclic k,
Page.GetPostBackEventReference(this, path));
writer.AddStyleAttribute(HtmlTextWriterStyle.Curso r,
"hand");
writer.AddStyleAttribute(HtmlTextWriterStyle.Left,
string.Format("{0}em", (depth*2)));

writer.AddStyleAttribute(HtmlTextWriterStyle.Posit ion, "relative");
writer.RenderBeginTag(HtmlTextWriterTag.Div);

writer.Write(node.Text);
writer.RenderEndTag();

RenderNodes(writer, node.DemoTreeViewNodes, path,
depth+1);
}
}
#endregion

Because I allow a DemoTreeViewNode to contain other nodes, the control has
to render recursively. The RenderNodes method takes care of that. It goes
through each node in the collection, calculates its path and position, and
then renders it. Each node is rendered as a div element with an OnClick
handler. The handler is what does the postback, passing the path of the
node.

Note that the div ends before the inner nodes are rendered. I found this
necessary so that each node could be clicked. Otherwise, only the outermost
div would receive clicks.

IPostBackEventHandler implementation:

#region IPostBackEventHandler Members
/// <summary>
/// Raise the appropriate event, given the postback data
/// </summary>
/// <param name="eventArgument">The string path to the node
posting back</param>
public void RaisePostBackEvent(string eventArgument)
{
if (eventArgument != null)
{
Page.Trace.Write("RaisePostBackEvent",
eventArgument);
OnClick(new
DemoTreeViewNodeEventArgs(DemoTreeViewNodes[eventArgument]));
}
}
#endregion

RaisePostBackEvent is called upon a postback. The eventArgument parameter
receives the path of the node. This simply raises the Click event of the
DemoTreeView, passing the referenced node. The DemoTreeViewNodeCollection
indexer does the hard work of locating the node.

DemoTreeViewNode Class Declaration:

/// <summary>
/// A node in the DemoTreeView
/// </summary>
[ParseChildren(true, "DemoTreeViewNodes")]
[ToolboxItem(false)]
public class DemoTreeViewNode

At present, the nodes are very simple, so they don't need to derive from
Control or WebControl.

Note the ParseChildren attribute, which is identical to the attribute on the
DemoTreeView class. This permits nodes to contain other nodes. ToolboxItem
is set to false so that the class doesn't show up in the toolbox.

Public Properties

The property implementation is so simple that I won't show it. The
DemoTreeViewNodes property is identical to the same property implemented in
DemoTreeView. The Text property is a simple string property with no
ViewState support.

DemoTreeViewNodeCollection:

This is a simple, strongly-typed collection of DemoTreeViewNode instances.
The only fancy part is the implementation of a second indexer. This indexer
accepts a pipe-separated list of integer node offsets and locates the
appropriate node. For instance, "1|2|0" would return
demoTreeView.DemoTreeViewNodes[1].DemoTreeViewNodes[2].DemoTreeViewNodes[0].

Test page:

<%@ Register TagPrefix="cc1" Namespace="JWS.WebInfrastructure.WebControls"
Assembly="JWS.WebInfrastructure.WebControls"%>
<%@ Page Language="C#" %>
<script runat="server">
void DemoTreeView1_Click(object sender,
JWS.WebInfrastructure.WebControls.DemoTreeView.Dem oTreeViewNodeEventArgs e)
{
Label1.Text = string.Format("Node clicked - {0}", e.Node.Text);
}</script>
<html>
<body>
<form id="form1" runat="server">
<div>
<cc1:DemoTreeView ID="DemoTreeView1" Runat="server"
OnClick="DemoTreeView1_Click">
<cc1:DemoTreeViewNode Text="treeViewNode5">
<cc1:DemoTreeViewNode Text="treeViewNode5.1">
</cc1:DemoTreeViewNode>
</cc1:DemoTreeViewNode>
<cc1:DemoTreeViewNode Text="treeViewNode6">
<cc1:DemoTreeViewNode Text="treeViewNode6.1">
<cc1:DemoTreeViewNode Text="treeViewNode6.1.0">
</cc1:DemoTreeViewNode>
</cc1:DemoTreeViewNode>
</cc1:DemoTreeViewNode>
</cc1:DemoTreeView>
<asp:Label ID="Label1" Runat="server" Text="Label">
</asp:Label>
</div>
</form>
</body>
</html>

So, the way this all works is that each node renders as:

<div onclick="__doPostBack('TreeView1','1|0|0')"
style="cursor:hand;left:4em;position:relative;">
treeViewNode6.1.0
</div>

When clicked, IPostBackEventHandler.RaisePostBackEvent will be called with
"1|0|0" as an argument. The indexer will locate node "treeViewNode6.1.0",
and will raise the Click event, passing that node in the EventArgs. The
Click event handler will fire and display the Text property of the node in
the label.

I hope that helps and isn't too long. Any questions, please ask.
--
John Saunders
johnwsaundersiii at hotmail


Nov 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Chris Millar | last post by:
I have a user control that i wish to extend to change the date when the user selects the numeric up down button. The code explains itself, hope someone can help. any ideas appreaciated.. ...
0
by: Duncan Mole | last post by:
Hi, I have created a control which draws a title bar and provides a drop down menu for a Smart Device Application. It seemed to work fine until I came to add an event handler to act on Paint...
1
by: Rhy Mednick | last post by:
I'm creating a custom control (inherited from UserControl) that is displayed by other controls on the form. I would like for the control to disappear when the user clicks outside my control the...
5
by: Dave Kolb | last post by:
Is there any other solution for an ASPNET application to access network resources other than running as SYSTEM, using delegation (a nightmare to get to work) or the COM+ solution? I cannot seem to...
6
by: Steve Booth | last post by:
I have a web form with a button and a placeholder, the button adds a user control to the placeholder (and removes any existing controls). The user control contains a single button. I have done all...
0
by: Matthew | last post by:
All, I have searched google and the newsgroups but can't find anything the same as what I am experiencing (though I may have missed something). I have controls (textboxes) within UserControls...
2
by: Sara | last post by:
Hello there, Iam creating a user control with 2 buttons, And i have a public static variable i'll be changing the value of the variable in the button click events. When i consume the user...
8
by: mark.norgate | last post by:
I've run into a few problems trying to use generics for user controls (classes derived from UserControl). I'm using the Web Application model rather than the Web Site model. The first problem...
1
by: Maury | last post by:
Hello, I created two user control, the first manage an object of mine, the second is a DataList in which each Item is the first control...when I click the 'save' button in the first control, the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.