How to Programmatically access a control that lies within a within a header template

How to programmatically access a control that lies within a within a wizardview header template (or any template header I would imagine) in C#


<asp:Wizard ID="wContent" runat="server" ActiveStepIndex="0">
<HeaderTemplate>
<asp:Label ID="lblHeader" runat="server" Text="Text Here" /><br />
<br />
</HeaderTemplate>
</asp:Wizard>


(instance name of the wizard).FindControl("HeaderContainer").FindControl("instance name of the control") as type of control).Text = "Hello World!";

Tip Of the Day - Remove Duplicate Items from a List(Of T) (or a List of whatever the hell you want)

If you need to remove duplicates from a from a collection that implements IEnumerable without iterating through or overriding the .equals method you can do the following:


Dim Cars As New List(Of String)(New String() {"Blue", "Red", "Yellow", "Red", "Black", "Blue"})
Cars = Cars.Distinct().ToList()

Your list can be of anything you want. Strings, objects etc.. you can just for duplicates with the Distinct().ToList() method.

Want to Pass In a UserControl into your Class? Yes it’s possible.

I had a situation where I had a Payment UserControl that had say, 20 properties on it. I also had a Class file (App_Code) that I wanted to use to handle some of the business logic when the payment was complete. Say create tables in the database.

I could have A) Created a method on the class and sent in 20 arguments. B) Created 20 properties on that class and populate them all before I call the method. But that was waste of code to me, when I already had the information. I just wanted to do something with it.

So I thought, what if I just send in my UserControl into my class method by reference, then I can just use the properties directly to retrieve the value.  So I tried… no luck. Class can’t see my UserControl class.

Problem/Broken Version:

My UserControl’s class is Application_Controls_Payment

Partial Class Application_Controls_Payment
Inherits System.Web.UI.UserControl
'...
End Class

In my class file I tried to use it as an argument on a function that the page would send me. However this fails on the compiler.

Public Shared Function CreateTransactionRecordInDB(ByRef PaymentUserControl As Application_Controls_Payment) As Integer
'This Errors, as Application_Controls_Payment class from the UserControl can not be seen from this level
End Function

Solution:

So I created an Interface for all the properties, and Implemented it on my UserControl. Then Sent the UserControl as the Interface and low and behold, it works great!

Step 1: Create Interface with all the properties.

Public Interface PaymentControl
Property CreditCardNumber() As String
Property CCVCode() As String
Property ExpirationMonth() As String
Property ExpirationYear() As String
Property TransactionAmount() As Double
Property TransactionAmountAdditionalInfo() As String
Property TransactionDescription() As String
Property InvoiceNumber() As String
Property BillingFirstName() As String
Property BillingLastName() As String
Property BillingEmail() As String
Property BillingCompany() As String
Property BillingAddress() As String
Property BillingState() As String
Property BillingCity() As String
Property BillingZipCode() As String
Property CustomerID() As String
Property CustomerIPAddress() As String
Property EmailCustomerReceipt() As Boolean
Property EmailReceiptHeader() As String
Property EmailReceiptFooter() As String
Property EmailOfMerchant() As String
Property TestMode() As Boolean
Property ValidationGroup() As String
End Interface

Step 2: Implement this Interface To your UserControl

Partial Class Application_Controls_Payment
Inherits System.Web.UI.UserControl
Implements CheckOut.PaymentControl

Public Property CreditCardNumber() As String Implements CheckOut.PaymentControl.CreditCardNumber
Get
Return txtCCNumber.Text
End Get
Set(ByVal Value As String)
txtCCNumber.Text = Value
End Set
End Property
Public Property CCVCode() As String Implements CheckOut.PaymentControl.CCVCode
Get
Return txtCCVCode.Text
End Get
Set(ByVal Value As String)
txtCCVCode.Text = Value
End Set
End Property
'... Rest of Properites
End Class

Step 3: Change your Method Constructor to want the Interface class vs trying to call the UserControl’s class.
Public Shared Function CreateTransactionRecordInDB(ByRef PaymentUserControl As PaymentControl) As Integer
'Now you can reference the properties from the usercontrol
Dim oPaymentTransactions As New PaymentTransaction
oPaymentTransactions.BillingAddress = PaymentUserControl.BillingAddress
oPaymentTransactions.CCVCode = PaymentUserControl.CCVCode
oPaymentTransactions.CCExpirationMonth = PaymentUserControl.ExpirationMonth
'Rest of code here
End Function

And there you go.

Look up Enum Value by String

 

I had a need to take a string value and get it’s equivalent Enum type back.  This is how you can do that in VB.Net.

Public Enum PackageTypes
Subscription = 1
Sponsorship = 2
End Enum

Dim EnumValue As PackageTypes

EnumValue = CType([Enum].Parse(GetType(PackageTypes), "Subscription", True), PackageTypes)

What if it doesn’t exist? You want to check to see if it does first.

You can check to see like this:

If [Enum].IsDefined(GetType(PackageTypes), "Subscription") Then
'Run previous code snippet here
EnumValue = CType([Enum].Parse(GetType(PackageTypes), "Subscription", True), PackageTypes)
End IF


Simple Grid Boolean Value Yes/No from DB or CustomYes/CustomNo

I'm just putting this up here so I have it somewhere when I forget.

Sometimes I pull data from my database from a Bit column or boolean column and want to display more then True/False.

With a simple statement you can make it say anything you want.

<asp:TemplateField HeaderText="Is Active">
<ItemTemplate>
<asp:Label id="lblActive" runat="server" Text='<%#IIf(Boolean.Parse(Eval("Active").ToString()), "Yes", "No")%>' />
</ItemTemplate>
</asp:TemplateField>

Still can't sleep

My jet lag still screwing me up. I'm up at midnight and not asleep until 5am. Then I want to sleep during the day, ugh!