Wednesday

binary serialization c#


using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace BinarySerializationTest
{
class Program
{
static void Main(string[] args)
{

// Serialization of String Object

String writeData = "Microsoft .NET Framework 2.0";
FileStream writeStream = new FileStream("C:\\Object.data", FileMode.Create);
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(writeStream, writeData);
writeStream.Close();

// Deserialization of String Object

FileStream readStream = new FileStream("C:\\Object.data", FileMode.Open);
String readData = (String) formatter.Deserialize(readStream);
readStream.Close();
Console.WriteLine(readData);

Console.Read();
}
}
}

Friday

set selected value dropdown javascript

following script allows to set value in dropdown with javascript

<script language='javascript'>
function setSelected(s, v) {
for ( var i = 0; i < s.options.length; i++ ) {
if ( s.options[i].value == v ) {
s.options[i].selected = true;
return;
}
}
}
</script>

Tuesday

asp.net dynamic controls example

 
Imports System.ComponentModel
Imports System.Web.UI
Imports System.Text
Imports System.Web.UI.WebControls
Imports System.Collections.Specialized
Public Class DevidedTextBox
'1. Inherits WebControl
Inherits WebControl
'2.implements IPostBackDataHandler if it's required to read Postback data
Implements IPostBackDataHandler
'3.implements IPostBackEventHandler if it's required to have events like "OnInit", "onChanged" etc.
Implements IPostBackEventHandler

Private ArraYOfTextBoxes As New Collection
Private m_AmountOfBoxes As Byte

Public Event ValidationError(ByVal ErrorId As Byte, ByVal ErrorDes As String)

Public Property AmountOfBoxes() As Byte
Get
m_AmountOfBoxes = ViewState(Me.ID & "_AmountOfBoxes")
Return m_AmountOfBoxes
End Get

Set(ByVal Value As Byte)
m_AmountOfBoxes = Value
ViewState(Me.ID & "_AmountOfBoxes") = m_AmountOfBoxes

End Set
End Property

'4.Must write this stuff 'case of implementation of the interfaces
Public Overridable Sub RaisePostBackEvent(ByVal eventArg As String) Implements IPostBackEventHandler.RaisePostBackEvent
End Sub
Public Overridable Sub RaisePostDataChangedEvent() Implements IPostBackDataHandler.RaisePostDataChangedEvent
End Sub


'5.Code procedure for reading postback data
Public Overridable Function LoadPostData(ByVal postdatakey As String, ByVal postCollection As NameValueCollection) As Boolean Implements IPostBackDataHandler.LoadPostData

For i As Byte = 1 To AmountOfBoxes
Dim txtBox As TextBox = ArraYOfTextBoxes(i)
txtBox.Text = postCollection(txtBox.ID)
Next

Return True
End Function

Private m_AllRequired As Boolean = True
Public Property AllRequired() As Boolean
Get
m_AllRequired = ViewState(Me.ID & "_AllRequired")
Return m_AllRequired
End Get
Set(ByVal Value As Boolean)
m_AllRequired = Value
ViewState(Me.ID & "_AllRequired") = m_AllRequired
End Set
End Property




Private Sub DevidedTextBox_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Init
'6.Register controls as postback receiver, required !!!
Page.RegisterRequiresPostBack(Me)

Dim i As Integer

For i = 1 To m_AmountOfBoxes

Dim intTextBox As New TextBox
With intTextBox
.ID = Me.ID & "txt" & i
.MaxLength = 1
.Width = New Unit(15)
.BorderWidth = New Unit(1)

If i <> m_AmountOfBoxes Then
Dim nextname As String = Me.ID & "txt" & i + 1
.Attributes.Add("onkeypress", "ChangeFocus(this,'" & nextname & "');")
End If

End With

ArraYOfTextBoxes.Add(intTextBox, i)
Next

End Sub

Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)

'writer.WriteLine("<script language='javascript'>")
'writer.WriteLine(" function ChangeFocus(obj,fild)")
'writer.WriteLine("{")
'writer.WriteLine("")
'writer.WriteLine(" if(obj.value.length==obj.maxLength)")
'writer.WriteLine(" {")
'writer.WriteLine(" // CopyToHiddenFields(obj)")
'writer.WriteLine(" // MaskSSN(obj)")
'writer.WriteLine(" document.all[fild].focus()")
'writer.WriteLine(" document.all[fild].select()")
'writer.WriteLine("")
'writer.WriteLine(" }")
'writer.WriteLine("")
'writer.WriteLine("}")
'writer.WriteLine("")
'writer.WriteLine("</script>")
'writer.WriteLine("")

For Each txtBox As TextBox In ArraYOfTextBoxes
txtBox.RenderControl(writer)
Next
End Sub

Private Sub CollectValues()
Dim bUpdate As Boolean = False
Dim sValue As String
' Dim inChar(AmountOfBoxes) As Byte
m_Value = ""
For i As Byte = 1 To AmountOfBoxes
Dim txtBox As TextBox = ArraYOfTextBoxes(i)
'inChar(i) = txtBox.Text
m_Value += txtBox.Text

Next
End Sub

Private Sub SpreadValues()
If Not m_Value Is Nothing Then
For i As Byte = 1 To ArraYOfTextBoxes.Count
Dim txtBox As TextBox = ArraYOfTextBoxes(i)
If m_Value.Length < i Then
txtBox.Text = ""
Else
txtBox.Text = m_Value.Chars(i)
End If
Next
End If
End Sub

Private m_Value As String
Public Property Text() As String
Get
m_Value = ViewState(Me.ID & "_Value")
Return m_Value
End Get
Set(ByVal Value As String)
m_Value = Value
ViewState(Me.ID & "_Value") = m_Value
SpreadValues()
End Set
End Property

Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
If Page.IsPostBack Then
CollectValues()
ViewState(Me.ID & "_Value") = m_Value
Else
m_Value = ViewState(Me.ID & "_Value")
SpreadValues()
End If
End Sub

End Class

test smtp server with powershell

Send-MailMessage -SMTPServer smtp.domain.com -To [email protected] -From [email protected] -Subject "This is a test email" -Body ...