|
Rank: Member Groups: Member
Joined: 8/9/2007 Posts: 18
|
Hi EO,
I'm implementing the TreeView control for the first time and have the basics pretty much sorted. It's databound to a DataTable with nested relationship in parent/child mode, with a CustomItem defined in ASPX file.
I have an ImageButton within the template, but I can't figure out how to handle it's serverside postback? I was expecting to find an ItemCommand event for the TreeView control a la DataList etc.
I'm sure I must be missing something obvious!
Regards,
Richard.
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,217
|
Hi Richard, Actually you didn't miss anything obvious. It's rather complicated to get the server events inside CustomItem sorted out. :) We think the easiest way is to call __doPostBack and delegate the event to somebody else. For example, you can: 1. Put a button in the page, say the button's ID is "Button1"; 2. Call __doPostBack('Button1', 'something') where you want to trigger a server event. For example:
Code: HTML/ASPX
<a href="javascript:__doPostBack('Button1', 'something');">test</a>
The above code will trigger Button1's Click event on the server side; 3. Inside Button1's Click event handler, check __EVENTARGUMENT form variable.
Code: C#
string arg = Request.Form["__EVENTARGUMENT"];
if (arg == "something")
{
//do something
}
else (arg == "something else")
{
//do something else
}
Here "arg" would be the second parameter you pass to __doPostBack. You may need to set Button1's CSS display to "none". For example:
Code: HTML/ASPX
<asp:Button style="display:none" ... />
This way user won't see the button because it really doesn't mean to be clicked by the user. Thanks
|
|
Rank: Member Groups: Member
Joined: 8/9/2007 Posts: 18
|
Hi EO, Think I may have a slightly better way! It seems that you are adding the CustomItems early enough in the pages lifecycle to hook up the events in the serverside code, something like this:
Code: HTML/ASPX
<eo:TreeView id="treContent" runat="server" height="250px" width="200px" allowdragdrop="True" allowdragreordering="True">
<TopGroup>
<TemplateItem>
<CustomItem>
<asp:ImageButton id="imgAdd" runat="server" imageurl="images/icon_add.gif" width="18" height="16" style="vertical-align:middle;" commandname="Add" commandargument='<%#Eval("fRecId")%>' /><%#Eval("fPageTitle")%>
</CustomItem>
</TemplateItem>
</TopGroup>
</eo:TreeView>
...then...
Code: Visual Basic.NET
Private Sub treContent_CustomItemCreated(ByVal sender As Object, ByVal e As EO.Web.NavigationItemEventArgs) Handles treContent.CustomItemCreated
AddHandler CType(e.CustomItem.FindControl("imgAdd"), ImageButton).Click, AddressOf ImageButton_Click
End Sub
Private Sub ImageButton_Click(ByVal sender As System.Object, ByVal e As ImageClickEventArgs)
With CType(sender, ImageButton)
Select Case .CommandName
Case "Add"
Debug.WriteLine("Add New Node to ID: " & .CommandArgument)
End Select
End With
End Sub
To me at least this is a slightly more elegant solution, plus I can handle lots of additional buttons if necessary. I don't know if there are any potential performance issues doing it this way, I may have a quick Reflector at some point to see if these events are effectively hooked up by the framework normally. Regards, Richard.
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,217
|
Hi Richard,
Your way is actually the right way to do it. Actually that's pretty much how ASP.NET itself hooks up event handler for static controls you defined in the page. A lot of people do not want to deal with/understand page life cycle, so we tend to avoid going there over time. :)
Thanks
|
|
Rank: Member Groups: Member
Joined: 8/9/2007 Posts: 18
|
Lol, fair enough. I've spent many hours worrying about ViewState while writing custom controls, so it's almost second nature these days.
Thanks for the speedy feedback all the same.
Regards,
Richard.
|
|
Rank: Member Groups: Member
Joined: 8/9/2007 Posts: 18
|
Hi EO, I think I may have discovered an issue with the ViewState of the TreeView control in this situation. When I PostBack by clicking on my ImageButton, the items seem to be reordered for some reason, possibly related to the use of an embedded CustomItem? To illustrate what is happening, these are the before and after screenshots of my TreeView control when clicking the ImageButton: Before:After:Under normal cirumstances I would only expect to have to databind the control on the inital page load (i.e. If Not Me.IsPostBack Then PopulateTreeView). However, to overcome this issue I am having to disable the ViewState for the control and re-populate it everytime the page loads. If I'm doing anything wrong then please let me know, if not then I hope I may have pointed you in the right direction to correct the issue. If you require an example project which replicates this behaviour then drop me an e-mail and I'll gladly forward you a copy. Regards, Richard.
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,217
|
Hi Richard,
The TreeView does not care about ViewState for CustomItems. CustomItem is a control by itself, so whatever other controls that are inside the CustomItem would take care of their own view state. From the TreeView's part, it does remember an item has been created so it recreates the item. That's why you are seeing the "null" items. As for why they are not re-attached to the CustomItems, I can't be sure without seeing the code. So yes, a project that replicates this behaviour would be much appreciated. I'll pm you as to where to send them.
Thanks
|
|