473,763 Members | 6,149 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2191
"Remi Hugnon" <r.******@voila .fr> wrote in message
news:40******** *************** @news.easynet.f r...
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
johnwsaundersii i 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******** ******@TK2MSFTN GP10.phx.gbl...
"Remi Hugnon" <r.******@voila .fr> wrote in message
news:40******** *************** @news.easynet.f r...
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
johnwsaundersii i at hotmail

Nov 18 '05 #3
"Remi Hugnon" <r.******@voila .fr> wrote in message
news:40******** *************** @news.easynet.f r...
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
johnwsaundersii i at hotmail
Nov 18 '05 #4
"Remi Hugnon" <r.******@voila .fr> wrote in message
news:40******** *************** @news.easynet.f r...
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, DemoTreeViewNod e and
DemoTreeViewNod eCollection:

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}:DemoTreeVie w>")]
[ParseChildren(t rue, "DemoTreeViewNo des")]
public class DemoTreeView : WebControl, IPostBackEventH andler

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.
IPostBackEventH andler is implemented in order to provide a Click event when
a node is clicked.

DemoTreeViewNod es property:

#region Public Properties
/// <summary>
/// Gets the top-level nodes in the tree
/// </summary>
[Category("Behav ior"),
Description("Th e top-level nodes in the tree"),

DesignerSeriali zationVisibilit y(DesignerSeria lizationVisibil ity.Content),
NotifyParentPro perty(true),
PersistenceMode (PersistenceMod e.InnerDefaultP roperty)]
public DemoTreeViewNod eCollection DemoTreeViewNod es
{
get
{
if (_nodes == null)
_nodes = new DemoTreeViewNod eCollection();

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. DesignerSeriali zationVisibilit y 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">T he HtmlTextWriter to render to</param>
protected override void RenderContents( HtmlTextWriter writer)
{
DemoTreeViewNod eCollection nodes = DemoTreeViewNod es;
RenderNodes(wri ter, nodes, string.Empty, 0);

base.RenderCont ents(writer);
}

private void RenderNodes(Htm lTextWriter writer,
DemoTreeViewNod eCollection nodes, string prefix, int depth)
{
for (int i = 0; i < nodes.Count; i++)
{
DemoTreeViewNod e node = nodes[i];
string path;

if (prefix.Length == 0)
path = i.ToString();
else
path = string.Format(" {0}|{1}", prefix, i);
writer.AddAttri bute(HtmlTextWr iterAttribute.O nclick,
Page.GetPostBac kEventReference (this, path));
writer.AddStyle Attribute(HtmlT extWriterStyle. Cursor,
"hand");
writer.AddStyle Attribute(HtmlT extWriterStyle. Left,
string.Format(" {0}em", (depth*2)));

writer.AddStyle Attribute(HtmlT extWriterStyle. Position, "relative") ;
writer.RenderBe ginTag(HtmlText WriterTag.Div);

writer.Write(no de.Text);
writer.RenderEn dTag();

RenderNodes(wri ter, node.DemoTreeVi ewNodes, path,
depth+1);
}
}
#endregion

Because I allow a DemoTreeViewNod e 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.

IPostBackEventH andler implementation:

#region IPostBackEventH andler Members
/// <summary>
/// Raise the appropriate event, given the postback data
/// </summary>
/// <param name="eventArgu ment">The string path to the node
posting back</param>
public void RaisePostBackEv ent(string eventArgument)
{
if (eventArgument != null)
{
Page.Trace.Writ e("RaisePostBac kEvent",
eventArgument);
OnClick(new
DemoTreeViewNod eEventArgs(Demo TreeViewNodes[eventArgument]));
}
}
#endregion

RaisePostBackEv ent 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 DemoTreeViewNod eCollection
indexer does the hard work of locating the node.

DemoTreeViewNod e Class Declaration:

/// <summary>
/// A node in the DemoTreeView
/// </summary>
[ParseChildren(t rue, "DemoTreeViewNo des")]
[ToolboxItem(fal se)]
public class DemoTreeViewNod e

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
DemoTreeViewNod es property is identical to the same property implemented in
DemoTreeView. The Text property is a simple string property with no
ViewState support.

DemoTreeViewNod eCollection:

This is a simple, strongly-typed collection of DemoTreeViewNod e 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.De moTreeViewNodes[1].DemoTreeViewNo des[2].DemoTreeViewNo des[0].

Test page:

<%@ Register TagPrefix="cc1" Namespace="JWS. WebInfrastructu re.WebControls"
Assembly="JWS.W ebInfrastructur e.WebControls"% >
<%@ Page Language="C#" %>
<script runat="server">
void DemoTreeView1_C lick(object sender,
JWS.WebInfrastr ucture.WebContr ols.DemoTreeVie w.DemoTreeViewN odeEventArgs e)
{
Label1.Text = string.Format(" Node clicked - {0}", e.Node.Text);
}</script>
<html>
<body>
<form id="form1" runat="server">
<div>
<cc1:DemoTreeVi ew ID="DemoTreeVie w1" Runat="server"
OnClick="DemoTr eeView1_Click">
<cc1:DemoTreeVi ewNode Text="treeViewN ode5">
<cc1:DemoTreeVi ewNode Text="treeViewN ode5.1">
</cc1:DemoTreeVie wNode>
</cc1:DemoTreeVie wNode>
<cc1:DemoTreeVi ewNode Text="treeViewN ode6">
<cc1:DemoTreeVi ewNode Text="treeViewN ode6.1">
<cc1:DemoTreeVi ewNode Text="treeViewN ode6.1.0">
</cc1:DemoTreeVie wNode>
</cc1:DemoTreeVie wNode>
</cc1:DemoTreeVie wNode>
</cc1:DemoTreeVie w>
<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="__doPo stBack('TreeVie w1','1|0|0')"
style="cursor:h and;left:4em;po sition:relative ;">
treeViewNode6.1 .0
</div>

When clicked, IPostBackEventH andler.RaisePos tBackEvent 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
johnwsaundersii i at hotmail


Nov 18 '05 #5

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

Similar topics

0
1924
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.. Chris. code :
0
1906
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 messages in the form which has drawn the control. Evidently, the control is consuming all of these messages. How can I pass them back/on? I have a reference to the owner form but calling Refresh() via this reference isn't helping. Help! Do I need to...
1
4026
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 same way a menu does. To do this my control needs to get notified when the user tried to click off of it. The Leave and LostFocus events of the UserControl work most of the time but not always. For example, if they click on a part of the form...
5
3110
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 impersonate a user and obtain network credentials using the DuplicateTokenEx call with appropriate parameters even though the call seems to not fail. I check my identity has changed but can only still do local commands. I would consider...
6
3380
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 the usual stuff of recreating the usercontrol in the Page Init event. The 'failure' sequence is as follows: - select web form button to display the user control - select user control button, event fires - select web form button to display...
0
2470
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 which are not behaving as I would expect. Specifically, if there is a command button external to the usercontrol which is activated by a shortcut key (eg Alt-B), the command button Click event handler code 'executes' even though the textbox set...
2
1847
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 control in the winforms application how to automatically get the value of the public variable on click of the user control.
8
3186
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 I'm having is that the partial class signature in my projectDetails.ascx.cs file looks like this: public partial class ProjectDetailsControl<TEntryServiceProvider: UserControl, INamingContainer where TEntryServiceProvider : IEntryServiceProvider...
1
1504
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 object is saved with new values, but in the list is displayed the old value (until I postBack again) Can someone tell me the right way to manage updates of nested user controls? Thanks
0
9563
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
9937
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9822
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8821
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3522
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.