object reference for adding click event

 I created a custom template just so I can add a button to the header of an Ajax tab panel and append a click event to remove the panel.  The template works fine for loading everything into the header as it should, however I can not set up the click event to reference the tab container.  I have a tab container statically placed on the page within an update panel which is inside a table.  The tab container is labeled TabContainer1 (default ID), and when importing the main page into this class it states this object is protected and can not be referenced in this context.  Embedding the following class into the page class I'm getting the error "Reference to a non-shared member requires an object reference.".  I have tried to dimension it as an object and it still does not want to reference it.  I can not find a way to add this click event to the button within this custom template...any help?

 

    Public Class DynamicHeaderTemplate
        Implements ITemplate

        Public Shared label As String
        Public Shared headTxt As String
        Public head As Label
        Public closeBtn As ImageButton

        'Constructor
        Public Sub New(ByVal label As String)
            Dim head As Label = New Label()
            Dim headTxt As String = label
            Dim saveTxt As Object = CType(headTxt, Object)
            head.Text = headTxt
            HttpContext.Current.Session("headTxt") = headTxt

            Dim closeBtn As ImageButton = New ImageButton()
            closeBtn.ImageUrl = "./Images/xButton.png"
            AddHandler closeBtn.Click, AddressOf Me.TabClose_Click
            closeBtn.ToolTip = "Close Tab"
        End Sub

        Public Sub TabClose_Click()
            Dim tabId As String = TabContainer1.ActiveTab.ID
            TabContainer1.Tabs.Remove(TabContainer1.ActiveTab)
            HttpContext.Current.Session(tabId) = Nothing
            HttpContext.Current.Session("label" & tabId) = Nothing
            HttpContext.Current.Session("url" & tabId) = Nothing
            TabCount = TabCount - 1
        End Sub

        Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements ITemplate.InstantiateIn

            ' Add Tab Label
            Dim head As Label = New Label()
            headTxt = CStr(HttpContext.Current.Session("headTxt"))
            head.Text = headTxt
            container.Controls.Add(head)

            ' Add Close Button
            Dim closeBtn As ImageButton = New ImageButton()
            closeBtn.ImageUrl = "./Images/xButton.png"
            AddHandler closeBtn.Click, AddressOf Me.TabClose_Click
            closeBtn.ToolTip = "Close Tab"
            container.Controls.Add(closeBtn)
            Exit Sub
        End Sub
    End Class

0
chohner
4/23/2009 9:01:57 PM
asp.net.custom-server-controls 3498 articles. 0 followers. Follow

5 Replies
697 Views

Similar Articles

[PageSpeed] 41

Hi,

You can try this to find the TabPanel and the TabContainer:

Public Sub TabClose_Click(ByVal sender As ImageButton, ByVal e As System.EventArgs)

'sender.NamingContainer is TabPanel

'sender.NamingContainer.NamingContainer is TabContainer

End Sub

 


Sincerely,
Allen Chen
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
0
Allen
4/27/2009 7:32:37 AM

 That did the trick!  Now all I have to do is find a way to load my content dynamically to each of the tab panels and I will be set.  Any ideas would be greatly appreciated!

 

 

Imports System
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports AjaxControlToolkit
Imports System.Net

Partial Class _Default
    Inherits System.Web.UI.Page
    Public Shared _tabCount As System.Nullable(Of Integer)
    Public Shared headTxt As String

    'Keep in SessionState the count on dynamically added tabs
    Public Shared Property TabCount() As Integer
        Get
            If _tabCount = Nothing Then
                _tabCount = HttpContext.Current.Session("TabCount")
            Else
                If HttpContext.Current.Session("TabCount") Is Nothing Then
                    TabCount = _tabCount
                Else
                    TabCount = CInt(HttpContext.Current.Session("TabCount"))
                    _tabCount = TabCount
                End If
            End If
            Return _tabCount.Value
        End Get
        Set(ByVal value As Integer)
            _tabCount = value
            HttpContext.Current.Session("TabCount") = value
        End Set
    End Property

    Public Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        If Not IsPostBack Then
            _tabCount = 0
            TabCount = 0
        End If
        If IsPostBack Then
            'This is to reload each of tab panel containers upon page post back
            Dim tabIdCount As Integer = 0
            For c As Integer = 0 To TabCount - 1
                Dim tabId As String = HttpContext.Current.Session("tab" & tabIdCount)
                Dim label As String = HttpContext.Current.Session("labeltab" & tabIdCount)
                Dim url As String = HttpContext.Current.Session("urltab" & tabIdCount)
                Dim tabIndex As String = HttpContext.Current.Session("tabIndex" & tabIdCount)
                AddPanel(tabId, label, url, TabIndex)
                tabIdCount = tabIdCount + 1
            Next
        End If
    End Sub

    'Dynamically add a panel to the TabContainer
    Public Sub AddPanel(ByVal tabId As String, ByVal label As String, ByVal url As String, ByVal tabIndex As Integer)
        Dim newTab As New TabPanel()
        Dim content As New UpdatePanel()
        newTab.ID = tabId
        newTab.TabIndex = tabIndex
        newTab.HeaderTemplate = New DynamicHeaderTemplate(label)
        TabContainer1.Tabs.Add(newTab)
        content.UpdateMode = UpdatePanelUpdateMode.Conditional
        content.ID = tabId & "Content"
        newTab.Controls.Add(content)
        TabContainer1.ActiveTab = newTab
        HttpContext.Current.Session(tabId) = tabId
        HttpContext.Current.Session("label" & tabId) = label
        HttpContext.Current.Session("url" & tabId) = url
        HttpContext.Current.Session("tabIndex" & tabIndex) = tabIndex
    End Sub

    'Click event to add a new panel and update the TabCount property
    Protected Sub AddPanel_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim myLink As LinkButton = DirectCast(sender, LinkButton)
        Dim label As String = myLink.Text
        Dim url As String = myLink.CommandName
        Dim tabId As String = "tab" & TabCount
        Dim tabIndex As Integer = TabCount
        AddPanel(tabId, label, url, tabIndex)
        TabCount = TabCount + 1
    End Sub

    Public Class DynamicHeaderTemplate
        Implements ITemplate

        Public Shared label As String
        Public Shared headTxt As String
        Public head As Label

        Public Sub New(ByVal label As String)
            Dim head As Label = New Label()
            Dim headTxt As String = label
            head.Text = headTxt
            HttpContext.Current.Session("headTxt") = headTxt

            Dim closeBtn As ImageButton = New ImageButton()
            closeBtn.ImageUrl = "./Images/xButton.png"
            AddHandler closeBtn.Click, AddressOf Me.TabClose_Click
            closeBtn.ToolTip = "Close Tab"
        End Sub

        Public Sub TabClose_Click(ByVal sender As ImageButton, ByVal e As System.EventArgs)
            Dim TabContainer1 As TabContainer = sender.NamingContainer.NamingContainer
            Dim TabPan As TabPanel = sender.NamingContainer
            Dim actTabId As String = TabPan.ID
            Dim content As UpdatePanel = TabPan.FindControl(actTabId & "Content")
            Dim tabCountId As Integer = TabPan.TabIndex + 1
            TabPan.Controls.Remove(content)
            TabContainer1.Tabs.Remove(TabPan)
            HttpContext.Current.Session.Remove(actTabId)
            HttpContext.Current.Session.Remove("label" & actTabId)
            HttpContext.Current.Session.Remove("url" & actTabId)
            HttpContext.Current.Session.Remove("tabIndex" & actTabId)
            For c As Integer = tabCountId To TabCount
                HttpContext.Current.Session("tab" & tabCountId - 1) = HttpContext.Current.Session("tab" & tabCountId)
                HttpContext.Current.Session("labeltab" & tabCountId - 1) = HttpContext.Current.Session("labeltab" & tabCountId)
                HttpContext.Current.Session("urltab" & tabCountId - 1) = HttpContext.Current.Session("urltab" & tabCountId)
                HttpContext.Current.Session("tabIndex" & tabCountId - 1) = HttpContext.Current.Session("tabIndex" & tabCountId)
                tabCountId = tabCountId + 1
            Next
            TabCount = TabCount - 1
        End Sub

        Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements ITemplate.InstantiateIn

            ' Add Tab Label
            Dim head As Label = New Label()
            headTxt = CStr(HttpContext.Current.Session("headTxt"))
            head.Text = headTxt
            container.Controls.Add(head)

            ' Add Close Button
            Dim closeBtn As ImageButton = New ImageButton()
            closeBtn.ImageUrl = "./Images/xButton.png"
            AddHandler closeBtn.Click, AddressOf Me.TabClose_Click
            closeBtn.ToolTip = "Close Tab"
            container.Controls.Add(closeBtn)
            Exit Sub
        End Sub
    End Class
End Class

0
chohner
4/28/2009 5:21:18 PM

Hi,

To do this you can set visibility of the control:

 

Public Sub TabClose_Click(ByVal sender As ImageButton, ByVal e As System.EventArgs)

Dim tp As TabPanel = sender.NamingContainer

' Dim tc As TabContainer = sender.NamingContainer.NamingContainer

 

tp.Controls(1).FindControl(
"Panel1").Visible = True 'Add controls to content of tp via tp.Controls(1)

 

End Sub

 

<cc1:TabPanel runat="server" ID="TabPanel1">

 

<ContentTemplate>

Tab1

<asp:Panel Visible="false" BackColor="Red" ID="Panel1" runat="server">

Hello World

</asp:Panel>

</ContentTemplate>

</cc1:TabPanel>


Sincerely,
Allen Chen
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
0
Allen
4/29/2009 2:44:33 AM

 My click event is working inside my header template.  I was referring to the content area of the tab panel...  I am trying to figure out how to post content to an update panel I have added to the content area of the tab panel, it may be an app, document file, or http content and I am trying to figure out how to load it.  Each tab panel's content area will need to load the url passed to it and then I would like to setup history points for each.  The other problem I may have with it is controlling the state of the applications that are loaded, basically when a postback is made can I resume the application where the user left off filling in all the data already entered....  Thanks for the ideas.

0
chohner
4/29/2009 3:33:24 PM

 This is the code for the content template....which sets the string from the stream, but will not write it, or at least it doesn't display.

 

    Public Class DynamicContent
        Implements ITemplate

        Public Shared destin As String

        Public Sub New(ByVal url As String)
            Dim destin As String = url
            HttpContext.Current.Session("destin") = destin
            Dim request As WebRequest = WebRequest.Create(destin)
            Dim response As WebResponse = request.GetResponse()
            Dim reader = New StreamReader(response.GetResponseStream())
            Dim data As String = reader.ReadToEnd
        End Sub

        Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements ITemplate.InstantiateIn
            Dim destin As String = HttpContext.Current.Session("destin")
            Dim request As WebRequest = WebRequest.Create(destin)
            Dim response As WebResponse = request.GetResponse()
            Dim reader = New StreamReader(response.GetResponseStream())
            Dim data As String = reader.ReadToEnd
            Console.Write(data)
            Exit Sub
        End Sub
    End Class

0
chohner
4/29/2009 10:30:44 PM
Reply:

Similar Artilces:

Table Cell click event
Hi, I used to have a custom server control inhirted from TableCell control to implement click event in VS.NET 2003.  When I use the custom control in VS.NET 2003 Web application (dynamically adding to table object), everything works fine.  Below is the code from 2003 project, and everything works good in this way.                 Dim c As New MyCustomTableCell                  c.Width = 200     &nb...

problems listenening to events on a custom server control from another custom server control
 Hi all,I am having problems listening to events on one of custom server controls(AddBookmark) from another custom server control(BookmarkList). The listener or the BookmarkList control has a property that can be set to the AddBookmark control (string at design but the reference is obtained at run-time). I am able to get a reference to the AddBookmark control and add a listener to it on the BookmarkList control on the OnPrender of BookmarkList. But the problem is that when the events on AddBookmark gets fired, the listener added from the BookmarkList is no longer present and the listene...

Adding events to controls, e.g. adding a click event to a label control
 Hi, Is it possible to add a click event to a label control so that one can make something happen when a user clicks the label? How can it be done? One could of course use a linkbutton control, which has an in-built click event. Unfortunately a linkbutton insists on doing a postback every time it is clicked.Thank you!Regards.   Label is span control in html call javascript onclick event <span id="fkdsjfl" onclick=" ">here</span> if you want to call postback event then chk ithttp://forums.devx.com/showthread.php?t=142257 If this post...

Custom Server Control Design Time Property Shows "Object reference not set to an instance of an object"
The property "ReferenceValue" from my code below keeps showing an error in the properties window as above. The control however functions perfectly otherwise. 1 Imports System 2 Imports System.Web 3 Imports System.Web.UI 4 Imports System.Web.UI.WebControls 5 Imports Capr.Web.UI.ControlInterfaces 6 7 8 Namespace Controls 9 10 Public Class WMSTextBox 11 Inherits System.Web.UI.WebControls.TextBox 12 Implements ICaprBaseControl 13 14 Public Sub New() 15 End Sub 16 17 Public P...

Custom event for Custom server control
  Hi All, I have a custom server control (Multi selection drop downlist). This control contains AspxEditButton control(which is like textbox with button), check box list control and panel.I have to display all selected values in AspxEditButton at control is leaving that panel. How can i create LostFocus event for panel . Please anyone help meRegards, Anu All I can think of, is that there is a javascript "onblur" you could look into, maybe you could use some AJAX techniques to make that JavaScript event fire a server side ev...

Custom Events in Custom Server Control
 Hi There I've created a custom server control. I have extended it by adding my own event which fires in one of the aggregated control's event handlers. My probelms is that I would like to be able to specify the event handler for the event from the aspx file as a tag attribute.Does anyone know how to link the attribute of a customer server control tag in the aspx file to a custom event in the class source for the control?   I've done this several times with Custom Controls.  All I do is declare the event in the CustomControl's class public...

Erros Invalid reference to object / Adding reference to object / Invalid server ID in backlink obituary is purged
--____SAPVXZPASDOFICLTZFCS____ Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable hi their, I have a customer who gets a lot of the above error messages. I'm thinking, it will be a timinge issue that the server cannot backlink = or sync the Database? Am I right? Or shell this be other problems? The Invalid Reference to Object is shown on User Object, simular to the = "Adding reference to object".=20 Their are 3 Netware Servers in the Network, and 2 of the are a 2 Node = Cluster. The eDir is 8.7.3.2 and the OS is NW6.5 S...

Button click event not fired for a custom webpart where controls are added dynamically.
We are migrating a Sharepoint 2003 site to MOSS 2007 and ASP.NET 2.0. We have also migrated the webparts but they are still inheriting from Sharepoint.WebPart. We have a webpart where controls are added dynamically at runtime. On this webpart a button is added dynamically and a click event handler is also attached. The button on the webpart is displayed only when there is some data. The button deletes the selected row of data when clicked. But when we click on the button, the page gets posted but the event handler is not called. We analysed and found that the code where we add the button to ...

adding custom properties to a custom server control
1.  I want to add a public property to a custom server control.  the property is called Search Type and the possible choices that will appear in the properties window from the asp.net page would be client, provider, vendor and service.  I want this property to function much like the border style property works for an asp.net button. 2.  My second question would be how do I add a section to the properties window?  What i mean is for this custom server control, i want to add a section called Search Features much like the appearance or behavior sections for an asp.net ...

handling events for controls in custom server control
Hi,     I am having a problem in using server controls in ASP.Net, with C#.  I am trying to create a server control which contains 2 other server controls that I created (Called MyImageButton).  MyImageButton extends the standard ImageButton with some other properties & methods that I needed.  It therefore also has the 'OnClick' event.  From the original server control, I would like to handle the Click event for these 2 image buttons in the same server control.  Basically, this server control will show 2 images, ascending and descendin...

Button click event not getting fired for a custom webpart where controls are added dynamically.
 We are migrating from ASP.NET1.1/Sharepoint 2003 to ASP.NET 2.0/Moss 2007. We have a webpart on which controls are getting added dynamically. We have a button which is also dynamically added and a click event handler is also attached to it. However when we click on this button the page gets posted but the event is not fired. We analysed and found that the code to add the button in the webparts controls collection is executed but the code to add the button to it's container (that is a table) is not getting executed. Hence the event is not getting handled. If we forcefully add the bu...

Usercontrol vs Custom Server Controls, Any need for Custom Server Controls
Hello I'm new to ASP.Net so this is probably a stupid question but, is there any need to create customer server controls now that you can pre-compile usercontrols into a dll and thus easily share between applications? This blog post from David Ebb describes how to do it and the restrictions he note don't seem that bad (other than being able to embed resources such as images in the dll). http://blogs.msdn.com/davidebb/archive/2005/10/30/487160.aspx His article desribes the techniques for ASP.Net 2.0 and he suggests that there will be further improvements in .Net 3.5 in this ar...

Adding controls to a Custom DataGrid Server Control
Edited by SomeNewKid. Please post code between <code> and </code> tags. I'm working on creating my own customized datagrid control. I've created a class which inherits "System.Web.UI.WebControls.DataGrid" and when I add my new datagrid control library to my project it seems to work nicely. Now I want to add some submit buttons to appear at the bottom of my datagrid control. How do I do this? Sorry, I'm a bit of a newbie at server controls. Here's what I've tried: Public Class UberDataGrid Inherits DataGrid Private MyButton As Button ...

Custom DataGrid control, click event never executes when adding a buttom from render. c#
Hi, this is what I've got: public class Custom_DataGrid : DataGrid {     public bool showbuttom = false;    protected override void Render(HtmlTextWriter output)     {         ...         if (this._showbuttom)         {            submitButton.ID = "button";             submitButton.Text = "+"; &...

Web resources about - object reference for adding click event - asp.net.custom-server-controls

Wikipedia talk:Reference desk/Guidelines/Medical advice - Wikipedia, the free encyclopedia
I disagree with this implication that this section of the guideline represents the consensus. If you look at the last few years of discussion ...

Microsoft dismisses iPad Pro as “a companion device” and references old Steve Jobs stylus quote
... to be a companion device.” Laycock also said that “at one point in time, Apple declared that if there’s a stylus, that’s failure” – a reference ...

New York Times Removes Reference to President Obama and Cable News
... — Noah Rothman (@NoahCRothman) December 18, 2015 The Times story was published Thursday, but by Friday morning, the reference to cable news ...

Can you spot these classic film references hidden in Pixar movies?
The animators at Pixar definitely love to throw in allusions to some of their favorite movies in their own films. Vimeo user Jorge Luengo Ruiz ...

10 hidden references to classic films in your favorite Pixar movies
... Madrid, told BuzzFeed that he noticed a ton of allusions in "Toy Story 3" and was inspired to make a video highlighting the cinematic references ...

See The Classic Movie References Found In Pixar's Greatest Films
... all well aware of just how clever Pixar movies are. These emotionally engaging stories are full of intricate themes and crammed with references ...

Zootopia Trailer Adds a Godfather Reference to Disney Film - Collider
Watch a new trailer for the Disney animated movie Zootopia, which takes place in a world inhabited by talking animals and features Jason Bateman ...

Hillary Clinton Ends Democratic Debate With A Star Wars Reference
The other day, President Obama ended his White House press conference with a reference to The [...]

Review: Klipsch X20i Reference In-Ear Headphones
These Klipsch X20i Reference In-Ear Headphones are pretty darn fantastic, and worth every penny.`

The Force Is Strong With This Sportscaster Who Drops ‘Star Wars’ References Like A Jedi
The Force Is Strong With This Sportscaster Who Drops ‘Star Wars’ References Like A Jedi

Resources last updated: 1/15/2016 11:43:13 PM