Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to use Delegates - call a parent function from a Child UserControl

Status
Not open for further replies.

NightZEN

Programmer
Apr 29, 2003
142
US
I have been reading about using delegates on a few posts here and msdn, but still don't quite get it. Here is my situation: I have a "mother" page add_customer.aspx and a"child" user control add_group.ascx. On the mother page is an "add group" button that makes the the panel inwhich the add_customer control resides VISIBLE=True. On the control, the user can edit the list of groups, or add a new one. After the edit is complete I want to make the panel again VISIBLE=False, as well as refill the "mother" page's dataset and rebind the controls. It seems clear that the correct method is to use a delegate to runa function on the mother page from the child user control, but I am having understanding how. Please help, if you have a moment, or direct me some similar examples.

Thanks
 
How about some more info. Here is what i have so far:

In my User Control is a datagrid where you can edit and add records. In the code behind page, I declare a delegate like so:
Code:
Public Class add_group
    Inherits System.Web.UI.UserControl
    Private delReturnValue As System.Delegate
    Private intGroup As Integer

...etc.

Then after updating the data, I have tried to invoke the Delegate. As I understand it, this is the User Control's method that does the work to trigger the parent page's PopulateData() method.

Code:
Private Sub DataGrid1_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.UpdateCommand
        'Grab edited row for delgate call
        intGroup = e.Item.Cells(1).Text

        'Fill DataSet and identify row to edit
        da_addgroup.Fill(Ds_addgroup)
        Dim str_debug As String = e.Item.Cells(1).Text
        Dim objEditedRow As DataRow = Ds_addgroup.Tables("tbl_customer_group").Rows.Find(e.Item.Cells(1).Text)

        'Cycle through valid "data" cells and put 
        'information back in underlying DataSet
        Dim intCount As Integer
        For intCount = 0 To e.Item.Cells.Count - 1
            If e.Item.Cells(intCount).Controls.Count > 0 Then
                If TypeOf (e.Item.Cells(intCount).Controls(0)) Is TextBox Then
                    ' This appears to be a TextBox-holding "data" cell
                    Dim strValue As String = CType(e.Item.Cells(intCount).Controls(0), TextBox).Text
                    ' Put value (or null if empty) back into relevant DataSet field
                    If strValue = "" Then
                        objEditedRow.Item(DataGrid1.Columns(intCount).SortExpression) = System.DBNull.Value
                    Else
                        objEditedRow.Item(DataGrid1.Columns(intCount).SortExpression) = strValue
                    End If
                End If
            End If
        Next


        ' Update backend data
        da_addgroup.Update(Ds_addgroup)

        ' Deselect DataGrid items and rebind
        With DataGrid1
            .SelectedIndex = -1
            .EditItemIndex = -1
            .DataSource = Ds_addgroup
            .DataBind()
        End With

        'Hide me on the add_customer.aspx page and update the ddl Group Value
        Dim aObj(0) As Object
        aObj(0) = intGroup
        delReturnValue.DynamicInvoke(aObj)


    End Sub

Now, I get an error at the delReturnValue.DynamicInvoke line that says: [Red] "Object reference not set to an instance of an object." [/Red]

On the mother page I declare the delegate class:
Code:
Public Class WebForm2
    Inherits System.Web.UI.Page
    Delegate Sub DelGroupsUpdated(ByVal myInt As Integer)

Then, in the Page load I call my subroutine via the Delegate.
Code:
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        If Not Page.IsPostBack Then
            Panel1.Visible = False
            btn_add.Enabled = False
            If GetQueryString("num", Me) = "new" Then
                'Load Defaults for a blank customer
                LoadDefaults()
            Else
                'Load Customer values
                LoadCustomer()
            End If
        End If

        Dim delGroup As New DelGroupsUpdated(AddressOf Me.UpdateGroupAndHide)

    End Sub

    Private Sub UpdateGroupAndHide(ByVal intGroup As Integer)
        'load dataset
        da_customer.SelectCommand.Parameters("@cust_num").Value = GetQueryString("num", Me)
        da_customer.Fill(Ds_customer_update)
        da_engineers.Fill(Ds_customer_update)
        da_country.Fill(Ds_customer_update)
        da_group.Fill(Ds_customer_update)
        da_states.Fill(Ds_customer_update)
        'rebind customere group ddl
        ddl_cust_group.DataBind()
        'select the new or updated value
        Me.ddl_cust_group.SelectedIndex = intGroup
        'Hide the panel
        Panel1.Visible = False
    End Sub

I am obviously not getting this right. Any help you have to offer would be great! (and please go easy on me, this is my first asp.net project...)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top