473,386 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Help using delegates to call form function from a control

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
1 2605
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Stephen | last post by:
I am new to C# and can't get my head round what delegates are and what they are for. can anyone enlighten me?
9
by: Edward | last post by:
Hello I hope someone could help me I'm trying to prevent code from running before the thread I created completes. Here's the code snippet DataTransformerWorker dtw = new...
5
by: Anand Ganesh | last post by:
Hi All, I need some help. I am sort of not sure how to approach this problem. I have a MAINPROGRAM. This is the core application. I have asked two of my staff to developed two different...
22
by: Jeff Louie | last post by:
Well I wonder if my old brain can handle threading. Dose this code look reasonable. Regards, Jeff using System; using System.Diagnostics; using System.IO; using System.Threading;
8
by: Nicky Smith | last post by:
Hello, I'm reading Mike Gunderloy's Mcad Vb.net book, and I've also read the MS press Mcad book for the same topic ".. Windows based applications with VB.net" for exam 70-306. In the...
4
by: AMDRIT | last post by:
I am trying to understand Delegates and where/when to use them. I can see one potential use of a delegate (on form closing, set the cancel property in the event arguments.) Does anyone have a...
9
by: Terry Olsen | last post by:
I'm running an asynchronous Socket. In the ReceiveCallback method, I need to append what is received to a textbox on the main form. I have this code: Private Sub ToChatWindow(ByVal msg As...
4
by: Miro | last post by:
I am trying to understand delegates and I think I do understand them ... just hoping if someone can tell me im on the right track. So far what I have read is that a Delegate is an Asynchronus call...
2
by: hcaptech | last post by:
This is my Test.can you help me ? 1.Which of the following statement about C# varialble is incorrect ? A.A variable is a computer memory location identified by a unique name B.A variable's name...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.