Help using delegates to call form function from a control

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • keith.walter@syron.com

    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.as px 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.U serControl
    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_Updat eCommand(ByVal source As Object, ByVal e As
    System.Web.UI.W ebControls.Data GridCommandEven tArgs) Handles
    DataGrid1.Updat eCommand
    'Grab edited row for delgate call
    intGroup = e.Item.Cells(1) .Text

    'Fill DataSet and identify row to edit
    da_addgroup.Fil l(Ds_addgroup)
    Dim str_debug As String = e.Item.Cells(1) .Text
    Dim objEditedRow As DataRow =
    Ds_addgroup.Tab les("tbl_custom er_group").Rows .Find(e.Item.Ce lls(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.Co unt - 1
    If e.Item.Cells(in tCount).Control s.Count > 0 Then
    If TypeOf (e.Item.Cells(i ntCount).Contro ls(0)) Is
    TextBox Then
    ' This appears to be a TextBox-holding "data" cell
    Dim strValue As String =
    CType(e.Item.Ce lls(intCount).C ontrols(0), TextBox).Text
    ' Put value (or null if empty) back into relevant
    DataSet field
    If strValue = "" Then

    objEditedRow.It em(DataGrid1.Co lumns(intCount) .SortExpression ) =
    System.DBNull.V alue
    Else

    objEditedRow.It em(DataGrid1.Co lumns(intCount) .SortExpression ) =
    strValue
    End If
    End If
    End If
    Next


    ' Update backend data
    da_addgroup.Upd ate(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.as px page and update the ddl Group
    Value
    Dim aObj(0) As Object
    aObj(0) = intGroup
    delReturnValue. DynamicInvoke(a Obj)


    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.P age
    Delegate Sub DelGroupsUpdate d(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.EventArg s) 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 DelGroupsUpdate d(AddressOf
    Me.UpdateGroupA ndHide)

    End Sub

    Private Sub UpdateGroupAndH ide(ByVal intGroup As Integer)
    'load dataset
    da_customer.Sel ectCommand.Para meters("@cust_n um").Value =
    GetQueryString( "num", Me)
    da_customer.Fil l(Ds_customer_u pdate)
    da_engineers.Fi ll(Ds_customer_ update)
    da_country.Fill (Ds_customer_up date)
    da_group.Fill(D s_customer_upda te)
    da_states.Fill( Ds_customer_upd ate)
    'rebind customere group ddl
    ddl_cust_group. DataBind()
    'select the new or updated value
    Me.ddl_cust_gro up.SelectedInde x = 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

  • cbDevelopment

    #2
    Re: Help using delegates to call form function from a control

    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
    DelGroupsUpdate d. 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.

    keith.walter@sy ron.com wrote in
    news:1128724569 .699611.109220@ f14g2000cwb.goo glegroups.com:
    [color=blue]
    > My first asp.net app is almost "done" and I am stuck. Here is my
    > situation: I have a "mother" page add_customer.as px 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.U serControl
    > 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_Updat eCommand(ByVal source As Object, ByVal e As
    > System.Web.UI.W ebControls.Data GridCommandEven tArgs) Handles
    > DataGrid1.Updat eCommand
    > 'Grab edited row for delgate call
    > intGroup = e.Item.Cells(1) .Text
    >
    > 'Fill DataSet and identify row to edit
    > da_addgroup.Fil l(Ds_addgroup)
    > Dim str_debug As String = e.Item.Cells(1) .Text
    > Dim objEditedRow As DataRow =
    > Ds_addgroup.Tab les("tbl_custom er_group").Rows .Find(e.Item.Ce lls(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.Co unt - 1
    > If e.Item.Cells(in tCount).Control s.Count > 0 Then
    > If TypeOf (e.Item.Cells(i ntCount).Contro ls(0)) Is
    > TextBox Then
    > ' This appears to be a TextBox-holding "data" cell
    > Dim strValue As String =
    > CType(e.Item.Ce lls(intCount).C ontrols(0), TextBox).Text
    > ' Put value (or null if empty) back into relevant
    > DataSet field
    > If strValue = "" Then
    >
    > objEditedRow.It em(DataGrid1.Co lumns(intCount) .SortExpression ) =
    > System.DBNull.V alue
    > Else
    >
    > objEditedRow.It em(DataGrid1.Co lumns(intCount) .SortExpression ) =
    > strValue
    > End If
    > End If
    > End If
    > Next
    >
    >
    > ' Update backend data
    > da_addgroup.Upd ate(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.as px page and update the ddl
    > Group
    > Value
    > Dim aObj(0) As Object
    > aObj(0) = intGroup
    > delReturnValue. DynamicInvoke(a Obj)
    >
    >
    > 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.P age
    > Delegate Sub DelGroupsUpdate d(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.EventArg s) 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 DelGroupsUpdate d(AddressOf
    > Me.UpdateGroupA ndHide)
    >
    > End Sub
    >
    > Private Sub UpdateGroupAndH ide(ByVal intGroup As Integer)
    > 'load dataset
    > da_customer.Sel ectCommand.Para meters("@cust_n um").Value =
    > GetQueryString( "num", Me)
    > da_customer.Fil l(Ds_customer_u pdate)
    > da_engineers.Fi ll(Ds_customer_ update)
    > da_country.Fill (Ds_customer_up date)
    > da_group.Fill(D s_customer_upda te)
    > da_states.Fill( Ds_customer_upd ate)
    > 'rebind customere group ddl
    > ddl_cust_group. DataBind()
    > 'select the new or updated value
    > Me.ddl_cust_gro up.SelectedInde x = 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
    >
    >[/color]

    Comment

    Working...