By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,084 Members | 1,207 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,084 IT Pros & Developers. It's quick & easy.

Help using delegates to call form function from a control

P: n/a
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).Sort Expression) =
System.DBNull.Value
Else

objEditedRow.Item(DataGrid1.Columns(intCount).Sort Expression) =
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

Nov 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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.

ke**********@syron.com wrote in
news:11**********************@f14g2000cwb.googlegr oups.com:
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).Sort Expression) =
System.DBNull.Value
Else

objEditedRow.Item(DataGrid1.Columns(intCount).Sort Expression) =
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


Nov 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.