WebChart and FindControl

TeeChart for Microsoft Visual Studio .NET, Xamarin Studio (Android, iOS & Forms) & Monodevelop.
Post Reply
norman
Newbie
Newbie
Posts: 82
Joined: Fri Jan 25, 2008 12:00 am

WebChart and FindControl

Post by norman » Fri Apr 10, 2009 4:13 pm

Hi,

I'm trying to get a reference to the control on my page that causes the postback but I'm running into problems when the postback is caused by the chart (e.g. after the WebChart_ClickSeries event).

I have a user control (.ascx) comprising of a chart control with its autopostback property set to true and several other asp.net controls (e.g. linkbuttons etc.) that also cause a postback. I have a routine in my user control that I use to determine which control causes the postback. Based on the outcome of this I decide whether or not to refresh the chart or not. The GetPostBackControl procedure is as follows:

Code: Select all

Public Shared Function GetPostBackControl(ByVal targPage As Page) As Control

            ' ASP.NET stores the name of the control that initiated the postback of the WebForm
            ' in a hidden input field named "__EVENTTARGET" with two exceptions - when Button and 
            ' ImageButton controls are clicked on the form. We need to handle these cases differently

            Dim ctrl As Control = Nothing

            Dim ctrlname As String = targPage.Request.Params.Get("__EVENTTARGET")
            If Not (ctrlname Is Nothing) And ctrlname <> String.Empty Then
                ctrl = targPage.FindControl(ctrlname)
            Else
                ' Handle the Button control postbacks
                Dim ctl As String
                For Each ctl In targPage.Request.Form
                    Dim c As Control = targPage.FindControl(ctl)
                    If TypeOf c Is System.Web.UI.WebControls.Button Then
                        ctrl = c
                        Exit For
                    End If
                Next ctl
            End If

            ' Handle the ImageButton postbacks
            If ctrl Is Nothing Then
               ' not shown for brevity
            End If

            Return ctrl

        End Function
and I call it as follows:

Code: Select all

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Page.IsPostBack Then
            Dim ctrl As Control = GetPostBackControl(Me.Page)
            If Not (ctrl Is Nothing) Then
                ' do something
            End If
        End If

    End Sub
This works for all controls on my page except for the chart control. If I step through the code ctrlname is getting populated with the Client ID of the chart but the FindControl method always returns Nothing. Anybody have any ideas?

Thanks,

Norman

Sandra
Site Admin
Site Admin
Posts: 3132
Joined: Fri Nov 07, 2008 12:00 am

Post by Sandra » Tue Apr 14, 2009 9:01 am

Hello norman,


We couldn't reproduce your issue here. Please, could you send us a simple example project we can run "as-is" to reproduce the problem here? You can either post your files at news://www.steema.net/steema.public.attachments newsgroup or at our upload page.


Thanks,
Best Regards,
Sandra Pazos / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

norman
Newbie
Newbie
Posts: 82
Joined: Fri Jan 25, 2008 12:00 am

Post by norman » Tue Apr 14, 2009 10:33 am

Hi Sandra,

I've uploaded a file called WebApplication1.zip to your upload page. If you run the project, and click on the LinkButton, ImageButton or Button the client ID of the control that caused the postback will be written to the screen. If you click the chart which has its autopostback property set to true then an error results.

Thanks,

Norman

Marc
Site Admin
Site Admin
Posts: 1266
Joined: Thu Oct 16, 2003 4:00 am
Location: Girona
Contact:

Post by Marc » Wed Apr 15, 2009 12:35 pm

Hello Norman,

This is related to naming convention. If you look at the page source of your example project when run, you will notice that the WebChart ID is: "WebUserControl1_1_WebChart1"

At page_load time all controls are reported to have the same name format, eg.
me.Controls(3).Controls(1).Controls(2).ClientID = "WebUserControl1_1_LinkButton1"
..that however is not the name that the postback passes. The ID passed when posted is of the same format as the other controls on the form, ie. "WebUserControl1_1$LinkButton1" and "WebUserControl1_1$WebChart1". 'ctrlnames' of most Form controls are sent in that format for FindControl but WebChart reamains as per ClientID. I don't have an explanation for that just yet but can offer you a workaround so as not to hold you up.

---
This code mod in GetPostbackControl will return the desired result:

Code: Select all

If Not (ctrlname Is Nothing) And ctrlname <> String.Empty Then
            ctrl = targPage.FindControl(ctrlname.Replace("_1_", "_1$"))
If we make changes here we might break existing customer code so if we arrive at a recommendable internal code modification we might need to hold it up for the next major version upgrade.

Regards,
Marc
Steema Support

norman
Newbie
Newbie
Posts: 82
Joined: Fri Jan 25, 2008 12:00 am

Post by norman » Thu Apr 16, 2009 7:40 am

Hi Marc,

Thanks for confirmation. I implemented such a workaround.

Post Reply