Passing arguments to a user control

A

AlBruAn

I have a user control that gets displayed via an AJAX modalpopupextender.
This control consists essentially of a text box, a label, three pushbuttons,
and some panels. If it's desired to have it display a db record, it will
retrieve it, populate the label with its value and set the label and an OK
pushbutton's visible properties to true; if it's to be used to edit/enter a
db record, it will set the visible properties of the text box and that of a
Save and a Cancel pushbuttons equal to true. Ideally, I'd like to be able to
simply register the control on whatever pages require it, drop a pushbutton
onto the page and write one or two lines of code to set the display/edit mode
and an ID value in the user control and another line of code to popup the
control when the form's pushbutton is clicked. Is there any way to
accomplish this?

I tried the properties route, but it doesn't seem to want to work for me.
The problem is I need to know the value of one of them prior to displaying
the user control, but the property doesn't get set until after the Page_Load
event for the user control fires ... too late to be of use.

In the code below, which comes from the parent form, notes is the ID of the
user control and mpe represents the ModalPopupExtender having the ID of the
user control as its PopupControlID. The first time the user clicks on the
button named btn, the popup acts as if it's in the Display mode; subsequent
button clicks causes the popup to act correctly, with it being in the Edit
mode. If I didn't stick in that bit of code in the parent form's Page_Load
event, none of the pushbuttons would display thus preventing the user from
dismissing the dialog box.

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
If Not IsPostBack Then
notes.Action = "Display"
notes.OverpaymentID = 368
End If
End Sub

Sub btn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles
btn.Click
notes.Action = "Edit"
notes.OverpaymentID = 368

mpe.Show()
End Sub
End Class

Following is the code-behind in my user control:

Imports Bcbst.IM.FRO.BusinessObjects
Imports Bcbst.IM.FRO.BusinessObjects.UserControl

Partial Class MyNotes
Inherits System.Web.UI.UserControl
'Implements IPostBackDataHandler

Dim status As Hashtable = New Hashtable()

Public Property Action() As String
Get
Return ViewState("Action")
End Get
Set(ByVal value As String)
ViewState("Action") = value
End Set
End Property

Public Property OverpaymentID() As Integer
Get
Return CInt(ViewState("OverpaymentID"))
End Get
Set(ByVal value As Integer)
ViewState("OverpaymentID") = value
End Set
End Property

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
If IsPostBack Then
If ViewState("Action") = "Edit" Then
btnSave.Visible = True
btnCancel.Visible = True
txtNotes.Visible = True
btnOK.Visible = False
lblNotes.Visible = False
ElseIf ViewState("Action") = "Display" Then
btnOK.Visible = True
lblNotes.Visible = True
txtNotes.Visible = False
btnSave.Visible = False
btnCancel.Visible = False
End If
If IsNothing(ViewState("Status")) Then
For i As Integer = 0 To Me.Parent.Parent.Controls.Count - 1
If Me.Parent.Parent.Controls(i).UniqueID <> "notesPopup" Then
' Code to disable all other controls on form
End If
ViewState("Status") = status
End If
Else
If ViewState("Action") = "Edit" Then
btnSave.Visible = True
btnCancel.Visible = True
txtNotes.Visible = True
btnOK.Visible = False
lblNotes.Visible = False
ElseIf ViewState("Action") = "Display" Then
btnOK.Visible = True
lblNotes.Visible = True
txtNotes.Visible = False
btnSave.Visible = False
btnCancel.Visible = False

' Code to retrieve data from the DB based on the OverpaymentID
and display it
End If
End If
End Sub

Sub ResetControls(ByVal sender As Object, ByVal e As EventArgs)
status = ViewState("Status")

For Each ctrl As Control In Me.Parent.Parent.Controls
If ctrl.UniqueID <> "notesPopup" Then
' Code to re-enable any previously disabled controls
End If
Next

ViewState("Status") = Nothing
End Sub

Sub SaveNote(ByVal sender As Object, ByVal e As EventArgs)
' Code to save any changes in the note to the DB
ResetControls(sender, e)
End Sub

'Public Function LoadPostData(ByVal postDataKey As String, ByVal
postCollection As System.Collections.Specialized.NameValueCollection) As
Boolean Implements System.Web.UI.IPostBackDataHandler.LoadPostData

'End Function

'Public Sub RaisePostDataChangedEvent() Implements
System.Web.UI.IPostBackDataHandler.RaisePostDataChangedEvent

'End Sub
End Class

Can anyone find what I'm doing wrong with this?
 
B

bruce barker

you fail to understand how the modalpopup works. when you click the trigger,
it just unhides the content, no postback is done. this means your user
control will display as set on the first page render.


-- bruce (sqlwork.com)
 

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

Top