Wednesday, September 24, 2008

ParamArray as a Parameter

Usually , we have to declare a (x) number of parameter for a method. But Mr Sascha has found a way to use ParamArray as a parameter , so that the method can accept any number of paramaters.

In this example , we'll call a sub , provided with few parameters

AddSilaPilih(ddlPaymentType, ddlDocLayer, ddlDocType)



Public Sub AddSilaPilih(ByVal ParamArray ddlObj() As DropDownList)
dim newItem As New ListItem
newItem.Value = 0
newItem.Text = "--Options--"
For i As Integer = 0 To ddlObj.GetLength(0) - 1
ddlObj(i).Items.Add(newItem)
ddlObj(i).SelectedIndex = ddlObj(i).Items.Count - 1
Next
End Sub



Credit to Mr Sascha

Thursday, September 11, 2008

Passing parameter to dynamic loaded user control

After a short discussion with Sascha

Create a class (clsHandle) that inherits System.Web.UI.UserControl

Create any variable or property
Public KeyProcessID As Integer

Create an EventHandler (so userControl can handle the event)
Public Event onShowDetail As EventHandler

Create a sub which will be called by user control's parent

Public Sub ShowDetail(ByVal intKeyProcessID As Integer)
KeyProcessID = intKeyProcessID
RaiseEvent onShowDetail(Me, EventArgs.Empty) 'Raise event
End Sub


In the page that will load the user control

ucLoad = Page.LoadControl("user control's path")
pnlUC.Controls.Add(ucLoad)
CType(ucLoad, clsHandle).ShowDetail(9)


In the User Control

Inherits clsHandle

Protected Sub Page_onShowDetail(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.onShowDetail
MsgBox(Me.KeyProcessID)
End Sub