Help using delegates to call form function from a control

K

keith.walter

My first asp.net app is almost "done" and I am stuck. 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.


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:
"Object reference not set to an instance of an object."

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!

Thanks, Keith
 
C

cbDevelopment

I'm not so much a Delegate person myself, I use events and event
handlers instead. However, without knowing too much about them, I
wonder as I look at the code, the user control has a delegate named
delReturnValue, but the parent page has a delegate sub named
DelGroupsUpdated. How is .NET going to know how to tie the two together
if they are named differently?

I hope it's that simple. Now if you want to know how to do it using
events, that I can help with.

(e-mail address removed) wrote in
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,738
Latest member
JinaMacvit

Latest Threads

Top